Hello everyone, I am new to Matlab and working on a code...
I have written this code.
dt = 1;
tmax = 200;
t = 0:dt:tmax;
G = (6.67408*10^-11);
Mearth = (5.9722*10^24);
R = 6371;
A = 75;
Cd = 0.4;
h = 0;
Mempty = 54000;
Minitial = 894000;
Ve = 4500;
dm = 5000;
V = zeros(1,length(t));
V(1) = 1;
m = zeros(1,length(t));
m(1) = Minitial;
h = zeros(1,length(t));
h(1) = 1
p = zeros(1,length(t));
p(1) = 1.225;
for n=2:length(t)
if m(n-1) >= Mempty;
m(n) = Minitial-(dt*dm*n);
else m(n-1) = Mempty;
end
V(n) = V(n-1)+dt*(Ve*dm-G*((Mearth*m(n-1))/(h(n-1))^2)-0.5*p(n-1)*A*(V(n-1))^2*Cd)/m(n-1);
h(n) = V(n)*n;
p(n) = 1.225*10^(-3*h(n-1)/50000);
end
plot(t,V)
This code is supposed to calculate the velocity and altitude of a rocket flying straight up. However, for some reason as it got evident from the graph my V(n) values seem to be calculate for only 2 times steps and somehow ignores tmax variable? My assumption is that there is something wrong in my V(n) Equation, line 32.
For my V(n) I am using this ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/286673/image.png)
and the graph that I am getting is ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/286674/image.png)
which is clearly not what I am trying to get here.
I would really appreciate if someone could help me out to fix the V(n) in order to find the velocity.