generates a simulation of the motion of a system of n bodies with initial states statei, governed by the specified potential or force law, over a length of time t.


generates a simulation of the motion of a system of n bodies with names bodyi.

Details and Options

  • NBodySimulation gives an NBodySimulationData object as a result.
  • NBodySimulation by default solves classical nonrelativistic equations of motion.
  • The states statei are given as associations containing the following elements:
  • "Mass"mass
    "Charge"electrical charge (optional)
  • Additional elements used by particular potential or force laws can also be included.
  • Potential or force laws can be given as:
  • "name"named law
    <|"prop1"f1,|>law with specified properties
    funpairwise potential function
  • Possible named laws include:
  • "InverseSquare"unitless inverse square law
    "Harmonic"unitless harmonic potential
    {"Harmonic",params}unitless harmonic potential with parameters
    "Newtonian"Newtonian gravity with physical units
    "Coulomb"Coulomb's law with physical units
  • By default, "Harmonic" uses spring constant 1, equilibrium length 1.
  • {"Harmonic",<|"SpringConstant"->k,"EquilibriumLength"->len|>} uses spring constant k and equilibrium length len.
  • The following keys can be used when giving a law as an association:
  • "PairwisePotential"pairwise potential function between any two bodies
    "PairwiseForce"pairwise force function between any two bodies
    "ExternalPotential"additional potential function applied to each body
    "ExternalForce"additional force function applied to each body
    "Region"region confining the bodies
  • A pairwise potential or force function f is applied for all pairs of bodies i and j in the form f[statei,statej].
  • The values in the initial states specified for each body can be given as quantities with units.


open all close all

Basic Examples  (2)

Simulate two masses attached by a spring:

Click for copyable input
Click for copyable input

Simulate a three-body problem in two dimensions:

Click for copyable input

Get the state for the bodies at time t=4:

Click for copyable input

Plot the positions of the bodies:

Click for copyable input

Scope  (6)

Applications  (1)

Introduced in 2019