N-body simulation of gravitational interaction: Matlab code/animation ode113

Antonio Sala, UPV

Difficulty: *** ,       Relevance: PIC,      Duration: 16:42

*Enlace a Spanish version

Materials:    [ Cód.: NbodySim.m ] [ PDF ]

Summary:

This video discusses the Matlab simulation code for the gravitational interaction of N bodies (just 2D planar motion, for simplicity). The actual ODE simulator is ode113, and the state equation is that the derivative of the position is the velocity and the derivative of the velocity is the acceleration of gravity (Newton formula). Each body has position ‘x’ and position ‘y’ as well as velocities, so the total order of the system of differential equations is 4N. All the loop code that calculates the forces for all pairs of bodies and the resulting acceleration for each body is explained.

The final part of the video briefly discusses the code for animating object motions, simply with plot.´

Actual examples of simulations with several number of bodies, Kepler laws and escape velocity are illustrated in video [nbdsim1EN].

*Link to my [ whole collection] of videos in English. Link to larger [ Colección completa] in Spanish.

© 2025, A. Sala. All rights reserved for materials from authors affiliated to Universitat Politecnica de Valencia.
Please consult original source/authors for info regarding rights of materials from third parties.