8.integration by Numerical Method

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Thermal Mechanical Department 8.

Numerical Integration (MATLAB)

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)

Now let’s create a uniformly spaced line of ten panels:


>> x = linspace(0,2,10);
>> y = x.^2;
>> a = trapz(x,y)
a=
2.6831
The relative error is:
>> error=100*abs((8/3–a)/(8/3))
error =
0.6173
Now let’s repeat the process with 20 uniformly spaced panels to see how we
can reduce the error:
>> x = linspace(0,2,20);
>> y = x.^2;
>> a = trapz(x,y)
a =
2.6704
>> error=100*abs((8/3–a)/(8/3))
error =
0.1385

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)

8.2 Simpson’s Rule


MATLAB can be used to perform Simpson’s integration by calling the
quad(’function’,a,b)
Example: Use the MATLAB function quad to approximate the value of the
integral:

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

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)(2/sqrt(x));
area=(f(a)+f(b));
for i=1:2:n-1
x=a+i*h;
area=area+4*f(x);
end
for i=2:2:n-2
x=a+i*h;
area=area+2*f(x);
end
Simp13=h*area/3

The results

>> Simp13 =

2.9291

Instru.:Ruslan.S.Abdul-Rahman Page 82
Thermal Mechanical Department 8.Numerical Integration (MATLAB)

Example: Use MATLAB functions to perform the integration numerically. In


addition, develop a plot of the numerically and computed distances along with
velocity with analytical solution on the same graph. The distance, y(t), of an
object based on its velocity, v(t),is defined

When velocity, v(t) is :

and m= mass (70kg) , c = drag coefficient (12.5) , g = gravitational (9.81m2/s)


Solution
We can first use some unequally-spaced times and velocities. We can then
round these velocities. So that they are more like measured values;
that is, they are not exact (numerical solution )
format short g
t=[0 1 2 3 4.3 7 12 16 20];
g=9.81;m=70;c=12.5;
v=round(g*m/c*(1-exp(-c/m*t)));
y=trapz(t,v) % numerical solution

The results
y=
789.6

If we desire the cumulative distance travelled at each time, cumtrapz can be


employed to compute,
yc=cumtrapz(t,v)
The results
yc =
0 4.5 17 36.5 70.3 162.1 379.6 579.6 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

You might also like