Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Week 1 starting notebook

%matplotlib widget
import matplotlib.pyplot as plt
import rebound
import time
import numpy as np
plt.close()

# set up the simulation
sim = rebound.Simulation()
sim.add(m=1)
sim.add(m=0.1, x=1, vy=1.3)
sim.move_to_com()

# plot the positions and instantaneous orbit
op = rebound.OrbitPlot(sim, periastron=True)

# output info about the particles and the orbit
for p in sim.particles:
    print(p.x, p.y, p.z)
for o in sim.orbits(): 
    print(o)
    print(o.P)
p_orb = sim.orbits()[0].P

Noutputs = 200
times = np.linspace(0, 2*p_orb, Noutputs)
xvec = np.zeros(Noutputs)
yvec = np.zeros(Noutputs)
xvec[0] = sim.particles[1].x
yvec[0] = sim.particles[1].y
op2, = plt.plot(xvec[:1], yvec[:1])

for i, t in enumerate(times):
    # integrate the next part of the orbit
    op.sim.integrate(t)

    # store the trajectory of particle 2 (index 1)
    xvec[i] = sim.particles[1].x
    yvec[i] = sim.particles[1].y

    # update the plot to animate it
    op.update() # can use updateLimits=True if you want to dynamically update the plot limits
    op2.set_data((xvec[:i+1],yvec[:i+1])) # plot the trajectory so far
    time.sleep(0.001)
    op.fig.canvas.draw()
# this is just a reminder that you can use help() to get info about a function's parameters
help(rebound.OrbitPlot)