8.integration by Numerical Method
8.integration by Numerical Method
8. Numerical Integration
The trapezoidal and Simpson’s rules are special cases of the Newton-Cote
rules which use higher degree functions for numerical integration.
8.1 The Trapezoidal Rule
MATLAB can be used to perform trapezoidal integration by calling the
trapz(x, y) function
Example: Use the MATLAB function trapz(x,y) to approximate the value of
the integral:
Using 10 and 20 evenly spaced panels, and compute the relative error in each
case.
Solution:
First let’s do the problem analytically to get the exact answer:
We can also find the analytical value with MATLAB’s int(f,a,b) function
where f is a symbolic expression, and a and b are the lower and upper limits of
integration respectively
>> syms x
Area=int(x^2,0,2)
Area =
8/3
Instru.:Ruslan.S.Abdul-Rahman Page 78
Thermal Mechanical Department 8.Numerical Integration (MATLAB)
By doubling the number of panels, we have reduced the error by nearly a factor
of 5.
Instru.:Ruslan.S.Abdul-Rahman Page 79
Thermal Mechanical Department 8.Numerical Integration (MATLAB)
Example: Use for loop program to approximate the value of the integral :
Using 10 spaced panels, and compute the relative error in this case.
soluation
a=input('Enter the value of intial value limit a=');
b=input('Enter the value of final value limit b=');
n=input('Enter the number of strips n=');
h=(b-a)/n;
f=@(x)(x^2);
area=(f(a)+f(b));
for i=1:n-1
x=a+i*h;
area=area+2*f(x);
end
TrapEq=h*area/2
syms x
area2=int(x^2,0,2)
error = 100* abs((area2-TrapEq)/(area2))
The results
>>TrapEq =
2.68
>>area2 =
8/3
>>error =
1/2
Instru.:Ruslan.S.Abdul-Rahman Page 80
Thermal Mechanical Department 8.Numerical Integration (MATLAB)
Solution:
>> area=quad('x.^2',0,2)
area =
2.6667
Example: Use MATLAB program with for loop to evaluated the following
integral by using Simpson’s Rule method (n=5)
solution
a=input('Enter the value of intial value limit a=');
b=input('Enter the value of final value limit b=');
n=input('Enter the number of strips n=');
h=(b-a)/n;
f=@(x)(x^2);
area=(f(a)+f(b));
for i=1:n-1
x=a+i*h;
area=area+3*f(x);
end
simp38=(3/8)*h*area
The results
>>simp38 =
2.76
Instru.:Ruslan.S.Abdul-Rahman Page 81
Thermal Mechanical Department 8.Numerical Integration (MATLAB)
Example: Use MATLAB program with for loop to evaluated the following
integral by using Simpson’s Rule method (n=4)
∫
√
Solution
The results
>> Simp13 =
2.9291
Instru.:Ruslan.S.Abdul-Rahman Page 82
Thermal Mechanical Department 8.Numerical Integration (MATLAB)
The results
y=
789.6
Instru.:Ruslan.S.Abdul-Rahman Page 83
Thermal Mechanical Department 8.Numerical Integration (MATLAB)
A graph of the numerical and analytical solutions along with both the exact and
rounded velocities are generated with the following commands,
% analytical solution
ya=g*m*t/c -g*m^2/c^2*(1-exp(-c/m*t));
plot(t,ya,t,yc,'o')
title('Distance versus time')
xlabel('t (s)'),ylabel('x (m)')
legend('analytical','numerical')
Finally, the quad function can be used to evaluate the integral with adaptive quadrature
va=@(t) g*m/c*(1-exp(-c/m*t));
yq=quad(va,0,20)
The results
yq =
799.73
Instru.:Ruslan.S.Abdul-Rahman Page 84