Integration

Introduction

The area beneath a section of a function F(x) can be determined by numerically integrating F(x), a process referred to as quadrature. The MATLAB® quadrature functions are:

quad

Use adaptive Simpson quadrature

quadl

Use adaptive Lobatto quadrature

quadgk

Use adaptive Gauss-Kronrod quadrature

quadv

Vectorized quadrature

dblquad

Numerically evaluate double integral

triplequad

Numerically evaluate triple integral

To integrate the function defined by humps.m from 0 to 1, use

q = quad(@humps,0,1)

q =
    29.8583

Both quad and quadl operate recursively. If either method detects a possible singularity, it prints a warning.

You can include a fourth argument for quad or quadl that specifies an absolute error tolerance for the integration. If a nonzero fifth argument is passed to quad or quadl, the function evaluations are traced.

Example: Arc Length

You can use quad or quadl to compute the length of a curve. Consider the curve parameterized by the equations

x(t) = sine 2t, y(t) = cosine t, z(t) = t

where t is an element of the range from 0 to 3 pi.

A three-dimensional plot of this curve is

t = 0:0.1:3*pi;
plot3(sin(2*t),cos(t),t)

The arc length formula says the length of the curve is the integral of the norm of the derivatives of the parameterized equations

integral from 0 to 3 pi  of the square root of 4 cosine (2t) squared + sine (t) squared +1 dt

The function hcurve computes the integrand

function f = hcurve(t)
f = sqrt(4*cos(2*t).^2 + sin(t).^2 + 1);

Integrate this function with a call to quad

len = quad(@hcurve,0,3*pi)

len =
   1.7222e+01

The length of this curve is about 17.2.

Example: Double Integration

Consider the numerical solution of

double integral: integral from y min to y max of the integral from x min to x max of f(x,y) dx dy

For this example f(x,y) = ysin(x) + xcos(y). The first step is to build the function to be evaluated. The function must be capable of returning a vector output when given a vector input. You must also consider which variable is in the inner integral, and which goes in the outer integral. In this example, the inner variable is x and the outer variable is y (the order in the integral is dxdy). In this case, the integrand function is

function out = integrnd(x,y)
out = y*sin(x) + x*cos(y); 

To perform the integration, two functions are available in the funfun directory. The first, dblquad, is called directly from the command line. This M-file evaluates the outer loop using quad. At each iteration, quad calls the second helper function that evaluates the inner loop.

To evaluate the double integral, use

result = dblquad(@integrnd,xmin,xmax,ymin,ymax);

The first argument is a string with the name of the integrand function. The second to fifth arguments are

xmin

Lower limit of inner integral

xmax

Upper limit of the inner integral

ymin

Lower limit of outer integral

ymax

Upper limit of the outer integral

Here is a numeric example that illustrates the use of dblquad.

xmin = pi;
xmax = 2*pi;
ymin = 0;
ymax = pi;
result = dblquad(@integrnd,xmin,xmax,ymin,ymax)

The result is -9.8698.

By default, dblquad calls quad. To integrate the previous example using quadl (with the default values for the tolerance argument), use

result = dblquad(@integrnd,xmin,xmax,ymin,ymax,[],@quadl);

Alternatively, you can pass any user-defined quadrature function name to dblquad as long as the quadrature function has the same calling and return arguments as quad.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS