Can someone help me ?
Afficher commentaires plus anciens
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
Réponses (1)
David Hill
le 13 Oct 2022
Modifié(e) : David Hill
le 13 Oct 2022
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 commentaires
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;
Catégories
En savoir plus sur Programming dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


