Magicaxis
Magicaxis

Reputation: 381

Why won't my 2D data points in Matlab connect to become lines?

My code creates three "lines" of of data points, but won't connect the points into lines! I've looked at the tutorials, and tried things like plot(Time, CurrentSpeed1, '--') and adding markers, but no matter what, its always three different coloured series of points that aren't connected. Here's what I got:

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

Why oh why? Cheers :)

Upvotes: 0

Views: 3820

Answers (2)

anonymous
anonymous

Reputation: 188

as @shoelzer said, you need an array of values. Here's a simplified version of your code to show an example:

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

I just make sure to store the 'old' points and then i can connect them with the new points

Upvotes: 1

shoelzer
shoelzer

Reputation: 10718

Your time and speed variables are single values so when you plot you get points. To plot a line, you need an array of values. Example:

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

Inside the loop you need to store the calculated values in arrays, then plot the arrays after the loop.

Upvotes: 1

Related Questions