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

  • Python
  • Thread starter Dustinsfl
  • Start date
In summary, the conversation discusses a problem with a spacecraft's orbit not plotting correctly and a code used to solve the three-body problem numerically and plot trajectories. There is also mention of a Mathematica notebook and non-dimensionalized units used in calculations. The issue of the spacecraft's starting orbit being perpendicular to Earth's orbit is also brought up.
  • #1
Dustinsfl
2,281
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

  • hw8problem3.nb
    29.4 KB · Views: 513
Last edited by a moderator:
Technology news on Phys.org
  • #2
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?
 
  • #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.
 

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

What is Python?

Python is a high-level, interpreted programming language that is widely used for various applications such as web development, data analysis, and artificial intelligence. It is known for its simple and readable syntax, making it an ideal language for beginners to learn.

What is an orbital question in Python?

An orbital question in Python refers to a type of question or problem that involves calculating and simulating the motion of objects in space. This can include calculating the trajectory of a satellite, predicting the position of planets, or simulating the orbits of celestial bodies.

What are some key features of Python that make it suitable for orbital questions?

Python has a rich library of scientific and mathematical modules, such as NumPy and SciPy, that make it easy to perform complex calculations and simulations. It also has a simple and flexible syntax that allows for quick prototyping and experimentation with different algorithms.

Can Python be used for real-world orbital calculations?

Yes, Python can be used for real-world orbital calculations and has been used by organizations such as NASA and SpaceX for this purpose. Its powerful libraries and easy-to-use syntax make it a popular choice for scientific and engineering applications.

Is Python the only language used for orbital questions?

No, there are other programming languages that can be used for orbital questions, such as C++, Java, and MATLAB. However, Python's simplicity and versatility make it a popular choice for these types of calculations.

Similar threads

Replies
2
Views
2K
Replies
6
Views
1K
Replies
5
Views
2K
Replies
17
Views
2K
Replies
15
Views
2K
Replies
1
Views
3K
Back
Top