MATLAB Mathematics - SERC - Index of

MATLAB Mathematics - SERC - Index of MATLAB Mathematics - SERC - Index of

serc.iisc.ernet.in
from serc.iisc.ernet.in More from this publisher
15.11.2014 Views

4 Function Functions 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 3π ∫ 0 2 4cos( 2t) 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 2 + sin( t) + 1 dt The length of this curve is about 17.2. Example: Double Integration Consider the numerical solution of ymax ∫ ymin xmax ∫ xmin fxy ( , ) dxdy For this example fxy ( , ) = 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); 4-28

Numerical Integration (Quadrature) 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 xmax ymin ymax Lower limit of inner integral Upper limit of the inner integral Lower limit of outer integral 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. 4-29

4 Function Functions<br />

A three-dimensional plot <strong>of</strong> this curve is<br />

t = 0:0.1:3*pi;<br />

plot3(sin(2*t),cos(t),t)<br />

The arc length formula says the length <strong>of</strong> the curve is the integral <strong>of</strong> the norm<br />

<strong>of</strong> the derivatives <strong>of</strong> the parameterized equations<br />

3π<br />

∫<br />

0<br />

2<br />

4cos( 2t)<br />

The function hcurve computes the integrand<br />

function f = hcurve(t)<br />

f = sqrt(4*cos(2*t).^2 + sin(t).^2 + 1);<br />

Integrate this function with a call to quad<br />

len = quad(@hcurve,0,3*pi)<br />

len =<br />

1.7222e+01<br />

2<br />

+ sin( t)<br />

+ 1 dt<br />

The length <strong>of</strong> this curve is about 17.2.<br />

Example: Double Integration<br />

Consider the numerical solution <strong>of</strong><br />

ymax<br />

∫<br />

ymin<br />

xmax<br />

∫<br />

xmin<br />

fxy ( , ) dxdy<br />

For this example fxy ( , ) = ysin( x)<br />

+ xcos( y)<br />

. The first step is to build the<br />

function to be evaluated. The function must be capable <strong>of</strong> returning a vector<br />

output when given a vector input. You must also consider which variable is in<br />

the inner integral, and which goes in the outer integral. In this example, the<br />

inner variable is x and the outer variable is y (the order in the integral is dxdy).<br />

In this case, the integrand function is<br />

function out = integrnd(x,y)<br />

out = y*sin(x) + x*cos(y);<br />

4-28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!