8 views (last 30 days)

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

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

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/511252-cannot-plot-iterations#comment_810937

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/511252-cannot-plot-iterations#comment_810937

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/511252-cannot-plot-iterations#comment_810968

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/511252-cannot-plot-iterations#comment_810968

Sign in to comment.