2013-04-30 46 views
0

我的代碼創建了三條數據點的「線」,但不會將這些點連接成線!我看過教程,嘗試了諸如plot(Time,CurrentSpeed1,' - ')和添加標記之類的東西,但無論如何,它總是有三個不相連的點的顏色系列。這是我得到的:爲什麼Matlab中的2D數據點不能成爲線?

Time = 0; 

while (Acceleration1 > 0.012 || Acceleration2 > 0.012 || Acceleration3 > 0.012) 
    Drag = (1/2) * AirDensity * (CurrentSpeed1^2) * DragCoefficient * Area; 
    Force = EnginePower/CurrentSpeed1; 
    Acceleration1 = (Force-Drag)/EmptyWeight; 
    CurrentSpeed1 = CurrentSpeed1 + Acceleration1; 

    Drag = (1/2) * AirDensity * (CurrentSpeed2^2) * DragCoefficient * Area; 
    Force = EnginePower/CurrentSpeed2; 
    Acceleration2 = (Force-Drag)/HalfWeight; 
    CurrentSpeed2 = CurrentSpeed2 + Acceleration2;  

    Drag = (1/2) * AirDensity * (CurrentSpeed3^2) * DragCoefficient * Area; 
    Force = EnginePower/CurrentSpeed3; 
    Acceleration3 = (Force-Drag)/FullWeight; 
    CurrentSpeed3 = CurrentSpeed3 + Acceleration3; 

    plot(Time, CurrentSpeed1, Time, CurrentSpeed2, Time, CurrentSpeed3); 

    Time = Time + 1; 
    hold on 
end 

xlabel('Time (Seconds)'); 
ylabel('Speed (m/s)'); 
hold off 

爲什麼哦爲什麼?歡呼:)

回答

1

@shoelzer說,你需要一個值的數組。這是你的代碼的簡化版本,顯示一個例子:

Time = 0; 
CurrentSpeed1=0; 
CurrentSpeed2=0; 
CurrentSpeed3=0; 
while (Time<10) 
    OldTime=Time; 
    Time = Time + 1; 

    OldSpeed1=CurrentSpeed1; 
    CurrentSpeed1 = Time+1; 

    OldSpeed2=CurrentSpeed2; 
    CurrentSpeed2 = Time+2;  

    OldSpeed3=CurrentSpeed2; 
    CurrentSpeed3 = Time+3; 


    plot([OldTime Time], [OldSpeed1 CurrentSpeed1], [OldTime Time], [OldSpeed2 CurrentSpeed2], [OldTime Time], [OldSpeed3 CurrentSpeed3]); 

    hold on 
end 

xlabel('Time (Seconds)'); 
ylabel('Speed (m/s)'); 
hold off 

我只是確保存儲「舊」點,然後我就可以用新的點連接它們

+0

So..hang on ...什麼是OldTime?陣列在哪裏? – Magicaxis 2013-04-30 16:58:24

+1

爲了通過一條直線連接兩個點,您需要爲plot命令提供矢量形式的兩個點的x和y值(即:plot([xpt1 xpt2],[ypt1 ypt2]);')。我正在做的'OldTime','OldSpeed1'等正在存儲您的點的以前的xy值,以便我可以將舊座標和新座標都傳遞給繪圖命令。這將連接你的兩個點 – anonymous 2013-04-30 17:01:19

+0

存儲每個值的數組像@shoelzer說是另一種方式來做同樣的事情;而不是在循環內逐步繪製它,然後繪製整個事件 – anonymous 2013-04-30 17:06:27

1

你的時間和速度變量是單一的值,所以當你繪製你得到積分。要繪製一條線,您需要一組值。例如:

figure 
hold all 
plot(3, 4, 'o') % plot a point 
plot(1:10, 1:10) % plot a line 

在循環內部,您需要將計算值存儲在數組中,然後在循環之後繪製數組。

+0

所以....每個之後段,我需要像這樣: 'array1.append(CurrentSpeed); array2.append(CurrentSpeed); array3.append(CurrentSpeed);' 然後在while循環之後: plot(Time,array1,Time,array2,Time,array3)? – Magicaxis 2013-04-30 16:52:51

+0

非常。在Matlab中你可以做'array1 [n] = CurrentSpeed;'。請務必[預先分配](http://www.mathworks.com/support/solutions/en/data/1-18150/),或者對於大型數組,它會變得非常慢。 – shoelzer 2013-04-30 16:57:59

相關問題