# NBodySimulation

NBodySimulation[law,{state1,,staten},t]

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.

NBodySimulation[law,<|body1state1,,bodynstaten|>,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 "Position" position "Velocity" velocity "Charge" electrical charge (optional) "PositionVariable" variable used for position (optional) "MomentumVariable" variable used for momentum (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 fun pairwise 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.

# Examples

open allclose all

## Basic Examples(2)

Simulate two masses attached by a spring:

Simulate a three-body problem in two dimensions:

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

Plot the positions of the bodies:

## Scope(8)

Simulate a four-body problem in three dimensions:

Plot the positions of the bodies:

Show how the energy evolves during the simulation:

Show the equations for the simulation:

Show the equations in Hamiltonian form:

Simulate three named bodies attached by springs in 2D:

Show how the vertical position of "bill" evolves with time:

Show how the horizontal positions of "sam" and "george" evolve with time:

Make an animation showing the motion:

Simulate a three-body problem confined to a disk in two dimensions:

Plot the positions of the bodies:

Simulate the interaction of two charged particles:

When two particles collide, the simulation typically stops: Get the collision time:

Plot the path of the particles up to the collision time:

Simulate a ring of masses with neighbors connected by springs:

Plot the positions of the masses:

Show the spring lengths as a function of time:

Use "PairwiseForce" to model an elastic collision between two spheres of radius 1:

Show the position of the center of the spheres as a function of time:

Specify the variables for position and momentum:

This only affects the equations:

## Applications(1)

Simulate the Sun-Earth-Moon system:

Use heliocentric coordinates and choose initial positions from 2018:

Estimate the velocity by advancing a day:

Simulate the system:

Show the orbit of the Earth:

Show the orbit of the Moon relative to the Earth:

Show the Earth-Moon distance as a function of time: