def orbit(R_0, V_0, M, tSpan):
    """ Compute the position and velocity of m1 relative to m2.

    m1 has a non-rotating cartesian coordinate frame.

    Parameters
    ----------
    R_0 : numpy.array
        Initial position of m1 relative to m2 (km).
    V_0 : numpy.array
        Initial velocity of m1 relative to m2 (km/s).
    M : numpy.array
        Vector of masses (kg) in the form [m1 m2].
    tSpan : numpy.array
        Range of times to solve for.

    Returns
    -------
    ys : numpy.array
        State time response.
    ts : numpy.array
        Time vector.
    """
    Y_0 = np.concatenate((R_0, V_0))

    # Create anonymous function to pass m1 and m2
    def rates(t, Y):
        return orbit_rates(t, Y, M[0], M[1])

    ys, ts = rkf45(rates, Y_0, tSpan)

    return (ys, ts)
def two_body_3d(R1_0, R2_0, V1_0, V2_0, m1, m2, tSpan=np.array([0., 10.0])):
    """ Compute the position and velocity of two bodies in 3D over time.

    Parameters
    ----------
    R1_0 : numpy.array
        Initial position of the first body.
    R2_0 : numpy.array
        Initial position of the second body.
    V1_0 : numpy.array
        Initial velocity of the first body.
    V2_0 : numpy.array
        Initial velocity of the second body.
    m1 : float
        Mass of the first body (kg).
    m2 : float
        Mass of the second body (kg).
    tSpan : numpy.array
        Range of times to solve for.

    Returns
    -------
    ys : numpy.array
        State time response.
    ts : numpy.array
        Time vector.
    """
    Y0 = np.concatenate((R1_0, R2_0, V1_0, V2_0))

    # Create anonymous function to pass m1 and m2
    def rates(t, Y):
        return two_body_3d_rates(t, Y, m1, m2)

    ys, ts = rkf45(rates, Y0, tSpan)

    return (ys, ts)
def n_body_3d(R_0, V_0, M, tSpan=np.array([0., 10.0])):
    """ Compute the position and velocity of N bodies in 3D over time.

    Parameters
    ----------
    R_0 : numpy.array
        Vector of initial positions of the N bodies in the form
        [x1 y1 z1 x2 y2 z2 ...]
    V_0 : numpy.array
        Vector of initial velocities of the N bodies in the form
        [vx1 vy1 vz1 vx2 vy2 vz2 ...]
    M : float
        Vector of masses (kg) in the form
        [m1 m2 m3 ...]
    tSpan : numpy.array
        Range of times to solve for.

    Returns
    -------
    ys : numpy.array
        State time response.
    ts : numpy.array
        Time vector.
    """
    n = M.shape[0]
    Y0 = np.concatenate(
        (np.reshape(R_0, (n * 3, ),
                    order='F'), np.reshape(V_0, (n * 3, ), order='F')))

    # Create anonymous function to pass m1 and m2
    def rates(t, Y):
        return n_body_3d_rates(t, Y, M)

    ys, ts = rkf45(rates, Y0, tSpan)

    return (ys, ts)
    F = np.zeros(Y.shape)
    F[0] = -0.5 * Y[1] + Y[2]
    F[1] = 0.5 * Y[0] - Y[2] / np.sqrt(2.)
    F[2] = -0.5 * Y[0] + Y[1] / np.sqrt(2.)
    return F


# Title
print("Orbital Mechanics for Engineering Students Problem 1.21")

# Parameters
tSpan = np.array([0., 20.])
Y0 = np.array([1., 0., 0.])

# Solve numerically
y, t = rkf45(rates, Y0, tSpan)
y, t = rk14(rates, Y0, tMax=20, h=1e-4)

# Show answer
print(f"x({t[-1]:.3f}) = {y[-1,0]:.3f}")
print(f"y({t[-1]:.3f}) = {y[-1,1]:.3f}")
print(f"z({t[-1]:.3f}) = {y[-1,2]:.3f}")

# Plot answer
plt.figure()
plt.plot(t, y[:, 0], label="x")
plt.plot(t, y[:, 1], label="y")
plt.plot(t, y[:, 2], label="z")
plt.xlabel("Time (-)")
plt.ylabel("Value (-)")
plt.legend()
Beispiel #5
0
Numerically solve the forth order-differential equation
    d^4y/dt^4 + 2d^2y/dt^2 + y = 0
for y at t = 20, if the initial conditions at t=0 are:
    - y = 1
    - dy/dt = d^2y/dt^2 = d^3y/dt^3 = 0
Written by: J.X.J. Bannwarth
"""
import numpy as np
from orbitutils.solvers import rkf45


# Differential equations
def Rates(t, Y):
    F = np.zeros(Y.shape)
    F[0:3] = Y[1:]
    F[3] = -2. * Y[2] - Y[0]
    return F


# Title
print("Orbital Mechanics for Engineering Students Problem 1.18")

# Parameters
tSpan = np.array([0., 20.])
Y0 = np.array([1., 0., 0., 0.])

# Solve numerically
y, t = rkf45(Rates, Y0, tSpan)

# Show answer
print(f"y({t[-1]:.3f}) = {y[-1,0]:.3f}")
    rE = 6378000  # m/s^2

    f = np.zeros((2,))
    f[0] = y[1]
    f[1] = - g0*rE**2/y[0]**2
    return f


# Title
print("Orbital Mechanics for Engineering Students Example 1.20")

# Parameters
tSpan = np.array([0, 70.*60.])
Y0 = np.array([6500000., 7800.])

y, t = rkf45(SpacecraftRates, Y0, tSpan)

# Plot results - Fig. 1.26
fig, ax = plt.subplots(2)
# Position
ax[0].grid()
ax[0].plot(t/60., y[:, 0]/1000., "o-", color="black",
           markerfacecolor='none', label=f"RKF")
ax[0].set_xlabel("Time (min)")
ax[0].set_ylabel("Position (km)")
ax[0].set_xlim((0., 70.))
ax[0].set_ylim((0.5e4, 1.5e4))
mf = matplotlib.ticker.ScalarFormatter(useMathText=True)
mf.set_powerlimits((-1,1))
ax[0].yaxis.set_major_formatter(mf)
ax[0].set_yticks(ticks=[0.5e4,1e4,1.5e4])