This page contains SageMath and Octave (or
MATLAB) code that will approximate definite integrals of
the form
\[
\int_a^b f(x)\,dx.
\]
SageMath is free mathematical computing software that is written in Python. You can download it here, or try it without downloading here.
The following code requires the user to input the integrand $f$, endpoints of
the interval $[a,b]$,
and number of partition intervals $N$. In the SageCell below, these inputs
comprise the first four lines. Do not change any of the code beyond those
first four lines.
This code outputs the values of all five of the numerical methods
that we've studied in class.
The first time you run this code, it might take a while. You can change the
parameters and re-run the code without refreshing the page. Successive runs
should be much quicker.
Octave is free mathematical computing software that is designed to mimic MATLAB. The syntax and most of the built-in functions for Octave are the same as for MATLAB. You can download Octave for free here. You can buy MATLAB here.
The following code was written for MATLAB and shared with us by
Justin Mears. Thank you, Justin!
The required inputs to use this code are exactly the same as for the
Sage code above, but the syntax is slightly different. The
integrand needs to be an anonymous function handle, so the
@(x)
must be the first code after the
equal sign. You also need to include the semicolons after each line.
The outputs are: left, right, trapezoid, midpoint, Simpson.
To visit my personal page, click here.
© 2009-20 Justin M. Ryan
Your use of any material found at this site is subject to this
Creative Commons License.