I am trying to write a simple n-body gravity simulation with 4 particles in C++. I am outputting the positions of the 4 particles to .mat files labeled “time_X.mat” (where X=1,2,3…. indicates the time-stamp) in the form of a 4×2 matrix where
i’th row indicates (x,y) cooridinates of the ith particle at time X .
Now for a particular time-step I am able load the .mat file into MATLAB and get a scatterplot of the points in the matrix, showing me particle positions. But I would like to create a movie out of all the .mat files / scatter-plots of the time_X.mat files which shows me the evolution of the 4 particle syestem . How should I do that in MATLAB?
First you have to make frames (images) of each time step of your simulation.
Since you have Cartesian coordinates, you have to convert your x and y coordinates to pixel indices, and make an image matrix from them. See my answer to a relevant question for an example of how to do this. You could also modify the code to make your points larger than a pixel, have shapes, etc., perhaps with
strelor[a,b,~] = sphere(N).Once you have the frames, you can easily make an AVI file (uncompressed or MPEG) in MATLAB:
Update
The above assumes the time steps are all available. For example:
The code from the linked answer is written to operate on two vectors
xandywith the coordinates, which you would get withtimeSteps(:,1,i)andtimeSteps(:,2,i), and do for each time step.