为什么Matlab中的2D数据点不能连接成线?

时间:2013-04-30 16:36:44

标签: matlab graph plot 2d line

我的代码会创建数据点的三个“行”,但不会将这些点连接成行!我查看了教程,并尝试了绘图(Time,CurrentSpeed1,' - ')和添加标记之类的东西,但无论如何,它总是有三个不同颜色的点系列,它们没有连接。这就是我得到的:

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

为什么哦为什么?干杯:)

2 个答案:

答案 0 :(得分:1)

您的时间和速度变量是单个值,因此当您绘制图表时,您获得积分。要绘制线条,您需要一组值。例如:

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

在循环内部,您需要将计算值存储在数组中,然后在循环后绘制数组。

答案 1 :(得分: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

我只是确保存储'旧'点,然后我可以将它们与新点连接