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

  • Context: Python 
  • Thread starter Thread starter Dustinsfl
  • Start date Start date
Click For Summary
SUMMARY

This discussion focuses on plotting the trajectory of a spacecraft through the solar system using Python, specifically addressing issues with an elliptical orbit calculation. The spacecraft is designed to complete three orbits around the Sun in two years, culminating in a hyperbolic flyby of Earth. The user employs a two-body simulation code with Python libraries such as NumPy, SciPy, and Matplotlib to visualize the trajectory. Key concerns include the initial conditions of the orbit and the unexpected behavior of the spacecraft's trajectory post-launch.

PREREQUISITES
  • Understanding of two-body orbital mechanics
  • Familiarity with Python programming, specifically using NumPy and SciPy
  • Experience with plotting in Python using Matplotlib
  • Knowledge of gravitational assists and hyperbolic trajectories
NEXT STEPS
  • Investigate the principles of two-body problem simulations in orbital mechanics
  • Learn about hyperbolic trajectories and gravitational assists in space missions
  • Explore advanced plotting techniques in Matplotlib for 3D visualizations
  • Review the use of non-dimensionalization in orbital simulations
USEFUL FOR

Aerospace engineers, astrophysicists, and Python developers interested in orbital mechanics and spacecraft trajectory simulations will benefit from this discussion.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K