1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python/orbital question

  1. Apr 17, 2013 #1
    I can't figure out why one of my orbits isn't plotting correctly. Here is first part of the question.
    This has already been turned in

    A spacecraft is launched from Earth into an orbit about the Sun such that the spacecraft will make
    a precisely three orbits in two years; it will thus make a hyperbolic flyby of Earth two years after
    the initial launch.

    Using the position and velocity information immediately after flyby in problem 3, use your two-
    body simulation code developed earlier this semester to calculate and plot the resulting spacecraft
    trajectory within the solar system (assuming no further planetary interactions occur). To provide a
    reference, include the circular orbits of Earth and Mars on this same plot.


    I am going to attach a Mathematica notebook that answers all the important info which is extensive. So the notebook is better than typing it all out.

    I then used Distance Units ant Time Units to nondimensionalize the speed, distance, and time for Earth, Mars, and elliptical orbit. Earth and Mars are fine.

    My elliptical orbit is wrong. There is probably a mistake in solving the problem in the Mathematica file but I don't know what I did wrong.

    Below is my python code for the plotting:
    Code (Text):

    #!/usr/bin/env python                                                              
    #  This program solves the 3 Body Problem numerically and plots the                
    #  trajectories in non-dimensionalized units                                                                  

    import numpy as np
    from scipy.integrate import odeint
    import matplotlib.pyplot as plt
    from numpy import linspace
    from mpl_toolkits.mplot3d import Axes3D

    mu = 1.0
    # r0 = [-149.6 * 10 ** 6, 0.0, 0.0]  #  Initial position                          
    # v0 = [29.9652, -5.04769, 0.0]      #  Initial velocity                          
    # u0 = [-149.6 * 10 ** 6, 0.0, 0.0, 29.9652, -5.04769, 0.0]                        
    u0 = [-1.0, 0.0, 0.0, 0.993545, -0.2, 0.0]
    e0 = [1.0, 0.0, 0.0, 0.0, 1.0, 0.0]
    m0 = [1.53, 0.0, 0.0, 0.0, 1.23152, 0.0]

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


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


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

    dt = np.arange(0.0, 3 * np.pi, .01)   #  Time to run code in seconds'            
    u = odeint(deriv, u0, dt)
    e = odeint(deriv2, e0, dt)
    m = odeint(deriv3, m0, dt)
    x, y, z, x2, y2, z2 = u.T
    x3, y3, z3, x4, y4, z5 = e.T
    x6, y6, z6, x7, y7, z7 = m.T

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.plot(x3, y3, z3)
    ax.plot(x, y, z)
    ax.plot(x6, y6, z6)

    plt.axis((-1.7, 1.7, -1.7, 1.7))

    plt.show()

     
    Here is a the output:
    http://img600.imageshack.us/img600/1580/hw8problem4.png [Broken]
     

    Attached Files:

    Last edited by a moderator: May 6, 2017
  2. jcsd
  3. Apr 17, 2013 #2

    I like Serena

    User Avatar
    Homework Helper

    Hi Dustinsfl!

    I haven't tried to figure out anything you might have done wrong, but it seems to me a bit odd that the spacecraft's orbit would start perpendicular to earth's orbit.
    The orbit itself seems okay after its launch, but such a jump in kinetic energy seems unreasonable to me.
    Do you have any clue why that is?
     
  4. Apr 17, 2013 #3
    The spacecraft doesn't start from Earth. It was in an elliptical orbit about the Sun. In 2 Earth years, the spacecraft makes 3 orbits. At that time, it use Earth for a Hyperbolic flyby--gravitational assist. It then take another elliptical path but from the green path we see that it isnt' taking an ellipse.

    The flyby occurs at apoapsis of the 2 year 3 revolutions ellipse which is -149.6 x 10^6 km in the x and 0 in the y.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Python/orbital question
  1. Python question (Replies: 2)

  2. Python question (Replies: 1)

  3. Newbie python question (Replies: 4)

  4. Python 3 Question (Replies: 3)

Loading...