quadrature integration in matlab

You can use the built-in quad function in MATLAB to perform quadrature integration. The general syntax for quad is:

main.m
I = quad(fun, a, b)
20 chars
2 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.m
fun = @(x) sin(x);
a = 0;
b = pi;
I = quad(fun, a, b)
54 chars
5 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.m
fun = @(x) quad(@(y) x.*y, -1, 1);
35 chars
2 lines

Then use quad to compute the integral of fun over the interval [-1,1]:

main.m
I = quad(fun, -1, 1)
21 chars
2 lines

The output should be 0, which is the exact value of the double integral.

gistlibby LogSnag