Sie sind auf Seite 1von 32

BTH Computational Engineering I

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.

Figure 1. Graph of Velocity and Displacement vs Time for Toy Rocket

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 3. Error in Velocity vs Time for Toy Rocket (Euler’s Method)

Figure 4. Displacement vs Time for Toy Rocket (Euler Method)

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

Figure 5. Error percentage in Displacement vs Time for Toy Rocket

6
BTH Computational Engineering I

3. Mid-Point Method

Figure 6. Velocity vs Time for Toy Rocket

Figure 7. Error in Velocity vs Time for Toy Rocket

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. Displacement vs Time for Toy Rocket

Figure 9. Error percentage in Displacement vs Time for Toy Rocket

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. Velocity vs Time for Toy Rocket

Figure 11. Error percentage in Velocity vs Time for Toy Rocket

Figure 10 and 11 show a good convergence but stability is observed only for higher resolution.

9
BTH Computational Engineering I

Figure 12. Displacement vs Time for Toy Rocket

Figure 13. Error percentage in Displacement vs Time for Toy Rocket

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

Figure 14. Velocity vs Time for Toy Rocket

Figure 15. Error percentage in Velocity vs Time for Toy Rocket

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

Figure 16. Displacement vs Time for Toy Rocket

Figure 17. Error percentage in Displacement vs Time for Toy Rocket

12
BTH Computational Engineering I

6. Runge-Kutta Method

Figure 18. Velocity vs Time for Toy Rocket

Figure 19. Error percentage in Velocity vs Time for Toy Rocket

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

Figure 20. Displacement vs Time for Toy Rocket

Figure 21. Error percentage in Displacement vs Time for Toy Rocket

14
BTH Computational Engineering I

7. Milne Method

Figure 22. Velocity vs Time for Toy Rocket

Figure 23. Error percentage in Velocity vs Time for Toy Rocket

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

Figure 24. Displacement vs Time for Toy Rocket

Figure 25. Error percentage in Displacement vs Time for Toy Rocket

16
BTH Computational Engineering I

8. MATLAB ode45 function

Figure 26. Velocity vs Time for Toy Rocket

Figure 27. Error percentage in Velocity vs Time for Toy Rocket

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

Figure 28. Displacement vs Time for Toy Rocket

Figure 29. Error percentage in Displacement vs Time for Toy Rocket

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

% To plot Velocity and Displacement against time for a toy rocket

g=9.81; %acceleration due to gravity = 9.81m/s^2


m=0.1; %mass of toy rocket = 0.1kg
k=0.004; %air drag constant = 0.004kg/m

% Toy rocket is modelled by Differential Equation dv/dx = -g -(k/m)*v^2

r=sqrt(k/(m*g)); %constant defined for ease of formulation


v(1)=60; %initial speed of rocket after burnout = 60m/s
s(1)=0; %initial displacement is considered 0 meters
t=0:1/320:3; %length of time array for which velcity is calculated

%1) Velocity as a function of time


v=(tan((atan(v(1)*r))-(g*r*t)))/r;

%2) Displacement as a function of time


s=log((cos((atan(r*v(1)))-g*r*t))/(cos(atan(r*v(1)))))/(g*r^2);

%% ---------------------------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

N=2^(n+2); %number of steps in Euler's method


h=3/N; %step size
vE(1,n)=60;
vActual(1,n)=60;
sE(1,n)=0;
tE(1,n)=0;

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

%EULER METHOD for Displacement


fEs(i,n)=(tan((atan(v(1)*r))-(g*r*tE(i,n))))/r;
sE(i+1,n)=sE(i,n)+fEs(i,n)*h;

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

%MIDPOINT METHOD for Velocity


fMPv(i,n)=-g-((k/m)*vMi(i,n)^2);
fMIv(i,n)=-g-((k/m)*(vMi(i,n)+fMPv(i,n)*0.5*h)^2);
vMi(i+1,n)=vMi(i,n)+fMIv(i,n)*h;

%MIDPOINT METHOD for Displacement


fMPs(i,n)=(tan((atan(v(1)*r))-(g*r*tMi(i,n))))/r;
fMIs(i,n)=(tan((atan(v(1)*r))-(g*r*(tMi(i,n)+0.5*h))))/r;
sMi(i+1,n)=sMi(i,n)+fMIs(i,n)*h;

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

N=2^(n+2); %number of steps in Euler's method


h=3/N; %step size
vT(1,n)=60;
vActual(1,n)=60;
sT(1,n)=0;
tT(1,n)=0;

for i=1:N

23
BTH Computational Engineering I

%TRAPEZOIDAL METHOD for Velocity


fTv(i,n)=-g-((k/m)*vT(i,n)^2);
vT(i+1,n)=vT(i,n)+fTv(i,n)*h;
fTv1(i+1,n)=-g-((k/m)*vT(i+1,n)^2);
vdiff=1;
while abs(vdiff)>0.01
vT1(i+1,n)=vT(i,n)+(fTv(i,n)+fTv1(i+1,n))*0.5*h;
vdiff=(vT1-vT)/vT;
vT=vT1;
end

tT(i+1,n)=i*h;

%TRAPEZOIDAL METHOD for Displacement


fTs(i,n)=(tan((atan(v(1)*r))-(g*r*tT(i,n))))/r;
sT(i+1,n)=sT(i,n)+fTs(i,n)*h;
fTs1(i+1,n)=(tan((atan(v(1)*r))-(g*r*tT(i+1,n))))/r;
sdiff=1;
while abs(sdiff)>0.01
sT1(i+1,n)=sT(i,n)+(fTs(i,n)+fTs1(i+1,n))*0.5*h;
sdiff=(sT1-sT)/sT;
sT=sT1;
end

%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

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

%% --------------------RICHARDSON EXTRAPOLATION METHOD------------------ %%

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

%Applying one step Euler with step size h


fRv1(i,n)=-g-((k/m)*(vR1(i,n))^2);
vR1(i+1,n)=vR1(i,n)+fRv1(i,n)*h;

%Applying two step Euler iteration with step size H=h/2

%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

%Applying Richardson Extrapolation Scheme to the Euler outputs


vR(i+1,n)=vR3(i+1,n)+(vR3(i+1,n)-vR1(i+1,n))/3;

%RICHARDSON EXTRAPOLATION for Displacement

%Applying one step Euler with step size h


fRs1(i,n)=(tan((atan(v(1)*r))-(g*r*tR(i,n))))/r;
sR1(i+1,n)=sR1(i,n)+fRs1(i,n)*h;

%Applying two step Euler iteration with step size H=h/2

%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

%Applying Richardson Extrapolation Scheme to the Euler outputs


sR(i+1,n)=sR3(i+1,n)+(sR3(i+1,n)-sR1(i+1,n))/3;
tR(i+1,n)=i*h;

%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

%RUNGE-KUTTA METHOD for Velocity


fKv1(i,n)=-g-((k/m)*(vK(i,n))^2);
kv1(i,n)=fKv1(i,n)*h;

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;

%RUNGE-KUTTA METHOD for Displacement

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

%Fifth value onwards Milne Method is applied

for i=4:N

%MILNE METHOD for Velocity


fMv(i-2,n)=-g-((k/m)*vM(i-2,n)^2);
fMv(i-1,n)=-g-((k/m)*vM(i-1,n)^2);
fMv(i,n)=-g-((k/m)*vM(i,n)^2);

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

%MILNE METHOD for Displacement

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

%% ------------------------ MATLAB ode45 METHOD ------------------------ %%

Tspan=[0 3];
V0=60;
S0=0;

% Velocity comparison

[Tv,V] = ode45(@(Tv,V) -g-((k/m)*V^2),Tspan,V0);

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

[Ts,S] = ode45(@(Ts,S) (tan((atan(V0*r))-(g*r*Ts)))/r,Tspan,S0);

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

%Velocity as a function of time


vCheck=((tan((atan(v(1)*r))-(g*r*Tv)))/r);
vError45=abs((vCheck-V)*100/vCheck);
subplot(2,2,3)
plot(Tv,vError45)
xlabel('t, Time in seconds')
ylabel('% Error in ode45 Velocity')

31
BTH Computational Engineering I

title('Error in MATLAB ode45 Velocity vs Time')

%Displacement as a function of time


sCheck=log((cos((atan(r*v(1)))-g*r*Ts))/(cos(atan(r*v(1)))))/(g*r^2);
sError45=abs((sCheck-S)*100/sCheck);
subplot(2,2,4)
plot(Ts,sError45)
xlabel('t, Time in seconds')
ylabel('% Error in ode45 Displacement')
title('Error in MATLAB ode45 Displacement vs Time')

32

Das könnte Ihnen auch gefallen