How to 2D-plot moving points in MATLAB

Spread the word!

The following MATLAB codes are also compatible with GNU Octave (I used the version 3.8.1).

This is the final function I wrote:

Note: since I want to plot bigger points for bigger masses, I intentionally limited the maximum mass dimension to 15. It means all other masses are also scaled proportionally.

Let’s take a look to the parameters:

x (or y) – it contains the sequences of all x(or y)-positions of all masses. The nth-row corresponds to the nth-mass and the mth-column represents its mth-position along x(or y)-axes.

m – mass of the point.

t – time (in seconds) between one position and the next one.

xdim (or ydim) – it specifies the lower and upper limits of the x(or y)-axes.

Example

Remember to use addpath containing the address of the massplot function file if this example’s script doesn’t findĀ  it.

This is the result:

Alternatively you can make use of the plot function just changing the scatter‘s line with the following:

Why I used the scatter function instead of plot? Because it looks to be faster here. I tried this example on GNU Octave 3.8.1 using first scatter and than plot. I just deleted the line with pause(t) and called the massplot function ten times (using a for loop). I measured the performance by writing tic before the loop and toc right after that. Result? “Elapsed time is 35.6928 seconds” for scatter, “Elapsed time is 40.7051 seconds” for plot. Successive tests give very similar results.

Be the first to comment

Leave a Reply

Your email address will not be published.


*