plotting orbital trajectories in python

前端 未结 1 909
时光取名叫无心
时光取名叫无心 2021-01-06 10:12

How can I setup the three body problem in python? How to I define the function to solve the ODEs?

The three equations are
x\'\' = -mu / np.sqrt(x ** 2 + y

相关标签:
1条回答
  • 2021-01-06 10:46

    As you've shown, you can write this as a system of six first-order ode's:

    x' = x2
    y' = y2
    z' = z2
    x2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * x
    y2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * y
    z2' = -mu / np.sqrt(x ** 2 + y ** 2 + z ** 2) * z
    

    You can save this as a vector:

    u = (x, y, z, x2, y2, z2)
    

    and thus create a function that returns its derivative:

    def deriv(u, t):
        n = -mu / np.sqrt(u[0]**2 + u[1]**2 + u[2]**2)
        return [u[3],      # u[0]' = u[3]
                u[4],      # u[1]' = u[4]
                u[5],      # u[2]' = u[5]
                u[0] * n,  # u[3]' = u[0] * n
                u[1] * n,  # u[4]' = u[1] * n
                u[2] * n]  # u[5]' = u[2] * n
    

    Given an initial state u0 = (x0, y0, z0, x20, y20, z20), and a variable for the times t, this can be fed into scipy.integrate.odeint as such:

    u = odeint(deriv, u0, t)
    

    where u will be the list as above. Or you can unpack u from the start, and ignore the values for x2, y2, and z2 (you must transpose the output first with .T)

    x, y, z, _, _, _ = odeint(deriv, u0, t).T
    
    0 讨论(0)
提交回复
热议问题