Lecture 26: Numerical Integration Trapezoid rule Simpson's rule Simpson's 3/8 rule Boole’s rule Newton-Cotes Formulas
trapezoid1.m trapezoid1test.m function f1=trapezoid1(func1,a,b,n) %Integration function func1 %using composite trapezoid rule at n points between a and b h=(b-a)/n; f1=func1(a)/2; for i=1:n-1 f1=f1+func1(a+i*h); end f1=f1+func1(b)/2; f1=h*f1;
%examples of use of function f1=trapezoid1(func1,a,b,n) %Integration function func1 %using composite trapezoid rule at n points between a and b a=1; b=5; f1=inline('exp(-x)') disp(['a=',num2str(a),' b=',num2str(b)]) %display limits of integration intexact=-exp(-b)+exp(-a); i=1:10; nvec=2.^(i); for i=1:length(nvec) f1int=trapezoid1(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
>> trapezoid1test f1 = Inline function: f1(x) = exp(-x) a=1 b=5 n=2 Error in composite trapezod rule = n=4 Error in composite trapezod rule = n=8 Error in composite trapezod rule = n=16 Error in composite trapezod rule = n=32 Error in composite trapezod rule = n=64 Error in composite trapezod rule = n=128 Error in composite trapezod rule =2.9389e-005 n=256 Error in composite trapezod rule =7.3474e-006 n=512 Error in composite trapezod rule =1.8369e-006 n=1024 Error in composite trapezod rule =4.5922e-007
a=0; b=2*pi; f1=inline('exp(sin(x)^2)') disp(['a=',num2str(a),' b=',num2str(b)]) %display limits of integration intexact=trapezoid1(f1,a,b,200); i=1:5; nvec=2.^(i); for i=1:length(nvec) f1int=trapezoid1(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
f1 = Inline function: f1(x) = exp(sin(x)^2) a=0 b= n=2 Error in composite trapezod rule = n=4 Error in composite trapezod rule = n=8 Error in composite trapezod rule = n=16 Error in composite trapezod rule =7.8954e-009 n=32 Error in composite trapezod rule = e-015 f1 = Inline function: f1(x) = exp(sin(x)^2) a=0 b= n=2 Error in composite trapezod rule = n=4 Error in composite trapezod rule = n=8 Error in composite trapezod rule = n=16 Error in composite trapezod rule = n=32 Error in composite trapezod rule =8.0649e-005
a=0; b=pi/4; f1=inline('exp(sin(x)^2)') disp(['a=',num2str(a),' b=',num2str(b)]) %display limits of integration intexact=trapezoid1(f1,a,b,200); i=1:5; nvec=2.^(i); for i=1:length(nvec) f1int=trapezoid1(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
simpson1.m simpson1test.m function f1=simpson1(func1,a,b,n) %Integration function func1 %using composite simpson rule at n points between a and b h=(b-a)/n; f1=func1(a); for i=1:(n/2-1) f1=f1+4*func1(a+(2*i-1)*h)+2*func1(a+(2*i)*h); end f1=f1+4*func1(a+(n-1)*h)+func1(b); f1=(h/3)*f1;
%examples of use of function f1=simpson1(func1,a,b,n) %Integration function func1 %using composite Simpson's rule at n points between a and b; n must be even a=1; b=5; f1=inline('exp(-x)') disp(['a=',num2str(a),' b=',num2str(b)]) %display limits of integration intexact=-exp(-b)+exp(-a); i=1:10; nvec=2.^(i); for i=1:length(nvec) f1int=simpson1(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 …
>> simpson1test f1(x) = exp(-x) a=1 b=5 n=2 Error in composite trapezod rule = n=4 Error in composite trapezod rule = n=8 Error in composite trapezod rule = n=16 Error in composite trapezod rule =7.7793e-006 n=32 Error in composite trapezod rule =4.8892e-007 n=64 Error in composite trapezod rule =3.06e-008 n=128 Error in composite trapezod rule =1.9132e-009 n=256 Error in composite trapezod rule =1.1958e-010 n=512 Error in composite trapezod rule =7.4751e-012 n=1024 Error in composite trapezod rule =4.6674e-013 The same function with composite trapezoid rule: a=1 b=5 n=2 Error in composite trapezod rule = n=4 Error in composite trapezod rule = n=8 Error in composite trapezod rule = n=16 Error in composite trapezod rule = n=32 Error in composite trapezod rule = n=64 Error in composite trapezod rule = n=128 Error in composite trapezod rule =2.9389e-005 n=256 Error in composite trapezod rule =7.3474e-006 n=512 Error in composite trapezod rule =1.8369e-006 n=1024 Error in composite trapezod rule =4.5922e-007
Exercise Compute integral of function e^(-x^2) between a=0 and b=10 using composite trapezoid and composite Simpson’s rules for n=2,4,8,…, Compare with exact answer = sqrt(pi)/2 Use simpson1test.m as example.
>> inclass26 f1 = Inline function: f1(x) = exp(-x^2) a=0 b=10 n=2 Error in composite trapezod rule = n=4 Error in composite trapezod rule = n=8 Error in composite trapezod rule = n=16 Error in composite trapezod rule = n=32 Error in composite trapezod rule = e-012 n=64 Error in composite trapezod rule =0 n=128 Error in composite trapezod rule =2.2204e-016 n=256 Error in composite trapezod rule =5.5511e-016 n=512 Error in composite trapezod rule =0 n=1024 Error in composite trapezod rule = e-015 The same function with composite trapezoid rule: a=0 b=10 n=2 Error in composite trapezod rule = n=4 Error in composite trapezod rule = n=8 Error in composite trapezod rule = n=16 Error in composite trapezod rule =1.8863e-011 n=32 Error in composite trapezod rule =0 n=64 Error in composite trapezod rule = e-016 n=128 Error in composite trapezod rule = e-016 n=256 Error in composite trapezod rule = e-016 n=512 Error in composite trapezod rule = e-016 n=1024 Error in composite trapezod rule = e-016