MATLAB Answers

Cannot plot iterations.

8 views (last 30 days)
Grace Piroscia
Grace Piroscia on 17 Mar 2020
Commented: Grace Piroscia on 17 Mar 2020
I'm trying to plot iterations but it doesn't seem to be working. The figure pops up but no points do. This is confusing because both variables i'm plotting have the same size. How to I plot the iterations of v(k-1) against x ?
x=20:30;%range of inital velocities plotted
figure;
hold on
for i=20:30 %to iterate through a few intial velocities.
v0=i;
v(1)=v0;
s(1)=0;
for k=2:length(t) %This loop uses eulers formula to calculate velocity and displacment
muk=0.7;
a=-muk*g;
dvdt=a; %acceleration
dsdt = v(k-1); %velocity = displacment over time
%approx value of v at current step given previous value
v(k) = v(k-1) + dvdt*dt;
%approx value of s at current step given previous value
s(k)=s(k-1)+dsdt*dt;
%Break loop when velocity starts to become negative
if v(k) < 0
break
end
end
%Value before the first negative velocity value (this is the time where
%the car has stopped).
v(k-1) %this iterates through initial velocities corresponding to the final positive velocity value in each case
%plotting
plot(x,v(k-1))
end

  2 Comments

Sriram Tadavarty
Sriram Tadavarty on 17 Mar 2020
what are the values of t, dt, and g? what are you trying to plot? The command plot(x,v(k-1)) will plot only the last element of v.. Try plot(x(1:length(v)),v)
Grace Piroscia
Grace Piroscia on 17 Mar 2020
Here are the intital parameters.
g = 9.81; %acceleration due to gravity (m/s^2)
dt = 1; %time step in seconds
t = 0:dt:5; %time interval examined
When I do plot(x(1:length(v)),v) it plots all iterations of v for each initial velocity. I only want the v(k-1) value at each initial velocity v0. What confuses me is if i write disp([i,v(k-1)]) I get the exact variables i'm looking for. I just don't know how to turn these into vectors.
writing disp([i; v(k-1)]'); in the for i=20:30 loop gives me:
20.0000 6.2660
21.0000 0.3990
22.0000 1.3990
23.0000 2.3990
24.0000 3.3990
25.0000 4.3990
26.0000 5.3990
27.0000 6.3990
28.0000 0.5320
29.0000 1.5320
30.0000 2.5320
Where the first column is initial values and the second is stopping velocity (v(k-1))
I want to plot these against eachother, i just dont know how to.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 17 Mar 2020
Edited: KSSV on 17 Mar 2020
clear all ; clc;
t=20:30;%range of inital velocities plotted
dt = min(diff(t)) ;
g = 9.81 ;
figure;
hold on
for i=20:30 %to iterate through a few intial velocities.
v0=i;
v(1)=v0;
s(1)=0;
for k=2:length(t) %This loop uses eulers formula to calculate velocity and displacment
muk=0.7;
a=-muk*g;
dvdt=a; %acceleration
dsdt = v(k-1); %velocity = displacment over time
%approx value of v at current step given previous value
v(k) = v(k-1) + dvdt*dt;
%approx value of s at current step given previous value
s(k)=s(k-1)+dsdt*dt;
%Break loop when velocity starts to become negative
if v(k) < 0
break
end
end
%Value before the first negative velocity value (this is the time where
%the car has stopped).
v(k-1) %this iterates through initial velocities corresponding to the final positive velocity value in each case
%plotting
plot(t,v(k-1),'*')
end

  7 Comments

Show 4 older comments
Grace Piroscia
Grace Piroscia on 17 Mar 2020
I replaced plot(x,v(k-1)) from my code with plot(x,v(k-1), '*') and it worked! I'm not sure why but it wont work if i use '--r' or don't specify a format.
Do you know what might be going on?
KSSV
KSSV on 17 Mar 2020
It is because your v(k-1) is only a single number i.e scalar. Which you can only plot by using markers.
Grace Piroscia
Grace Piroscia on 17 Mar 2020
Thank you so much !

Sign in to comment.

More Answers (0)

Sign in to answer this question.