Beruflich Dokumente
Kultur Dokumente
Exercise 1
Toy Rocket Model Analysis
Date: 2020-04-11
Course: MT2558 Computational Engineering I
Author: Rahul SHARMA
P. Number: 911211-2959
Summary
The nature and natural phenomena are mathematically expressed/modelled in the form of
differential equations. As important as it is to deal with differential equations, it can be
extremely challenging to solve them analytically or there might not even be a way to solve a
differential equation. So, to deal with such a challenge, numerical methods are used.
Numerical methods are iterative methods that are employed to solve a certain mathematical
problem, a differential equation in our case. Since computers are great at iterative tasks, it
can be relatively easy to solve differential equations using these numerical methods.
The Exercise is focused on modelling a given toy rocket propelling upwards under the
influence of gravity. The model is used to find the velocity and displacement of the toy rocket
as functions of time. The velocity and displacements are then re-calculated using various
numerical methods and their reliability is studied.
1
BTH Computational Engineering I
Introduction
The aim of this exercise is to calculate the velocity and displacement of a given toy rocket that
is flying vertically upwards and is out of propellant when its motion is studied. Its initial velocity
v0 is upwards at time t0 and displacement s0, where
v0 = 60 m/s
t0 = 0 s
s0 = 0 m
Using the given initial conditions, it is required to calculate the velocity and displacement by
using the following methods with three different step sizes of 8, 16 and 32:
a. Analytical Method
b. Euler Method
c. Mid-Point Method
d. Trapezoidal Method
e. Richardson Extrapolation
f. Runge-Kutta Method
g. Milne Method
h. MATLAB ode45 function
The result of each method should be compared to the Analytical result and the deviation
discussed.
2
BTH Computational Engineering I
1. Analytical Method
The toy rocket and is modelled using Newton’s Second Law, and the following differential
equation is obtained
𝑑𝑣 𝑘
= −𝑔 − 𝑣 2 − (1)
𝑑𝑡 𝑚
Solving the equation analytically gives, velocity as a function of time given by the equation
1 𝑘 𝑘
𝑣(𝑡) = tan (arctan (𝑣0 √ ) − (𝑔√ ) 𝑡) − (2)
𝑘 𝑚𝑔 𝑚𝑔
√
𝑚𝑔
Using the velocity – time relation the displacement – time relation is calculated and given by
the equation
𝑘 𝑘
|cos (arctan (𝑣0 √𝑚𝑔) − (𝑔√𝑚𝑔) 𝑡) |
1
𝑠(𝑡) = ln − (3)
𝑘 𝑘
√ |cos (arctan (𝑣0 √ )) |
𝑚𝑔 𝑚𝑔
( )
Using the equations (2) and (3), the velocity and displacement are plotted against time in
MATLAB (Appendix 1), and result attained is given in Figure 1.
3
BTH Computational Engineering I
2. Euler Method
In Euler Method, we use the equation shown below iteratively to calculate velocity against
time for different steps as shown in Figure 2.
𝑑𝑣
𝑣𝑛+1 = 𝑣𝑛 + (𝑡 , 𝑣 )
𝑑𝑡 𝑛 𝑛
In Euler Method, we use the equation shown below iteratively to calculate displacement
against time for different steps as shown in Figure 4.
𝑑𝑠
𝑠𝑛+1 = 𝑠𝑛 + (𝑡𝑛 , 𝑠𝑛 )
𝑑𝑡
Figure 2. Graph of Velocity vs Time for Toy Rocket plotted with Euler Method
It can be clearly seen from Figure 2, that as we increase the resolution, the result starts to
converge closer to the analytical graph, and hence convergence is observed for velocity
iteration with Euler’s Method.
Whereas, from Figure 3 of error values, it can be seen that the error reaches a value of about
1300% which is unacceptable for numerical methods. The error keeps varying and it is
uncertain whether the error decreases or not with time. So, the velocity iteration with Euler’s
Method seems unstable.
4
BTH Computational Engineering I
Figure 4 shows clear convergence in the values of Displacement as the number of iterative
steps are increased, hence it does converge.
But, error in the values of displacement shown in Figure 5 depict uncertainty in reduction of
errors, hence unstable.
5
BTH Computational Engineering I
6
BTH Computational Engineering I
3. Mid-Point Method
Figures 6 and 7 show a clear convergence with increase in number of iterative steps, but
stability is uncertain as the error keep varying.
7
BTH Computational Engineering I
Figure 8 and 9 show a clear convergence for more refined iteration, but stability can only be
observed clearly for higher resolution of N=32 steps.
8
BTH Computational Engineering I
4. Trapezoidal Method
Figure 10 and 11 show a good convergence but stability is observed only for higher resolution.
9
BTH Computational Engineering I
Figure 12 and 13 show a clear convergence for more refined iteration, but stability can only
be observed clearly for higher resolution of N=32 steps. Also the errors are low compared to
previous methods.
10
BTH Computational Engineering I
5. Richardson Extrapolation
Convergence for Richardson is observed good here for both displacement and velocity
through figures 14 to 17. But poor stability is observed for velocity as the errors are very high.
11
BTH Computational Engineering I
12
BTH Computational Engineering I
6. Runge-Kutta Method
Excellent convergence is observed even for lower iteration steps for both velocity and
displacement. But stability for displacement is very good because of the very low errors. Show
in figures 18 through 21.
13
BTH Computational Engineering I
14
BTH Computational Engineering I
7. Milne Method
Excellent convergence is observed even for low iteration steps for both velocity and
displacement. But stability for displacement is very good because of the very low errors, and
for velocity the stability is acceptable only for higher resolution.
15
BTH Computational Engineering I
16
BTH Computational Engineering I
MATLAB ode45 function shows excellent agreement with the analytical solution with an error
of less than 0.05% for velocity 0.003% for displacement throughout. Convergence can not be
commented on here, because there is just one resolution being dealt with, but stability is very
good as can be seen from the sharply reducing errors in both velocity and displacement graphs
shown in figure 27 and 29.
17
BTH Computational Engineering I
18
BTH Computational Engineering I
Conclusion
The exercise highlighted the importance of numerical methods, and their usefulness in
calculating the solution of differential equations. Also it can be seen that not all methods are
suitable for a specific case, and it must be decide with the convergence and stability of results
as to which method would be more suitable for a given problem.
As can be seen from the results, it is mostly observed that the numerical methods usually have
a good convergence as the number of steps are increased, but for the toy rocket example
Runge-Kutta turned out to give the preferable outcome with least error and most
convergence.
19
BTH Computational Engineering I
Appendix 1
clc
clear all
%% ---------------------------EULER'S METHOD---------------------------- %%
figure(1)
subplot(2,2,1)
plot(t,v)
xlabel('t, Time in seconds')
ylabel('v(t), Velocity in meters/second')
title('Plot of Velocity vs Time for toy rocket compared with Euler Method')
hold on
subplot(2,2,2)
plot(t,s)
xlabel('t, Time in seconds')
ylabel('s(t), Displacement in meters')
title('Plot of Displacement versus time for toy rocket compared with Euler
Method')
hold on
for n=1:3
for i=1:N
%EULER METHOD for Velocity
fEv(i,n)=-g-((k/m)*vE(i,n)^2); %differential equation
vE(i+1,n)=vE(i,n)+fEv(i,n)*h; %Euler's formula
20
BTH Computational Engineering I
tE(i+1,n)=i*h;
%Error in Velocity
vActual(i+1,n)=v(t==tE(i+1,n));
vEError(i+1,n)=abs((vActual(i+1,n)-vE(i+1,n))*100/vActual(i+1,n));
%Error in Displacement
sActual(i+1,n)=s(t==tE(i+1,n));
sEError(i+1,n)=abs((sActual(i+1,n)-sE(i+1,n))*100/sActual(i+1,n));
end
subplot(2,2,1)
plot(tE(:,n),vE(:,n),'x')
hold on
subplot(2,2,2)
plot(tE(:,n),sE(:,n),'+')
hold on
subplot(2,2,3)
plot(tE(:,n),vEError(:,n))
hold on
subplot(2,2,4)
plot(tE(:,n),sEError(:,n))
hold on
end
subplot(2,2,1)
legend('Analytical Velocity','8 Steps','16 Steps','32 Steps');
subplot(2,2,2)
legend('Analytical Displacement','8 Steps','16 Steps','32 Steps');
subplot(2,2,3)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Velocity')
title('Percentage deviation from values of Analytical Velocity vs Time')
legend('8 Steps','16 Steps','32 Steps')
subplot(2,2,4)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Displacement')
title('Percentage deviation from values of Analytical Displacement vs
Time')
legend('8 Steps','16 Steps','32 Steps')
hold off
%% ---------------------------MIDPOINT METHOD--------------------------- %%
figure(2)
subplot(2,2,1)
21
BTH Computational Engineering I
plot(t,v)
xlabel('t, Time in seconds')
ylabel('v(t), Velocity in meters/second')
title('Plot of Velocity vs Time for toy rocket compared with Midoint
Method')
hold on
subplot(2,2,2)
plot(t,s)
xlabel('t, Time in seconds')
ylabel('s(t), Displacement in meters')
title('Plot of Displacement versus time for toy rocket compared with
Midpoint Method')
hold on
for n=1:3
N=2^(n+2);
h=3/N;
vMi(1,n)=60;
vActual(1,n)=60;
sMi(1,n)=0;
tMi(1,n)=0;
for i=1:N
tMi(i+1,n)=i*h;
%Error in Velocity
vActual(i+1,n)=v(t==tMi(i+1,n));
vMiError(i+1,n)=abs((vActual(i+1,n)-
vMi(i+1,n))*100/vActual(i+1,n));
%Error in Displacement
sActual(i+1,n)=s(t==tMi(i+1,n));
sMiError(i+1,n)=abs((sActual(i+1,n)-
sMi(i+1,n))*100/sActual(i+1,n));
end
subplot(2,2,1)
plot(tMi(:,n),vMi(:,n),'x')
hold on
subplot(2,2,2)
plot(tMi(:,n),sMi(:,n),'+')
hold on
22
BTH Computational Engineering I
subplot(2,2,3)
plot(tMi(:,n),vMiError(:,n))
hold on
subplot(2,2,4)
plot(tMi(:,n),sMiError(:,n))
hold on
end
subplot(2,2,1)
legend('Analytical Velocity','8 Steps','16 Steps','32 Steps');
subplot(2,2,2)
legend('Analytical Displacement','8 Steps','16 Steps','32 Steps');
subplot(2,2,3)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Velocity')
title('Percentage deviation from values of Analytical Velocity vs Time')
legend('8 Steps','16 Steps','32 Steps')
subplot(2,2,4)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Displacement')
title('Percentage deviation from values of Analytical Displacement vs
Time')
legend('8 Steps','16 Steps','32 Steps')
hold off
%% -------------------------TRAPEZOIDAL METHOD-------------------------- %%
figure(3)
subplot(2,2,1)
plot(t,v)
xlabel('t, Time in seconds')
ylabel('v(t), Velocity in meters/second')
title('Plot of Velocity vs Time for toy rocket compared with Trapezoidal
Method')
hold on
subplot(2,2,2)
plot(t,s)
xlabel('t, Time in seconds')
ylabel('s(t), Displacement in meters')
title('Plot of Displacement versus time for toy rocket compared with
Trapezoidal Method')
hold on
for n=1:3
for i=1:N
23
BTH Computational Engineering I
tT(i+1,n)=i*h;
%Error in Velocity
vActual(i+1,n)=v(t==tT(i+1,n));
vTError(i+1,n)=abs((vActual(i+1,n)-vT(i+1,n))*100/vActual(i+1,n));
%Error in Displacement
sActual(i+1,n)=s(t==tT(i+1,n));
sTError(i+1,n)=abs((sActual(i+1,n)-sT(i+1,n))*100/sActual(i+1,n));
end
subplot(2,2,1)
plot(tT(:,n),vT(:,n),'x')
hold on
subplot(2,2,2)
plot(tT(:,n),sT(:,n),'+')
hold on
subplot(2,2,3)
plot(tT(:,n),vTError(:,n))
hold on
subplot(2,2,4)
plot(tT(:,n),sTError(:,n))
hold on
end
subplot(2,2,1)
legend('Analytical Velocity','8 Steps','16 Steps','32 Steps');
subplot(2,2,2)
legend('Analytical Displacement','8 Steps','16 Steps','32 Steps');
subplot(2,2,3)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Velocity')
24
BTH Computational Engineering I
subplot(2,2,4)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Displacement')
title('Percentage deviation from values of Analytical Displacement vs
Time')
legend('8 Steps','16 Steps','32 Steps')
hold off
figure(4)
subplot(2,2,1)
plot(t,v)
xlabel('t, Time in seconds')
ylabel('v(t), Velocity in meters/second')
title('Plot of Velocity vs Time for toy rocket compared with Richardson
Extrapolation Method')
hold on
subplot(2,2,2)
plot(t,s)
xlabel('t, Time in seconds')
ylabel('s(t), Displacement in meters')
title('Plot of Displacement versus time for toy rocket compared with
Richardson Extrapolation Method')
hold on
for n=1:3
N=2^(n+2);
h=3/N;
vActual(1,n)=60;
vR1(1,n)=60;
vR2(1,n)=60;
vR3(1,n)=60;
vR(1,n)=60;
sR1(1,n)=0;
sR2(1,n)=0;
sR3(1,n)=0;
sR(1,n)=0;
tR(1,n)=0;
for i=1:N
%RICHARDSON EXTRAPOLATION for Velocity
%Step 1
fRv2(i,n)=-g-((k/m)*(vR3(i,n))^2);
vR2(i+1,n)=vR3(i,n)+fRv2(i,n)*0.5*h; %Step 1 output
25
BTH Computational Engineering I
%Step 2
fRv3(i,n)=-g-((k/m)*(vR2(i+1,n))^2);
vR3(i+1,n)=vR2(i+1,n)+fRv3(i,n)*0.5*h; %Step 2 output
%Step 1
fRs2(i,n)=(tan((atan(v(1)*r))-(g*r*(tR(i,n)+0.5*h))))/r;
sR2(i+1,n)=sR3(i,n)+fRs2(i,n)*0.5*h; %Step 1 output
tR3(i+1,n)=i*h;
%Step 2
fRs3(i,n)=(tan((atan(v(1)*r))-(g*r*(tR(i,n)+0.5*h))))/r;
sR3(i+1,n)=sR2(i+1,n)+fRs3(i,n)*0.5*h; %Step 2 output
%Error in Velocity
vActual(i+1,n)=v(t==tR(i+1,n));
vRError(i+1,n)=abs((vActual(i+1,n)-vR(i+1,n))*100/vActual(i+1,n));
%Error in Displacement
sActual(i+1,n)=s(t==tR(i+1,n));
sRError(i+1,n)=abs((sActual(i+1,n)-sR(i+1,n))*100/sActual(i+1,n));
end
subplot(2,2,1)
plot(tR(:,n),vR(:,n),'x')
hold on
subplot(2,2,2)
plot(tR(:,n),sR(:,n),'+')
hold on
subplot(2,2,3)
plot(tR(:,n),vRError(:,n))
hold on
subplot(2,2,4)
plot(tR(:,n),sRError(:,n))
hold on
end
subplot(2,2,1)
legend('Analytical Velocity','8 Steps','16 Steps','32 Steps');
26
BTH Computational Engineering I
subplot(2,2,2)
legend('Analytical Displacement','8 Steps','16 Steps','32 Steps');
subplot(2,2,3)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Velocity')
title('Percentage deviation from values of Analytical Velocity vs Time')
legend('8 Steps','16 Steps','32 Steps')
subplot(2,2,4)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Displacement')
title('Percentage deviation from values of Analytical Displacement vs
Time')
legend('8 Steps','16 Steps','32 Steps')
hold off
%% --------------------------RUNGE-KUTTA METHOD------------------------- %%
figure(5)
subplot(2,2,1)
plot(t,v)
xlabel('t, Time in seconds')
ylabel('v(t), Velocity in meters/second')
title('Plot of Velocity vs Time for toy rocket compared with Runge-Kutta
Method')
hold on
subplot(2,2,2)
plot(t,s)
xlabel('t, Time in seconds')
ylabel('s(t), Displacement in meters')
title('Plot of Displacement versus time for toy rocket compared with Runge-
Kutta Method')
hold on
for n=1:3
N=2^(n+2);
h=3/N; %step size
vK(1,n)=60;
vActual(1,n)=60;
sK(1,n)=0;
tK(1,n)=0;
for i=1:N
fKv2(i,n)=-g-((k/m)*(vK(i,n)+0.5*kv1(i,n))^2);
kv2(i,n)=fKv2(i,n)*h;
fKv3(i,n)=-g-((k/m)*(vK(i,n)+0.5*kv2(i,n))^2);
27
BTH Computational Engineering I
kv3(i,n)=fKv3(i,n)*h;
fKv4(i,n)=-g-((k/m)*(vK(i,n)+kv3(i,n))^2);
kv4(i,n)=fKv4(i,n)*h;
vK(i+1,n)=vK(i,n)+(kv1(i,n)+2*kv2(i,n)+2*kv3(i,n)+kv4(i,n))/6;
fKs1(i,n)=(tan((atan(v(1)*r))-(g*r*tK(i,n))))/r;
ks1(i,n)=fKs1(i,n)*h;
fKs2(i,n)=(tan((atan(v(1)*r))-(g*r*(tK(i,n)+0.5*h))))/r;
ks2(i,n)=fKs2(i,n)*h;
fKs3(i,n)=(tan((atan(v(1)*r))-(g*r*(tK(i,n)+0.5*h))))/r;
ks3(i,n)=fKs3(i,n)*h;
fKs4(i,n)=(tan((atan(v(1)*r))-(g*r*(tK(i,n)+h))))/r;
ks4(i,n)=fKs4(i,n)*h;
sK(i+1,n)=sK(i,n)+(ks1(i,n)+2*ks2(i,n)+2*ks3(i,n)+ks4(i,n))/6;
tK(i+1,n)=i*h;
%Error in Velocity
vActual(i+1,n)=v(t==tK(i+1,n));
vKError(i+1,n)=abs((vActual(i+1,n)-vK(i+1,n))*100/vActual(i+1,n));
%Error in Displacement
sActual(i+1,n)=s(t==tK(i+1,n));
sKError(i+1,n)=abs((sActual(i+1,n)-sK(i+1,n))*100/sActual(i+1,n));
end
subplot(2,2,1)
plot(tK(:,n),vK(:,n),'x')
hold on
subplot(2,2,2)
plot(tK(:,n),sK(:,n),'+')
hold on
subplot(2,2,3)
plot(tK(:,n),vKError(:,n))
hold on
subplot(2,2,4)
plot(tK(:,n),sKError(:,n))
hold on
end
subplot(2,2,1)
legend('Analytical Velocity','8 Steps','16 Steps','32 Steps');
subplot(2,2,2)
legend('Analytical Displacement','8 Steps','16 Steps','32 Steps');
28
BTH Computational Engineering I
subplot(2,2,3)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Velocity')
title('Percentage deviation from values of Analytical Velocity vs Time')
legend('8 Steps','16 Steps','32 Steps')
subplot(2,2,4)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Displacement')
title('Percentage deviation from values of Analytical Displacement vs
Time')
legend('8 Steps','16 Steps','32 Steps')
hold off
%% -----------------------------MILNE METHOD---------------------------- %%
figure(6)
subplot(2,2,1)
plot(t,v)
xlabel('t, Time in seconds')
ylabel('v(t), Velocity in meters/second')
title('Plot of Velocity vs Time for toy rocket compared with Milne Method')
hold on
subplot(2,2,2)
plot(t,s)
xlabel('t, Time in seconds')
ylabel('s(t), Displacement in meters')
title('Plot of Displacement versus time for toy rocket compared with Milne
Method')
hold on
for n=1:3
N=2^(n+2);
h=3/N;
for j=1:4
vActual(1,n)=60;
vM(j,n)=vK(j,n);
sM(j,n)=sK(j,n);
tM(j,n)=(j-1)*h;
end
end
for n=1:3
N=2^(n+2);
h=3/N; %step size
for i=4:N
29
BTH Computational Engineering I
vM(i+1,n)=vM(i-3,n)+4*h*(2*fMv(i-2,n)-fMv(i-1,n)+2*fMv(i,n))/3;
%predictor
fMv(i+1,n)=-g-((k/m)*vM(i+1,n)^2);
vM(i+1,n)=vM(i-1,n)+h*(fMv(i-1,n)+4*fMv(i,n)+fMv(i+1,n))/3;
%corrector
fMs(i-2,n)=(tan((atan(v(1)*r))-(g*r*tM(i-2,n))))/r;
fMs(i-1,n)=(tan((atan(v(1)*r))-(g*r*tM(i-1,n))))/r;
fMs(i,n)=(tan((atan(v(1)*r))-(g*r*tM(i,n))))/r;
tM(i+1,n)=i*h;
sM(i+1,n)=sM(i-3,n)+4*h*(2*fMs(i-2,n)-fMs(i-1,n)+2*fMs(i,n))/3;
%predictor
fMs(i+1,n)=(tan((atan(v(1)*r))-(g*r*tM(i+1,n))))/r;
sM(i+1,n)=sM(i-1,n)+h*(fMs(i-1,n)+4*fMs(i,n)+fMs(i+1,n))/3;
%corrector
%Error in Velocity
vActual(i+1,n)=v(t==tM(i+1,n));
vMError(i+1,n)=abs((vActual(i+1,n)-vM(i+1,n))*100/vActual(i+1,n));
%Error in Displacement
sActual(i+1,n)=s(t==tM(i+1,n));
sMError(i+1,n)=abs((sActual(i+1,n)-sM(i+1,n))*100/sActual(i+1,n));
end
subplot(2,2,1)
plot(tM(:,n),vM(:,n),'x')
hold on
subplot(2,2,2)
plot(tM(:,n),sM(:,n),'+')
hold on
subplot(2,2,3)
plot(tM(:,n),vMError(:,n))
hold on
subplot(2,2,4)
plot(tM(:,n),sMError(:,n))
hold on
end
subplot(2,2,1)
legend('Analytical Velocity','8 Steps','16 Steps','32 Steps');
subplot(2,2,2)
legend('Analytical Displacement','8 Steps','16 Steps','32 Steps');
subplot(2,2,3)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Velocity')
title('Percentage deviation from values of Analytical Velocity vs Time')
legend('8 Steps','16 Steps','32 Steps')
30
BTH Computational Engineering I
subplot(2,2,4)
xlabel('t, time in seconds')
ylabel('% Error in Measurement of Displacement')
title('Percentage deviation from values of Analytical Displacement vs
Time')
legend('8 Steps','16 Steps','32 Steps')
hold off
Tspan=[0 3];
V0=60;
S0=0;
% Velocity comparison
figure(7)
subplot(2,2,1)
plot(t,v)
hold on
subplot(2,2,1)
plot(Tv,V,'h')
xlabel('t, Time in seconds')
ylabel('v(t), Velocity in m/s')
title('Plot of Velocity vs Time for toy rocket compared with MATLAB
ode45compared with MATLAB ode45')
legend('Analytical Velocity','MATLAB ode45 Velocity ')
hold off
% Displacement Comparison
subplot(2,2,2)
plot(t,s)
hold on
subplot(2,2,2)
plot(Ts,S,'s')
xlabel('t, Time in seconds')
ylabel('s(t), Displacement in meters')
title('Plot of Displacement vs Time for toy rocket compared with MATLAB
ode45')
legend('Analytical Displacement','MATLAB ode45 Displacement')
hold off
31
BTH Computational Engineering I
32