File Exchange

image thumbnail

Vectorized N-Body Equation

version 1.0 (3.81 KB) by

Fully vectorized n-body equations of motion in second and first order form.



View License

NBODYVECT - Fully vectorized n-body equation
nbodyVect(x0,dx0,mus,t) integrates the n-body equations of motion using either a symplectic second order integrator, or any of the standard built-in or user supplied first order integrators.

x0 - 3n x 1 stacked initial position vectors:
dx0 - 3n x 1 stacked initial velocity vectors
mus - gravitational parameters (G*m_i) where G is the gravitational constant and m_i is the mass of the ith body.
t - time span: either a 2 element array of initial and final time, or array of all time steps to output.

All units must be complementary, i.e., if positions are in AUs, and time is in days, dx0 must be in AU/day and mus must be in AU^3/day^2 (these are the units in solarSystemData.mat). This data was downloaded from JPL's System Web Interface (

To switch to first order integrator, comment line 61, and uncomment lines 64-65. Line 61 should be replaced with your symplectic integrator of choice, such as rkn86 (this needs modifications to accept arbitrary time arrays).

%integrate solar system for one year
ssdat = load('solarSystemData.mat');
[t,x,dx] = nbodyVect(ssdat.p0,ssdat.v0,ssdat.mus,0:1:365);
%plot first 3 orbits

Comments and Ratings (2)

Eneru Y

It overestimate the effects of gravity compared to JPL ephemeris about solar system simulation (same initial conditions). Even symplectic second order integrator "rkn1210".

sun Chong

MATLAB Release
MATLAB 7.9 (R2009b)

Inspired by: rkn86

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today