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

댓글 수: 1

Sriram Tadavarty
Sriram Tadavarty 2020년 3월 17일
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)

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2020년 3월 17일
편집: KSSV 2020년 3월 17일

0 개 추천

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

Matthew Worker
Matthew Worker 2020년 3월 17일
This still does not work unfortunately.
KSSV
KSSV 2020년 3월 17일
It is able to plot right?
Matthew Worker
Matthew Worker 2020년 3월 17일
The function runs with no errors, but no plot shows. Just an empty figure screen.
KSSV
KSSV 2020년 3월 17일
It is plotting...I have attched the image...Edited the code..check now.
Matthew Worker
Matthew Worker 2020년 3월 17일
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 2020년 3월 17일
It is because your v(k-1) is only a single number i.e scalar. Which you can only plot by using markers.
Matthew Worker
Matthew Worker 2020년 3월 17일
Thank you so much !

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Contour Plots에 대해 자세히 알아보기

질문:

2020년 3월 17일

댓글:

2020년 3월 17일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by