Attempting to Plot Multiple Answers from a Loop

1 view (last 30 days)
I'm rather new/inexperienced in MatLab and was wondering if I could get any help!
I have to create a while loop within a for loop to determine what breaking system should be implimented in a rollercoaster for it to stop on time, safely. My code looks like this:
Tm = 13000 %kg, total mass of coaster
g = 9.81 %m/s^2, gravity
m = 75 %kg, mass of passenger
h = 130 %m, height of coaster
for n = 1:3
%Part B
%Opt. 1 Opt. 2 Opt. 3
Brake=[0 0 10000; 0 100 10000; -10 1000 100];
% Part C
xo = 0 %m, starting position
Ti = 10 %m/s, inital value at top of hill
Di = 0 %m/s^2, inital de-acceleration at bottom of the hill
Pi = 0 %m, inital position at the bottom of the hill
%Part D
Vb = sqrt(Ti^2+2*g*h) % motion down the track
%Part E
Vn=Vb
xn=xo
i=0
t=0
while Vn>0
deltat=0.0001
i=i+1
t=t+deltat*i
T(i)=t
Fb(n)=(Brake(n,1)*Ti^2)+(Brake(n, 2)*Ti)+Brake(n, 3) %apply breaks
ac(n) = -Fb(n)/Tm %acceleration
Fp = abs(m*ac(n)) %force exerted on passenger
Vn=(ac(n)*T(i))+Vn %new velocity
xn=(V*T(i))+xn %new position
end
figure(1)
plot(T(i),V(1)), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
hold on
plot(T(i),V(2)), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
hold on
plot(T(i),V(3)), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
hold off
end
Every time I attemped to graph each possible breaking system, the graphs appear blank. I'm unsure if it has something to do with my code, or what I'm trying to graph, or something else entierly.
Any help is appreciated, thank you!

Answers (2)

Walter Roberson
Walter Roberson on 12 Dec 2019
You do not define a variable V in your code, but you use it in the line
xn=(V*T(i))+xn
and you attempt to plot it several times in
plot(T(i),V(1)), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
plot(T(i),V(2)), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
plot(T(i),V(3)), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
The way you use it as the Y component suggests that you expect V to be an output of the loop. The fact that you expect your plot to be in time suggests that you expect that V(1), V(2), V(3) will each be vectors.
Note that the variable named V has nothing to do with the variable named Vn
It looks to me as if you are attempting to use dynamic variable names. Don't do that. http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
plot(T(i),V(1)), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
After your while loop, i will have a specific scalar value, so T(i) will be a scalar. So in each of your three plots, you are plotting a scalar versus a scalar. MATLAB only ever draws lines when there are two adjacent finite values, and since you only plot one point at a time, it will not draw anything.
plot(T(i),V(1)), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
hold on
plot(T(i),V(2)), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
You are not creating new figures or sub-plots, and you are using hold on so all three of your plots are going to get plotted on the same axes. I would suggest to you that there is no value in setting the title() or *label() multiple times for the same axes.
If you do succeed in plotting three lines, how do you expect the user to be able to tell which of the lines has which meaning? Perhaps you should consider using legend()

Ridwan Alam
Ridwan Alam on 12 Dec 2019
Edited: Ridwan Alam on 12 Dec 2019
I believe you missed to track the V inside your loops. Also, please lookup the documentation for plot() for a better manipulation.
Tm = 13000 %kg, total mass of coaster
g = 9.81 %m/s^2, gravity
m = 75 %kg, mass of passenger
h = 130 %m, height of coaster
for n = 1:3
%Part B
%Opt. 1 Opt. 2 Opt. 3
Brake=[0 0 10000; 0 100 10000; -10 1000 100];
% Part C
xo = 0 %m, starting position
Ti = 10 %m/s, inital value at top of hill
Di = 0 %m/s^2, inital de-acceleration at bottom of the hill
Pi = 0 %m, inital position at the bottom of the hill
%Part D
Vb = sqrt(Ti^2+2*g*h) % motion down the track
%Part E
Vn=Vb
xn=xo
i=0
t=0
while Vn>0
deltat=0.0001
i=i+1
t=t+deltat*i
T(i)=t
Fb(n)=(Brake(n,1)*Ti^2)+(Brake(n, 2)*Ti)+Brake(n, 3) %apply breaks
ac(n) = -Fb(n)/Tm %acceleration
Fp = abs(m*ac(n)) %force exerted on passenger
Vn=(ac(n)*T(i))+Vn %new velocity
xn=(Vn*T(i))+xn %new position
V(i) = Vn;
end
figure(1)
plot(T,V), title('Train Coaster Velocity in Time'), xlabel('Time, sec'), ylabel('Velocity, m/s'), grid
hold on
end
This generates the figure: (you can add legend to it using legend())
untitled.jpg

Categories

Find more on 2-D and 3-D Plots 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!