Thursday, July 30, 2009

Numerically evaluate double integral

dblquad

Numerically evaluate double integral

Syntax

  • q = dblquad(fun,xmin,xmax,ymin,ymax)
    q = dblquad(fun,xmin,xmax,ymin,ymax,tol)
    q = dblquad(fun,xmin,xmax,ymin,ymax,tol,method)
    q = dblquad(fun,xmin,xmax,ymin,ymax,tol,method,p1,p2,...)

Description

q = dblquad(fun,xmin,xmax,ymin,ymax) calls the quad function to evaluate the double integral fun(x,y) over the rectangle xmin <= x <= xmax, ymin <= y <= ymax. fun(x,y) must accept a vector x and a scalar y and return a vector of values of the integrand.

q = dblquad(fun,xmin,xmax,ymin,ymax,tol) uses a tolerance tol instead of the default, which is 1.0e-6.

q = dblquad(fun,xmin,xmax,ymin,ymax,tol,method) uses the quadrature function specified as method, instead of the default quad. Valid values for method are @quadl or the function handle of a user-defined quadrature method that has the same calling sequence as quad and quadl.

dblquad(fun,xmin,xmax,ymin,ymax,tol,method,p1,p2,...) passes the additional parameters p1,p2,... to fun(x,y,p1,p2,...). Use [] as a placeholder if you do not specify tol or method. dblquad(fun,xmin,xmax,ymin,ymax,[],[],p1,p2,...) is the same as dblquad(fun,xmin,xmax,ymin,ymax,1.e-6,@quad,p1,p2,...)

Example

fun can be an inline object

  • Q = dblquad(inline('y*sin(x)+x*cos(y)'), pi, 2*pi, 0, pi)

or a function handle

  • Q = dblquad(@integrnd, pi, 2*pi, 0, pi)

where integrnd.m is an M-file.

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

The integrnd function integrates y*sin(x)+x*cos(y) over the square pi <= x <= 2*pi, 0 <= y <= pi. Note that the integrand can be evaluated with a vector x and a scalar y .

Nonsquare regions can be handled by setting the integrand to zero outside of the region. For example, the volume of a hemisphere is

  • dblquad(inline('sqrt(max(1-(x.^2+y.^2),0))'),-1,1,-1,1)

or

  • dblquad(inline('sqrt(1-(x.^2+y.^2)).*(x.^2+y.^2<=1)'),-1,1,-1,1
    )

No comments:

Post a Comment