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 Kommentare

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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Hill
David Hill am 13 Okt. 2022
Bearbeitet: David Hill am 13 Okt. 2022

0 Stimmen

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 Kommentare

Cedrik
Cedrik am 14 Okt. 2022
Bearbeitet: Cedrik am 14 Okt. 2022
@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;

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 13 Okt. 2022

Kommentiert:

am 14 Okt. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by