Python Plotting the Trajectory of a Spacecraft Through the Solar System using Python

  • Thread starter Thread starter Dustinsfl
  • Start date Start date
AI Thread Summary
The discussion centers on troubleshooting a spacecraft trajectory simulation that is not plotting correctly. The spacecraft is designed to complete three orbits around the Sun in two years and perform a hyperbolic flyby of Earth. The user has provided a Mathematica notebook for detailed calculations and has shared Python code for plotting the trajectory. While the orbits of Earth and Mars are plotted correctly, the elliptical orbit of the spacecraft is flawed. The user suspects a mistake in the Mathematica calculations, particularly regarding the initial conditions and the spacecraft's trajectory, which appears to start perpendicular to Earth's orbit, raising concerns about the kinetic energy involved. The flyby is intended to occur at the apoapsis of the spacecraft's elliptical path, but the resulting trajectory does not reflect an expected elliptical shape. Further investigation into the initial position and velocity parameters, as well as the simulation code, is needed to resolve the issue.
Dustinsfl
Messages
2,217
Reaction score
5
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:
#!/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
 

Attachments

Last edited by a moderator:
Technology news on Phys.org
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?
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top