How do I plot a line graph from two for loops?
1 view (last 30 days)
Show older comments
Theodore Oberg
on 18 Nov 2016
Commented: Theodore Oberg
on 19 Nov 2016
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
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).
Accepted Answer
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!
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!