Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6831267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:43:03+00:00 2026-05-26T22:43:03+00:00

I am trying to write a simple n-body gravity simulation with 4 particles in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:43:04+00:00Added an answer on May 26, 2026 at 10:43 pm

    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 strel or [a,b,~] = sphere(N).

    Once you have the frames, you can easily make an AVI file (uncompressed or MPEG) in MATLAB:

    aviOutput = fullfile('path','to','file.avi');
    aviobj = VideoWriter(aviOutput);
    aviobj.Quality = 100;
    aviobj.FrameRate = 24; %# arbitrary
    open(aviobj);
    for i=1:length(frames)
        writeVideo(aviobj,frames{i}); %# or frames(:,:,i) etc.
    end
    close(aviobj);
    

    Update

    The above assumes the time steps are all available. For example:

    files = dir('path/to/dir');
    files(1:2) = []; %# . and ..
    orderedfiles = cell(length(files),1);
    for i=1:length(files)
       ind = sscanf(files(i).name,[name '%*[_]%u%*s']);
       orderedfiles{ind+1} = files(i).name;
    end
    timeSteps = zeros(numPoints,2,length(orderedfiles));
    for i=1:length(orderedfiles)
        temp = load(orderedfiles(i).name);
        timeSteps(:,:,i) = temp.matrixName %# All the same name?
    end
    

    The code from the linked answer is written to operate on two vectors x and y with the coordinates, which you would get with timeSteps(:,1,i) and timeSteps(:,2,i), and do for each time step.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write simple Visual Studio Add-In for code generation. In my
I'm trying to write simple proxy server for some purpose. In it I use
I'm trying to write a simple game/utility to calculate poker odds. I know there's
I'm trying to write a simple program in VC++ which will just initialize the
I'm trying to write a simple WPF app that has two ellipses, joined by
I'm trying to write a simple raytracer as a hobby project and it's all
I'm trying to write a simple ruby script that will copy a file to
I'm trying to write a simple AppleScript to do some text manipulation on the
I am trying to write a simple tool using Shoes. This will indent code
I am trying to write a simple program to open a socket channel to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.