How do I plot a line graph from two for loops?
Show older comments
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
Nick Counts
on 18 Nov 2016
Edited: Nick Counts
on 18 Nov 2016
Hi, Theodore,
I'm not quite sure I understand what you mean by "combine functions". If you are having trouble doing something specific in a plot, a little more information would be very helpful - maybe even attach an image that gives us an idea what you are trying to achieve.
If your issue is just that you don't know how to generate a "line graph" then look at the documentation for plot
A quick example to get started:
% Make your data
x = 0:0.01:2*pi();
sx = sin(x);
cx = cos(x);
tx = tan(x);
% Create a figure
fig = figure;
ax = axes;
% Plot your data
hold on;
plot(x, sx, 'b', 'displayname', 'sin(x)');
plot(x, cx, 'g', 'displayname', 'cos(x)');
plot(x, tx, 'm', 'displayname', 'tan(x)');
% Add style to your plot
ax.YLim = [-1 1];
ax.XGrid = 'on';
ax.YGrid = 'on';
legend('show');

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
on 18 Nov 2016
Edited: Theodore Oberg
on 18 Nov 2016
Accepted Answer
More Answers (1)
dpb
on 18 Nov 2016
0 votes
Categories
Find more on General Applications 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!