close all;clc;SATURNVdata = fopen('Rocket.dat','w'); % Opens a file to dump into
dt=10^1;while dt>=10^-2 % change in dt while loop
% Define variables
G = 6.67e-11;ME = 5.97e24;RE = 6.37e6;T_1 = 3.4e7;ve_1 = 2.58e3;T_2 = 5.0e6;ve_2 = 4.13e3;T_3 = 1.0e6;ve_3 = 4.13e3;m0 = 2951000;% Define initial conditions
y0=0;v0=0;m=m0;t0=0;t=t0;% Define initial parameters
y=y0;a=-(G*ME)/(RE+y)^2+(T_1/m);v=v0;fprintf(SATURNVdata,'%f %f %f %f %f\n',t,y,v,a,m); % Dump variables into table
t=t+dt; % time step
while t<164.6 % ingite stage 1
m=m-T_1*(dt)/ve_1; v=v+a*dt; a=-G*ME/(RE+y)^2+T_1/m; y=y+v*dt; fprintf(SATURNVdata,'%f %f %f %f %f\n',t,y,v,a,m); t=t+dt; end % empty tank stage 1
m=m-131000; % jettison stage 1
while t<=167.8 % no thrust
m=m; v=v+a*dt; a=-G*ME/(RE+y)^2; y=y+v*dt; fprintf(SATURNVdata,'%f %f %f %f %f\n',t,y,v,a,m); t=t+dt; end while t<534.6 % ignite stage 2
m=m-T_2*(dt)/ve_2; v=v+a*dt; a=-G*ME/(RE+y)^2+T_2/m; y=y+v*dt; fprintf(SATURNVdata,'%f %f %f %f %f\n',t,y,v,a,m); t=t+dt; end % empty tank stage 2
m=m-36000; % jettision stage 2
while t<=537.8 % no thrust m=m; v=v+a*dt; a=-G*ME/(RE+y)^2; y=y+v*dt; fprintf(SATURNVdata,'%f %f %f %f %f\n',t,y,v,a,m); t=t+dt; end while t<983.5 % ignite stage 3
m=m-T_3*(dt)/ve_3; v=v+a*dt; a=-G*ME/(RE+y)^2+T_3/m; y=y+v*dt; fprintf(SATURNVdata,'%f %f %f %f %f\n',t,y,v,a,m); t=t+dt; end % empty tank stage 3
m=m-11000; % jettison stage 3
while t<=1000.1 % no thrust m=m; v=v+a*dt; a=-G*ME/(RE+y)^2; y=y+v*dt; fprintf(SATURNVdata,'%f %f %f %f %f\n',t,y,v,a,m); t=t+dt; end dt=dt*0.1; % change in dt
endfclose(SATURNVdata); % Closes the file after loop has stopped
load Rocket.dat % Loads the file for creating graphs
%plot of altitude
figure(1)plot(Rocket(:,1),Rocket(:,2));title('Altitude')ylabel('Altitude [m]')xlabel('time [s]')hold on%plot of velocity
figure(2)plot(Rocket(:,1),Rocket(:,3));title('Velocity')ylabel('Velocity [m/s]')xlabel('time [s]')hold on%plot of acceleration
figure(3)plot(Rocket(:,1),Rocket(:,4));title('Acceleration')ylabel('Acceleration [m/s^2]')xlabel('time [s]')hold on%plot of mass
figure(4)plot(Rocket(:,1),Rocket(:,5));title('Mass')ylabel('Mass [kg]')xlabel('time [s]')hold on
MATLAB: So whenever I run this program, which is a model of the saturn V rocket going into space. The plot will reset and overlay another plot on top of the other. I don’t know how to explain it well so I hope if you run the program you can see the problem
graphplotplottingwhile loop
Best Answer