Marta Sampietro
Marta Sampietro

Reputation: 339

Plot a line graph from 3 variables in Matlab

I have three variables: px, py and t, which indicate the x coordinate, y coordinate and time respectively.

I need to have a 2D line graph in order to visualize the evolution of the position over time. I don't need to plot the exact position, nor know where the object goes. I just to visualize the trajectory and speed of changing in the position of the object. For example, if between t1 and t2 the object stays still, the line would be completely horizontal. If then it moves slightly, the line would go up a little bit, and if then it moves drastically, the line's slope would go up a lot as well.

In other words, I need a way to join both position coordinates into one single variable to plot over time, but without losing the meaningful information they give me.

I've tried to plot the information in 3D with plot3, but its visualization is just not clear enough, so I decided to change my approach and try to only visualize the speed at which the object's trajectory is changing.

Upvotes: 0

Views: 172

Answers (1)

Xcross
Xcross

Reputation: 2132

In the below code, I calculated the distance from the origin and plotted it along with time.

distance=sqrt(px.^2+py.^2);
plot(t,distance);

If that doesn't satisfy your requirement use the below code. In this, I calculated the displacement from one point(x,y) to the next point(x1,y1) and appended zero in front to make the dimensions same. Then found the cumulative sum, so that you will get a horizontal line when object stays still.

p=[px;py];
pd=p;
p(:,end)=[];
pd(:,1)=[];
p=[[0;0] p];
pd=[[0; 0] pd];
displacement=sqrt((pd(1,:)-p(1,:)).^2+(pd(2,:)-p(2,:)).^2);
cdisp=cumsum(displacement);
plot(t,cdisp); 

Upvotes: 1

Related Questions