Title: Lecture 26: Numerical Integration
1Lecture 26 Numerical Integration
Newton-Cotes Formulas
Trapezoid rule
Simpson's rule
Simpson's 3/8 rule
Booles rule
2trapezoid1.m
function f1trapezoid1(func1,a,b,n) Integration
function func1 using composite
trapezoid rule at n points between a and
b h(b-a)/n f1func1(a)/2 for i1n-1
f1f1func1(aih) end f1f1func1(b)/2 f1hf1
trapezoid1test.m
3examples of use of function f1trapezoid1(func1,a
,b,n) Integration function func1
using composite trapezoid rule at n points
between a and b a1 b5 f1inline('exp(-x)') d
isp('a',num2str(a),' b',num2str(b))
display limits of integration
intexact-exp(-b)exp(-a) i110 nvec2.(i)
for i1length(nvec) f1inttrapezoid1(f1
,a,b,nvec(i)) disp('n',num2str(nvec(i)),'
Error in composite trapezod rule
',num2str(f1int-intexact))
display value of function f(x) end
4gtgt trapezoid1test f1 Inline function
f1(x) exp(-x) a1 b5 n2 Error in composite
trapezod rule 0.11305 n4 Error in composite
trapezod rule 0.029605 n8 Error in composite
trapezod rule 0.0074926 n16 Error in composite
trapezod rule 0.001879 n32 Error in composite
trapezod rule 0.00047011 n64 Error in composite
trapezod rule 0.00011755 n128 Error in
composite trapezod rule 2.9389e-005 n256 Error
in composite trapezod rule 7.3474e-006 n512
Error in composite trapezod rule
1.8369e-006 n1024 Error in composite trapezod
rule 4.5922e-007
5a0 b2pi f1inline('exp(sin(x)2)') disp('a'
,num2str(a),' b',num2str(b)) display limits
of integration intexacttrapezoid1(f1,a,b,200)
i15 nvec2.(i) for i1length(nvec)
f1inttrapezoid1(f1,a,b,nvec(i))
disp('n',num2str(nvec(i)),' Error in composite
trapezod rule ',num2str(f1int-intexact))
display value of function f(x)
end
6f1 Inline function f1(x)
exp(sin(x)2) a0 b6.2832 n2 Error in
composite trapezod rule -4.7337 n4 Error in
composite trapezod rule 0.66447 n8 Error in
composite trapezod rule 0.0034145 n16 Error in
composite trapezod rule 7.8954e-009 n32 Error
in composite trapezod rule -3.5527e-015 f1
Inline function f1(x)
exp(sin(x)2) a0 b0.7854 n2 Error in
composite trapezod rule 0.021343 n4 Error in
composite trapezod rule 0.005305 n8 Error in
composite trapezod rule 0.0013228 n16 Error in
composite trapezod rule 0.00032898 n32 Error in
composite trapezod rule 8.0649e-005
7a0 bpi/4 f1inline('exp(sin(x)2)') disp('a'
,num2str(a),' b',num2str(b)) display limits
of integration intexacttrapezoid1(f1,a,b,200)
i15 nvec2.(i) for i1length(nvec)
f1inttrapezoid1(f1,a,b,nvec(i))
disp('n',num2str(nvec(i)),' Error in composite
trapezod rule ',num2str(f1int-intexact))
display value
of function f(x) end
8simpson1.m
function f1simpson1(func1,a,b,n) Integration
function func1 using composite
simpson rule at n points between a and
b h(b-a)/n f1func1(a) for i1(n/2-1)
f1f14func1(a(2i-1)h)2func1(a(2i)h) end
f1f14func1(a(n-1)h)func1(b) f1(h/3)f1
simpson1test.m
9examples of use of function f1simpson1(func1,a,b
,n) Integration function func1
using composite Simpson's rule at n points
between a and b n must be even a1 b5 f1inl
ine('exp(-x)') disp('a',num2str(a),'
b',num2str(b)) display limits of
integration intexact-exp(-b)exp(-a) i110
nvec2.(i) for i1length(nvec)
f1intsimpson1(f1,a,b,nvec(i))
disp('n',num2str(nvec(i)),' Error in composite
trapezod rule ',num2str(f1int-intexact))
display value of function f(x)
end
10gtgt simpson1test f1(x) exp(-x) a1 b5 n2
Error in composite trapezod rule 0.021369 n4
Error in composite trapezod rule 0.0017902 n8
Error in composite trapezod rule 0.00012176 n16
Error in composite trapezod rule
7.7793e-006 n32 Error in composite trapezod
rule 4.8892e-007 n64 Error in composite
trapezod rule 3.06e-008 n128 Error in composite
trapezod rule 1.9132e-009 n256 Error in
composite trapezod rule 1.1958e-010 n512 Error
in composite trapezod rule 7.4751e-012 n1024
Error in composite trapezod rule
4.6674e-013 The same function with composite
trapezoid rule a1 b5 n2 Error in composite
trapezod rule 0.11305 n4 Error in composite
trapezod rule 0.029605 n8 Error in composite
trapezod rule 0.0074926 n16 Error in composite
trapezod rule 0.001879 n32 Error in composite
trapezod rule 0.00047011 n64 Error in composite
trapezod rule 0.00011755 n128 Error in
composite trapezod rule 2.9389e-005 n256 Error
in composite trapezod rule 7.3474e-006 n512
Error in composite trapezod rule
1.8369e-006 n1024 Error in composite trapezod
rule 4.5922e-007
11Exercise
Compute integral of function e(-x2) between a0
and b10 using composite trapezoid and composite
Simpsons rules for n2,4,8,, 1024. Compare with
exact answer sqrt(pi)/2 Use simpson1test.m as
example.
12gtgt inclass26 f1 Inline function
f1(x) exp(-x2) a0 b10 n2 Error in
composite trapezod rule 0.78044 n4 Error in
composite trapezod rule -0.046459 n8 Error in
composite trapezod rule -0.1186 n16 Error in
composite trapezod rule -0.0010671 n32 Error in
composite trapezod rule -6.2875e-012 n64 Error
in composite trapezod rule 0 n128 Error in
composite trapezod rule 2.2204e-016 n256 Error
in composite trapezod rule 5.5511e-016 n512
Error in composite trapezod rule 0 n1024 Error
in composite trapezod rule -1.3323e-015 The same
function with composite trapezoid rule a0
b10 n2 Error in composite trapezod rule
1.6138 n4 Error in composite trapezod rule
0.3686 n8 Error in composite trapezod rule
0.0032014 n16 Error in composite trapezod rule
1.8863e-011 n32 Error in composite trapezod
rule 0 n64 Error in composite trapezod rule
-1.1102e-016 n128 Error in composite trapezod
rule -1.1102e-016 n256 Error in composite
trapezod rule -4.4409e-016 n512 Error in
composite trapezod rule -6.6613e-016 n1024
Error in composite trapezod rule -4.4409e-016