Hello!
i want to plot a curve which the X axis is the time in second and the Y axis is the distance(m).
i have a vehicle runs with 20m/s through a distance of 1000 m but if it arrives at the end position , it will come back at the first position ,until finish a period T=3600s .
first of all the this vehicle should take 50s to arrive the final position.
so i have used the function "linspace"
X1=linspace(0,50,50) ;
Y1=linspace(0,1000,50);
plot(X1,Y1, 'LineWidth', 1.5), hold on ;
X2= linspace(50,100,50);
Y2=linspace(980,20,50);
plot(X2,Y2, 'LineWidth', 1.5), hold on ;
X3=linspace(100,150,50);
Y3=linspace(40,1000,50);
plot(X3,Y3, 'LineWidth', 1.5), hold on ;
........
until arrive to T=3600s.
I am asking if there is an easier way than typing the command manually because I will repeat this for 72 times to get the whole period?
thanks in advance

댓글 수: 2

John D'Errico
John D'Errico 2022년 10월 13일
You cannot use a loop?
Cedrik
Cedrik 2022년 10월 14일
@John D'Errico Hi! i'm trying to user a loop but it doesn't work that i want

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

답변 (1개)

David Hill
David Hill 2022년 10월 13일
편집: David Hill 2022년 10월 13일

0 개 추천

figure;hold on;
x=linspace(0,50,50);
s=0;f=1000;
y=linspace(s,f,50);
plot(x,y,'LineWidth', 1.5);
for k=1:71
x=x+50;
s=s+20;
if mod(k,2)==1
y=linspace(f-s,s,50);%adjust, not sure about the pattern
else
y=linspace(s,1000,50);%adjust, not sure about the pattern
end
plot(x,y,'LineWidth', 1.5);
end

댓글 수: 2

Cedrik
Cedrik 2022년 10월 14일
편집: Cedrik 2022년 10월 14일
@David Hill thank you for your answer, but I did not get the required result, the curve is discontinuous.
after doing the plot, i will extract data (all positions through period T), so if there is a way to find data without plot i will use it.
Try this:
figure;hold on;
x=linspace(0,50,50);
s=0;f=1000;
y=linspace(s,f,50);
plot(x,y,'LineWidth', 1.5);
allx = zeros(50 * 71, 1);
ally = zeros(50 * 71, 1);
for k=1:71
x=x+50;
s=s+20;
if mod(k,2)==1
y=linspace(f-s,s,50);%adjust, not sure about the pattern
else
y=linspace(s,1000,50);%adjust, not sure about the pattern
end
plot(x,y,'LineWidth', 1.5);
drawnow;
% Append to our master lists
startingIndex = (k - 1) * 50 + 1;
allx(startingIndex : startingIndex + 49) = x;
ally(startingIndex : startingIndex + 49) = y;
end
grid on;
figure
plot(allx, ally)
grid on;

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

카테고리

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

질문:

2022년 10월 13일

댓글:

2022년 10월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by