Title: Lecture 28: Comparison of different numerical integrators
1Lecture 28 Comparison of different numerical
integrators
- Adaptive Simpsons and Trapezoid Rules
- 2. Romberg Integration
- 3. Adaptive Gaussian Quadrature
2adaptivesimpson.m
test of adaptive Simpson's rule and comparison
with composite Trapezoid and Composite
Simpson's tol10(-10) a1 b4 f1inline('si
n(x).2') disp('a',num2str(a),'
b',num2str(b)) display limits of
integration intexactquad(f1,a,b,10(-16))
fadaptive funccountquad(f1,a,b,tol) disp('Int
egration using adaptive simpson gives
error',num2str(fadaptive-intexact), ' in
',num2str(funccount),
' function evaluations') disp('
') disp('Now compare with n step of composite
simpson rule') i110 nvec2.(i) .
3gtgt adaptivesimpson f1 Inline function
f1(x) sin(x).2 a1 b4 Integration using
adaptive simpson gives error-1.0503e-013 in 249
function evaluations Now compare with n step of
composite simpson rule n2 Error in composite
simpson rule -0.12324 n4 Error in composite
simpson rule -0.00075995 n8 Error in composite
simpson rule -3.7687e-005 n16 Error in
composite simpson rule -2.2363e-006 n32 Error
in composite simpson rule -1.3801e-007 n64
Error in composite simpson rule
-8.5986e-009 n128 Error in composite simpson
rule -5.3699e-010 n256 Error in composite
simpson rule -3.3555e-011 n512 Error in
composite simpson rule -2.0945e-012 n1024 Error
in composite simpson rule -1.299e-013 The same
function with composite trapezoid rule a1
b4 n2 Error in composite trapezod rule
0.017886 n4 Error in composite trapezod rule
0.0039016 n8 Error in composite trapezod rule
0.00094713 n16 Error in composite trapezod rule
0.0002351 n32 Error in composite trapezod rule
5.8673e-005 n64 Error in composite trapezod
rule 1.4662e-005 n128 Error in composite
trapezod rule 3.665e-006 n256 Error in
composite trapezod rule 9.1623e-007 n512 Error
in composite trapezod rule 2.2906e-007 n1024
Error in composite trapezod rule
5.7264e-008 n2048 Error in composite trapezod
rule 1.4316e-008 n4096 Error in composite
trapezod rule 3.579e-009
4romberg1.m
Romberg integration with number of function
evaluations count funccount Computes
approximation to definite integral Inputs
Matlab inline function specifying integrand f,
a,b integration interval, nnumber of rows
Output r(n,n) funccount function r1
funccount romberg(f,a,b,n) h(b-a)./(2.(0n-1))
r(1,1)(b-a)(f(a)f(b))/2 funccount2 for
j2n subtotal 0 for i12(j-2)
subtotal subtotal f(a(2i-1)h(j))
funccount funccount1 end r(j,1)
r(j-1,1)/2h(j)subtotal for k2j r(j,k)
(4(k-1)r(j,k-1)-r(j-1,k-1))/(4(k-1)-1)
end end r1r(n,n)
5rombergtest.m
test of romberg integration vs. adaptive
Simpson's rule and comparison with composite
Trapezoid and Composite Simpson's tol10(-10)
desired tolerance a1 b4 f1inline('sin(x).2
') function f(x) disp('a',num2str(a),'
b',num2str(b)) display limits of
integration intexactquadl(f1,a,b,10(-16))
calculation of "exact" value of
integral fadaptive funccountquad(f1,a,b,tol)
disp('Integration using adaptive simpson gives
error',num2str(fadaptive-intexact), ' in
',num2str(funccount), ' function
evaluations') disp('Now compare with Romberg
integration') i18 nvec1.(i) for
i1length(nvec) intromber funccountromberg1(f1
,a,b,nvec(i)) disp(' Error in Romberg
integration ',num2str(intromber-intexact), ' in
',num2str(funccount), ' function evaluations')
end
6gtgt rombergtest f1 Inline function
f1(x) sin(x).2 a1 b4 Integration using
adaptive simpson gives error-1.0525e-013 in 249
function evaluations Now compare with Romberg
integration Error in Romberg integration
0.44125 in 2 function evaluations Error in
Romberg integration -0.12324 in 3 function
evaluations Error in Romberg integration
0.0074051 in 5 function evaluations Error in
Romberg integration -0.00010691 in 9 function
evaluations Error in Romberg integration
3.8208e-007 in 17 function evaluations Error in
Romberg integration -3.4053e-010 in 33 function
evaluations Error in Romberg integration
7.5717e-014 in 65 function evaluations Error in
Romberg integration 2.2204e-016 in 129 function
evaluations Now compare with n step of
composite simpson rule n2 Error in composite
simpson rule -0.12324 n4 Error in composite
simpson rule -0.00075995 n8 Error in composite
simpson rule -3.7687e-005 n16 Error in
composite simpson rule -2.2363e-006 n32 Error
in composite simpson rule -1.3801e-007 n64
Error in composite simpson rule
-8.5986e-009 n128 Error in composite simpson
rule -5.3699e-010 n256 Error in composite
simpson rule -3.3555e-011 n512 Error in
composite simpson rule -2.0948e-012 n1024 Error
in composite simpson rule -1.3012e-013
7rombergtest2.m
rombergtest2.m test of romberg integration vs.
adaptive Simpson's rule and comparison with
composite Trapezoid
and Composite Simpson's tol10(-10)
desired tolerance a1 b15 f1inline('sin(x).2
') function f(x) disp('a',num2str(a),'
b',num2str(b)) display limits of
integration intexactquadl(f1,a,b,10(-16))
calculation of "exact" value of
integral fadaptive funccountquad(f1,a,b,tol)
disp('Integration using adaptive simpson gives
error',num2str(fadaptive-intexact), ' in
',num2str(funccount), ' function
evaluations') disp(' ') disp('Now compare
with Romberg integration') disp(' ')
8gtgt rombergtest2 f1 Inline function
f1(x) sin(x).2 a1 b15 Integration using
adaptive simpson gives error-1.4833e-013 in 1233
function evaluations Now compare with Romberg
integration Error in Romberg integration
0.4423 in 2 function evaluations Error in
Romberg integration 4.3003 in 3 function
evaluations Error in Romberg integration 4.1559
in 5 function evaluations Error in Romberg
integration -2.6801 in 9 function evaluations
Error in Romberg integration 0.23911 in 17
function evaluations Error in Romberg
integration -0.004795 in 33 function
evaluations Error in Romberg integration
2.3442e-005 in 65 function evaluations Error in
Romberg integration -2.8473e-008 in 129 function
evaluations Error in Romberg integration
8.6331e-012 in 257 function evaluations Error
in Romberg integration 0 in 513 function
evaluations Error in Romberg integration
-8.8818e-016 in 1025 function evaluations Error
in Romberg integration -7.9936e-015 in 2049
function evaluations Now compare with n step of
composite simpson rule n2 Error in composite
simpson rule 4.3003 n4 Error in composite
simpson rule 4.165 n8 Error in composite
simpson rule -2.1522 n16 Error in composite
simpson rule 0.037939 n32 Error in composite
simpson rule 0.0016978 n64 Error in composite
simpson rule 9.8788e-005 n128 Error in
composite simpson rule 6.0685e-006 n256 Error
in composite simpson rule 3.7766e-007 n512
Error in composite simpson rule
2.3579e-008 n1024 Error in composite simpson
rule 1.4733e-009
9gaussianadaptive.m
gaussianadaptive.m test of gaussian adaptive
integration vs. adaptive Simpson's rule and
Romberg's integration tol10(-8) desired
tolerance a1 b4 f1inline('sin(x).2')
function f(x) disp('a',num2str(a),'
b',num2str(b)) display limits of
integration intexactquadl(f1,a,b,10(-16))
calculation of "exact" value of
integral fadaptive funccountquadl(f1,a,b,tol)
disp('Integration using adaptive Gaussian
quadrature gives error',num2str(fadaptive-intexac
t), ' in ',num2str(funccount), ' function
evaluations') disp(' ') fadaptive
funccountquad(f1,a,b,10(-10)) disp('Integrat
ion using adaptive simpson gives
error',num2str(fadaptive-intexact), ' in
',num2str(funccount), ' function
evaluations') disp(' ') disp('Now compare
with Romberg integration')
10gtgt gaussianadaptive f1 Inline function
f1(x) sin(x).2 a1 b4 Integration using
adaptive Gaussian quadrature gives
error2.1849e-013 in 48 function evaluations
Integration using adaptive simpson gives
error-1.0525e-013 in 249 function evaluations
Now compare with Romberg integration Error
in Romberg integration 0.44125 in 2 function
evaluations Error in Romberg integration
-0.12324 in 3 function evaluations Error in
Romberg integration 0.0074051 in 5 function
evaluations Error in Romberg integration
-0.00010691 in 9 function evaluations Error in
Romberg integration 3.8208e-007 in 17 function
evaluations Error in Romberg integration
-3.4053e-010 in 33 function evaluations Error
in Romberg integration 7.5717e-014 in 65
function evaluations Error in Romberg
integration 2.2204e-016 in 129 function
evaluations Error in Romberg integration
-2.2204e-016 in 257 function evaluations Error
in Romberg integration -4.4409e-016 in 513
function evaluations Error in Romberg
integration 4.4409e-016 in 1025 function
evaluations Error in Romberg integration 0 in
2049 function evaluations
11gtgt adaptivesimpson f1 Inline function
f1(x) sin(x).2 a1 b4 Integration using
adaptive simpson gives error-1.0503e-013 in 249
function evaluations Now compare with n step of
composite simpson rule n2 Error in composite
simpson rule -0.12324 n4 Error in composite
simpson rule -0.00075995 n8 Error in composite
simpson rule -3.7687e-005 n16 Error in
composite simpson rule -2.2363e-006 n32 Error
in composite simpson rule -1.3801e-007 n64
Error in composite simpson rule
-8.5986e-009 n128 Error in composite simpson
rule -5.3699e-010 n256 Error in composite
simpson rule -3.3555e-011 n512 Error in
composite simpson rule -2.0945e-012 n1024 Error
in composite simpson rule -1.299e-013 The same
function with composite trapezoid rule a1
b4 n2 Error in composite trapezod rule
0.017886 n4 Error in composite trapezod rule
0.0039016 n8 Error in composite trapezod rule
0.00094713 n16 Error in composite trapezod rule
0.0002351 n32 Error in composite trapezod rule
5.8673e-005 n64 Error in composite trapezod
rule 1.4662e-005 n128 Error in composite
trapezod rule 3.665e-006 n256 Error in
composite trapezod rule 9.1623e-007 n512 Error
in composite trapezod rule 2.2906e-007 n1024
Error in composite trapezod rule
5.7264e-008 n2048 Error in composite trapezod
rule 1.4316e-008 n4096 Error in composite
trapezod rule 3.579e-009