I Fxyda I Fxydxdy: Doing Physics With Matlab Mathematical Routines
I Fxyda I Fxydxdy: Doing Physics With Matlab Mathematical Routines
I Fxyda I Fxydxdy: Doing Physics With Matlab Mathematical Routines
MATHEMATICAL ROUTINES
COMPUTATION OF TWO-DIMENSIONAL
INTEGRALS:
DOUBLE or SURFACE INTEGRALS
by bx
I f ( x, y ) dA I a f ( x, y ) dx dy
A ay x
Ian Cooper
School of Physics, University of Sydney
ian.cooper@sydney.edu.au
math_integration_2D.m
Demonstration mscript evaluating the integral of functions of the form f(x,y) using a
two-dimensional form of Simpsons 1/3 rule. The code can be changed to integrate
functions between the specified lower and upper bounds.
simpson2d.m
Function to give the integral of a function f(x,y) using a two-dimensional form of
Simpsons 1/3 rule. The format to call the function is
Ixy = simpson2d(f,ax,bx,ay,by)
We want to compute a number expressing the definite integral of the function f(x,y)
between two specific limits (ax, bx) and (ay, by)
by bx
I a f ( x, y ) dx dy
ay x
This rule is based on using a quadratic polynomial approximation to the function f(x)
over a pair of partitions. N-1 is the number of partitions where N must be odd and
x h = (b a) / (N-1). The integral is expressed below and is known as the
composite Simpsons 1/3 rule.
I
h
3
f1 f N 4( f2 f4 ... f N 2 2( f3 f5 ... f N 1)
h
I cf T
3
can be approximated by applying Simpsons 1/3 rule twice once for the x integration
and once for the y integration with N partitions for both the x and y values.
x-values: x1 x2 x3 xc xN
y-values: y1 y2 y3 yc yN
The lower and upper bounds determine the size of the partitions
b ax b ay
dx hx x dy hy y
N 1 N 1
Therefore, the two-dimensional Simpsons rule which is used to estimate the value
of the surface integral can be expressed as
hx hy N N
I Smn Fmn
9 m1 n 1
1 4 2 4 2 4 2 4 1
4 16 8 16 8 16 8 16 4
2 8 4 8 4 8 4 8 2
4 16 8 16 8 16 8 16 4
2 8 4 8 4 8 4 8 2
4 16 8 16 8 16 8 16 4
2 8 4 8 4 8 4 8 2
4 16 8 16 8 16 8 16 4
1 4 2 4 2 4 2 4 1
I xy1
by bx 5 2 x 2 y 3 dx dy 416
(1)
ay a x
f ( x, y ) dx dy
1 0
The exact value of the integral can be found analytically and its value is 416. So we
can compare the numerical estimate with the known exact value.
Clear all variables, close any Figure Windows and clear the Command Window:
clear all
close all
clc
Enter the number of partitions (must be an odd number), and the lower and
upper bounds for x and y and calculate the range for the x and y values
num = 5;
xMin = 0;
xMax = 2;
yMin = 1;
yMax = 5;
x = linspace(xMin,xMax,num);
y = linspace(yMin,yMax,num);
y= 1 2 3 4 5
yy =
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
f=
0 0.2500 1.0000 2.2500 4.0000
0 2.0000 8.0000 18.0000 32.0000
0 6.7500 27.0000 60.7500 108.0000
0 16.0000 64.0000 144.0000 256.0000
0 31.2500 125.0000 281.2500 500.0000
row vectors
xx(4,:) = [0 0.5 1.0 1.5 2.0]
yy(4,:) = [4 4 4 4 4 ]
xx(5,5) = 2 yy(5,5) = 5 f(5,5) = 500
1 4 2 4 1
4 16 8 16 4
2 8 4 8 2 Simpsons [2D]
coefficients
4 16 8 16 4
1 4 2 4 1
Fig. 1. The grid points for N = 5 and how these points relate to the Matlab
matrices.
scx = meshgrid(sc,sc);
scxy = ones(num,num);
scxy(2:2:num-1,:) = scx(2:2:num-1,:)*sc(2);
scxy(3:2:num-2,:) = scx(3:2:num-2,:)*sc(3);
scxy(1,:) = sc';
scxy(num,:) = sc';
num = length(f);
hx = (bx-ax)/(num-1); hy = (by-ay)/(num-1);
h = hx * hy / 9;
I xy1
5 2 x 2 y 3 dx dy 416
The exact value of the integral is 416
1 0
With only 5 partitions and 25 (5x5) grid points, the numerical estimate is 416, the
same as the exact value.
Volume f ( x, y ) dx dy
A
To gain an intuitive feel for double integrals, the volume of the region enclosed by the
area A is equal to the value of the double integral.
Base of box the lower bounds (ax and ay) and upper bounds (bx and by) determine
the area of the rectangular base of the box
by bx
Volume V k dx dy
ay ax
Box
k = 1 ax = 0 bx = 1 ay = 0 by = 1 N = 299
f(x,y) = 1 - x
Base of box the lower bounds (ax and ay) and upper bounds (bx and by) determine
the area of the rectangular base of the box
1 x dx dy
by bx
Volume V
ay ax
ax = 0 bx = 1 ay = 0 by = 1 N = 299
f(x,y) = x2 + y2
Base of box the lower bounds (ax and ay) and upper bounds (bx and by) determine
the area of the rectangular base of the surface
x y 2 dx dy
by bx
Volume V 2
ay ax
ax = -1 bx = 1 ay = 0 by = 1 N = 299
f ( x, y ) cos x sin y
Base of box the lower bounds (ax and ay) and upper bounds (bx and by) determine
the area of the rectangular base of the surface
cos x sin y dx dy
by bx
Volume V
ay ax
ax = 0 bx = /2 ay = 0 by = /2 N = 299
2 a3
Volume of a hemisphere of radius a V
3
Function
x2 y 2 a2 f ( x, y ) a 2 x 2 y 2 x2 y2 a2 f ( x, y ) 0
Volume V
ay
by
ax
bx
a 2 x 2 y 2 dx dy
ax = -1 bx = 1 ay = -1 by = 1
A logical Matlab function is used to define the function when y > 1 x. The code to
define the function is
y 1 x f ( x, y ) h y 1 x f ( x, y ) 0
by bx
Volume V h dx dy
ay ax
ax = 0 bx = 1 ay = 0 by = 1 height h = 6
Even with N = 2999 the calculation took less than 1.0 s on a fast Windows computer.
The differences between the exact and computed values is due to the rectangular grid
and the condition on the function being zero when y > 1 x
(y = 1 x is a diagonal line and the grid is rectangular).
A logical Matlab function is used to define the function when y > 1 x. The code to
define the function is
f = h .* ones(num,num);
f(yy > 1 - xx) = 0;
b b
V f ( , ) d d
a a
xP cos yP sin
The grid pattern for the integration is shown in figure (2) for N = 9. At each point the
function and Simpson [2D] coefficients are calculated.
2 a
f ( x, y ) h V h d d
0 0
ax = 0 bx = 2 ay = 0 by = 2 N = 299
2 a
f ( x, y ) 2 V 2 d d
0 0
ax = 0 bx = 2 ay = 0 by = 2 N = 299