How do I plot a line graph from two for loops?

1 view (last 30 days)
How can I plot a line graph for h over t and v over t in my code below? I have two for loops I want to graph continuously. Everything else seems to be working as I want it. I want to display the values when f=0, when v is closest to 0, and when h is closest to 0. Yet, I need a line plot to visually show both v over t, velocity over time, and h over t, height over time, combining both functions from t=1:80, and t=81:442
Thank you!
s = f / l;
l = 50;
f = 4000;
a=20;
g=-9.8;
h=0;
for t=1:s
v=a*t;
f=f-l;
h=h+v;
if f==0
display (t)
display (v)
display (f)
display (h)
fprintf ('Average velocity in meters per second per second when rocket runs out of fuel is %d\v',v)
a=g;
for t=(s+1):450;
v=v+(a);
h=h+v;
if t==243
display (t)
display (v)
display (h)
fprintf ('Average maximum height in meters above the ground is %d\v',h)
else
if t==442
display (t)
display (v)
display (h)
fprintf ('Average velocity in meters per second per second right before the rocket hits the ground is %d\v',v)
break;
end
end
end
end
end
  3 Comments
dpb
dpb on 18 Nov 2016
Edited: dpb on 18 Nov 2016
s = f / l;
l = 50;
f = 4000;
...
NB: You've calculated s prior to having defined f, l ...
Reorder to compute s after defining its constituents; else't it'll either error if were to clear variables to start fresh or it's using a previously computed value which may or may not reflect the l/f in the script (at least until the second time it's run w/o changing the two values, anyway).
Theodore Oberg
Theodore Oberg on 18 Nov 2016
Edited: Theodore Oberg on 18 Nov 2016
Ok, I have constructed a code to graph what I want my two functions to look like. I will attach my entire code below, with comments, including my line plot. I need to graph both the velocity and height of a rocket over time. Ignoring other forces such as drag etc, the first function is the rockets height and velocity due to burning fuel. Once fuel runs out, the rocket begins to free fall. And the two outputs of the functions are velocity and height.
I need to edit my graph, optimize it, so that it only shows the values applicable. First, while the fuel is burning from 0 to 80 seconds. Second, while rocket is in free fall from the moment it runs out of fuel to when it hits the ground. Right now, my graph continues after it hits the ground, I need it to stop where the rocket hits the ground, i.e. where I inserted the black horizontal and vertical lines.
%This program will perform two functions for a rocket by calculating its velocity and height over time.
%The first function is the accumulation of height and velocity, and the depreciation
%of fuel, over time, using the acceleration of the fuel. Once the rocket has
%run out of fuel, the second function will come into play and will use the
%acceleration of gravity to reverse the direction of the rocket until it
%hits the ground.
%the burn rate in -kg per sec of fuel
burnrate = 50;
%the initial amount of fuel in kg
fuel = 4000;
%the number of seconds it takes to run out of fuel, fuel/burn rate
s = fuel / burnrate;
%the acceleration of the rocket due to the fuel burning in meters per
%second per second
acceleration = 20;
%the acceleration due to gravity in meters per second per second, this
%takes effect after the fuel has run out
gravity = -9.8;
%the initial heigth of the rocket in meters
height = 0;
%this for loop is using the acceleration of the fuel to determine the
%height and velocity, and depreciation of fuel, over time from 1 to s, when the fuel runs out
for seconds=1:s velocity=acceleration*seconds; fuel=fuel-burnrate; height=height+velocity;
%when the fuel runs out, f=0, the rocket has reached max velocity, so
%display the figures associates with the last second of accumulation due to
%acceleration of fuel
if fuel==0
display (seconds)
display (fuel)
display (velocity)
display (height)
fprintf ('Velocity in meters per second per second when rocket runs out of fuel is %d\v',velocity)
%when fuel has run out, f=0, use the new acceleration of gravity, a=g
acceleration=gravity;
%for every second after the fuel ran out, s+1, calculate the velocity and
%height of the rocket for every second through a large enough number of
%seconds to calculate the end values when the rocket hits the ground, I
%picked 450
for seconds=(s+1):450; velocity=velocity+(acceleration); height=height+velocity;
%I found the peak of the rockets height by saying below, when velocity is less than 0, v<=0, display the figures, and the
%first number of time t was 244, so I backed up one second to equal 243
%when the rocket last had a positive velocity, at this point the rocket
%would reach the maximum height
%when t=243 display the values of time, velocity, and height
if seconds==243
display (seconds)
display (velocity)
display (height)
fprintf ('Maximum height of the rocket in meters above the ground is %d\v',height)
%I used the same strategy as above to find when the rocket hits the ground,
%by saying if h<=0 then t=443, however height was negative, so I backed up
%one second to 442 to have a positive height. This was the last second the
%rocket was in motion and can determine the result is its final velocity
%442 seconds after launch, print display the values of time, velocity, and
%height
else if seconds==442
display (seconds)
display (velocity)
display (height)
fprintf ('Velocity in meters per second per second right before the rocket hits the ground is %d\v',velocity)
%stop displaying any further calculations
break;
end
end
end
end
end
%plot both velocity functions so they intersect
subplot(1,2,1)
x = 1:400;
y = 20 * x;
g = 1600-(9.8 * x);
plot(x(1:80),y(1:80),x+80,g);
%draw vertical and horizontal line to intersect at the coordinate where the
%rocket hits the ground
yL = get(gca,'YLim');
line([443 443],yL,'Color','k');
xL = get(gca,'XLim');
line(xL,[-1947.6 -1947.6],'Color','k');
xlabel('Time (sec)')
ylabel('Velocity (m/s^2)')
title('Velocity of the Rocket')
%plot both height functions so they intersect
subplot(1,2,2)
x = 1:380;
y = cumsum(20 * x);
g = 64800+cumsum(1600+(-9.8 * x));
plot(x(1:81),y(1:81),x+80,g)
%draw vertical and horizontal line to intersect at the coordinate where the
%rocket hits the ground
yL = get(gca,'YLim');
line([443 443],yL,'Color','k');
xL = get(gca,'XLim');
line(xL,[0 0],'Color','k');
xlabel('Time (sec)')
ylabel('Height (m)')
title('Height of the Rocket')

Sign in to comment.

Accepted Answer

Nick Counts
Nick Counts on 18 Nov 2016
If you want to do a simulation where you are stepping forward in time, I suggest saving the results of each timestep so when your simulation completes you have all the data.
In your example, you see to be recreating your data at the end in order to plot.
Note: my example is just to show you the structure I am suggesting
% Model Parameters
burnrate = 50; % kg/s
fuel = 4000; % kg
t = fuel / burnrate; % seconds
a_thrust = + 20; % m/s^2
a_gravity = -9.8; % m/s^2
y_0 = 0; % m
v_0 = 0; %m/s
% Simulate each second:
altitude = y_0;
velocity = v_0;
acceleration = 0;
time = 0;
% Simulate until rocket is below ground level
while altitude >= 0
time(end + 1) = time(end) + 1;
% Change model behavior based on fuel
if fuel > 0
deltaFuel = burnrate;
accel = a_thrust;
else
deltaFuel = 0;
accel = a_gravity;
end
% Build vectors for each parameter at each timestep
fuel = fuel - burnrate;
acceleration(end + 1,1) = accel;
velocity(end + 1,1) = velocity(end) + accel;
altitude(end + 1,1) = altitude(end) + velocity(end);
end
% Use logical indexing to select the data corresponding to a positive
% vertical velocity
% Some benefits of this approach are that you can do additional
% math on your results. For example, you could find the points with
% positive acceleration as follows (if you didn't already have an
% acceleration vector)
posAccelIndex = diff(velocity)>0; % NOTE: this vector is 1 line shorter than the model!
posAccelIndex = [true; posAccelIndex]; % Include the initial condition
% Since you do have that parameter, you can use simple logical
% indexing:
posAccelIndex = acceleration >= 0;
% Print some output - use the vectors you built in the model
disp(sprintf('Altitude reached: %f meters', max(altitude)));
disp(sprintf('Velocity at impact: %f m/s', velocity(end)));
% Plot your results using logical indexing to color code and set the legend
figure;
subplot(1,2,1)
hold on;
plot(time(posAccelIndex), velocity(posAccelIndex), 'r', 'displayname', 'Powered');
plot(time(~posAccelIndex), velocity(~posAccelIndex), 'b', 'displayname', 'Freefall');
title('Vehicle Velocity (in m/s)')
legend('show')
subplot(1,2,2)
hold on;
plot(time(posAccelIndex), altitude(posAccelIndex), 'r', 'displayname', 'Powered');
plot(time(~posAccelIndex), altitude(~posAccelIndex), 'b', 'displayname', 'Freefall');
title('Vehicle Altitude (in m)')
legend('show')
Hopefully this helps!
  1 Comment
Theodore Oberg
Theodore Oberg on 19 Nov 2016
Thank you, Nick!
The code you provided seems to streamline my process, I will study the code so I understand what each part is doing, and make sure I am within the parameters of my task. I will ask when I have any questions that may come up.
Again, thank you!!

Sign in to comment.

More Answers (1)

dpb
dpb on 18 Nov 2016
To animate a line as compute points, see the example at <line animation>

Categories

Find more on Graphics in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!