You can use the built-in quad
function in MATLAB to perform quadrature integration. The general syntax for quad
is:
main.m20 chars2 lines
where fun
is a function handle specifying the integrand, and a
and b
are the lower and upper limits of integration, respectively. The output, I
, is the approximate value of the integral of fun
from a
to b
.
Here's an example that uses quad
to compute the integral of sin(x)
from 0
to pi
:
main.m54 chars5 lines
The output should be 2
, which is the exact value of the integral.
You can also use quad
to compute a double integral by nesting calls to the function. For example, to compute the double integral of x*y
over the square [-1,1]x[-1,1]
, you could define the function fun
as follows:
main.m35 chars2 lines
Then use quad
to compute the integral of fun
over the interval [-1,1]
:
main.m21 chars2 lines
The output should be 0
, which is the exact value of the double integral.
gistlibby LogSnag