numerical integration uc berkeley fall 2004, e77 pack/e77 copyright 2005, andy packard. this work is...
TRANSCRIPT
Numerical IntegrationUC Berkeley
Fall 2004, E77http://jagger.me.berkeley.edu/~pack/e77
Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.0/ or send a letter to
Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
IntegrationFor a function f,
The “integral of f from a to b” is the area under the graph of the function.
If f is continuous, then the area is well defined, as the common limit of upper and lower sums. The integral is denoted
a b x
graph of f(x)
b
a
dxxf )(
Integration: Fundamental theorem of Calculus
If a function g is the antiderivative of f, namely
for all x, then the fundamental theorem of calculus gives that the integral of f can be computed by evaluating g
But finding antiderivatives can be hard…
In many cases of engineering and scientific (and economics, etc) interest
–the functions do not have known antiderivatives, so…–the integral (area) must be approximated by a finite number of
function evaluations.
)()()( agbgdxxfb
a
xfxg
Numerical Integration: Trapezoid
a b x
graph of f(x)
a b x
graph of f(x)
approximate this region
with this trapezoid
afbfab
2
AREA
Composite Trapezoid
a b x
graph of f(x)
Apply “trapezoid” approximation to k subdivisions of [a b]
a b x
graph of f(x)
“trapezoid” approximation with 3 subdivisions of [a b]
Composite Trapezoid
a b x
graph of f(x)
“trapezoid” approximation with 3 subdivisions of [a b]
h
Let h=(b-a)/3. The sum of all the approximations is
)()2(2)(2)(2
)()2(2
)2()(2
)()(2
bfhafhafafh
bfhafh
hafhafh
hafafh
I
Derivation of Simpson’s Rule: Suppose f(x)=x3
443
4
1 abdxx
b
a
3223
4
1abaabbab
332233
3
2
3
1
3
1
3
2
4
1aabaabbbab
333
3
2
3
1
3
2
4
1aabbab
33
3
3
2
23
8
3
2
4
1a
abbab
33
3
24
6
1a
abbab
bfab
fafab2
46
1
Simpson’s Rule: Suppose f(x)=x2
332
3
1 abdxx
b
a
bfab
fafab2
46
1
Simpson’s Rule: Suppose f(x)=x
22
2
1 abdxx
b
a
bfab
fafab2
46
1
Simpson’s Rule: Suppose f(x)=1
abdxb
a
1
bfab
fafab2
46
1
Simpson’s Rule: Derivation
bfab
fafabdxxfb
a 24
6
1)(
Put all of those together, along with
Hence: If f is any cubic polynomial, then
This is the basis for Simpson’s rule.
b
a
b
a
b
a
dxxgdxxfdxxgxf )()()()(
b
a
b
a
dxxfdxxf )()(
Simpson’s Rule
bfab
fafabI2
46
1Simpson
For any function f, the Simpson’s approximation to
is
b
a
dxxf )(
Evaluate the function at the endpoints
and in the middle
Composite Simpson’s Rule
a b x
graph of f(x)
Simpson on 3 subdivisions of [a b]
h
h=(b-a)/3
hafafafh
I hS 21 4
6
hafafhafh
I hS 24
62
32
bfafhafh
I hS 2
53 426
Add them up. Total of 7 function evaluations.
Composite Simpson’s Rule
a b x
graph of f(x)
Simpson on 2 subdivisions of [a b]h=(b-a)/2
hafafafh
I hS 21 4
6
bfafhafh
I hS 2
32 46 5 function
evaluations
bfafhafafafh
hh 23
2 42)(4)(6
giving the approximation as IS=I1S+I2
S
Numerical Integration: Ad-Hoc stopping criteria
Pick a method (trapezoid, or Simpson’s).
Set a stopping tolerance TOL.
Pick k, an initial number of subdivisions
Iterate as below–Apply composite method using k divisions–Apply composite method using 2k divisions–If answers are within TOL, stop, and return the 2k division
answer– If answers are not within TOL, increase k and repeat.
With only this much analysis, the computed answer is not specifically known to be accurate to any specific precision.
Adaptive Stepsize
Only use small h (the “stepsize”) where the convergence demands it. Recursive implementation is straightforward.
function I = adr(fh,a,b,tol)
Compute I1 using 1 subdivision
Compute I2 using 2 subdivisions
If the answers are within tol, I = I2;
Else
m = (a+b)/2;
ILeft = adr(fh,a,m,tol/2);
IRight = adr(fh,m,b,tol/2);
I = ILeft + IRight;
end
Error Analysis
More error analysis can be done making assumptions about the magnitude of the derivatives of f
–A bound on the magnitude of 3rd derivative of f yields accuracy bounds for the Trapezoid method
–A bound on the magnitude of 5th derivative of f yields accuracy bounds for the Simpson’s method
Learn more about these in Math 128A
Matlab functions
trapz –fixed trapezoidal approximation
–data is vectors x and y (y represents f(x))
–1 division between each data pair
quad–Adaptive Simpson’s method–Data is function handle, interval endpoints, stopping tolerance