Gravity-Assist Trajectory Analysis (MATLAB)
Gravity-Assist Trajectory Analysis (MATLAB)
This document describes a MATLAB script named flyby_matlab that can be used to design and
optimize interplanetary patched-conic trajectories that include a single gravity assist maneuver. The
user specifies the launch, flyby and destination planets along with initial guesses for the launch, flyby
and arrival calendar dates, and lower and upper bounds for the flyby altitude. This script searches for a
patched-conic gravity-assist trajectory that satisfies the flyby mission constraints (V matching and
flyby altitude) and minimizes the launch, arrival or total impulsive heliocentric delta-v for the mission.
The type of trajectory optimization is specified by the user.
The planet positions and velocities are modeled using the JPL DE 421 ephemeris. Information about
JPL ephemerides can be found at http://naif.jpl.nasa.gov/naif/. The trajectory optimization is performed
with the SNOPT nonlinear programming (NLP) algorithm. SNOPT mex files for several computer
platforms can be found at http://ccom.ucsd.edu/~optimizers/.
The flyby_matlab software provides a simple two-dimensional graphic display of the heliocentric
planet orbits and the interplanetary transfer trajectory. It also provides a three-dimensional graphic
display of the flyby trajectory.
The following is typical user interaction with this MATLAB application. This example is an Earth with
Venus gravity-assist to Mars mission. The user inputs for this example are in bold font.
The software allows the user to specify an initial guess for the launch, flyby and arrival calendar dates,
and lower and upper bounds on the actual launch, flyby and arrival calendar dates found during the
optimization process. For any guess for launch time t L and user-defined launch time lower and upper
bounds tl and tu , the launch time t is constrained as follows:
tL tl t tL tu
Likewise, for any guess for arrival time t A and user-defined arrival time bounds tl and tu , the arrival
time t is constrained as follows:
t A tl t t A tu
For fixed launch, flyby or arrival times, the lower and upper bounds should be set to 0.001.
The following is a typical user interaction with the flyby_matlab script. It defines one of the classical
JPL Earth-Venus-Mars missions analyzed during the 1960s.
program flyby_matlab
planet menu
<1> Mercury
<2> Venus
<3> Earth
<4> Mars
<5> Jupiter
<6> Saturn
<7> Uranus
<8> Neptune
<9> Pluto
planet menu
<1> Mercury
<2> Venus
<3> Earth
<4> Mars
<5> Jupiter
<6> Saturn
<7> Uranus
<8> Neptune
<9> Pluto
planet menu
<1> Mercury
<2> Venus
<3> Earth
page 2
<4> Mars
<5> Jupiter
<6> Saturn
<7> Uranus
<8> Neptune
<9> Pluto
please input the lower bound for the flyby altitude (kilometers)
? 500
please input the upper bound for the flyby altitude (kilometers)
? 5000
optimization menu
selection (1, 2 or 3)
? 1
This section summarizes the program output for this example. The software provides the heliocentric
orbital elements and state vectors of each leg of the transfer trajectory in the Earth mean ecliptic and
equinox of J2000 coordinate system. These results also include the characteristics of the hyperbolic
flyby trajectory with respect to the flyby planet as well as the trajectory characteristics at the time of
entrance to the sphere-of-influence (SOI) and closest approach relative to the flyby planet. The time
scale is Barycentric Dynamical Time (TDB). See Appendix A for a description of this information.
program flyby_matlab
--------------------
LAUNCH CONDITIONS
=================
page 3
launch delta-vx -1520.218236 meters/second
launch delta-vy -2163.716837 meters/second
launch delta-vz 1903.009119 meters/second
ARRIVAL CONDITIONS
==================
MISSION SUMMARY
===============
page 6
total mission duration 310.191489 days
page 7
b-plane coordinates at closest approach
(Earth mean ecliptic and equinox of J2000)
------------------------------------------
After the solution is displayed, the software will ask the user if he or she would like to create a graphics
display of the transfer and flyby trajectories with the following prompt:
would you like to create graphics for this mission (y = yes, n = no)
?
If the user’s response is y for yes, the script will request a plot step size with
please input the plot step size (days)
?
The following is the heliocentric graphics display for this example. This plot is a north ecliptic view
where we are looking down on the ecliptic plane from the north celestial pole. The vernal equinox
direction is the labeled line pointing to the right, the launch planet is labeled with an L, the flyby planet
is labeled with an F and the arrival planet is labeled with an A. The locations of the launch, flyby and
arrival planets at the launch time are marked with an asterisk. Please note that the x and y axes are
labeled in Astronomical Units (AU).
page 8
The following is the flyby graphics display for this example. The direction of the sun is the yellow line
and the black line points in the direction of the heliocentric velocity of the flyby planet. This display is
labeled with a planet-centered, inertial mean ecliptic and equinox of J2000 coordinate system. The x-
axis of this system is red, the y-axis is green and the z-axis is blue. The flyby hyperbola is colored
magenta with the incoming leg marked with an asterisk and periapsis labeled with a small circle. Please
note that the axes are labeled in radii of the flyby planet (PR).
This script will also create disk files of these graphic displays. These files are color Postscript images
with a TIFF preview. The name and other characteristics of these files can be edited in the following
lines of MATLAB source code;
print -depsc -tiff -r300 flyby_matlab1.eps
Technical discussion
The computational steps used to create an initial guess for the spacecraft state, and the launch and arrival
delta-v characteristics are as follows:
(1) compute the state vector of the departure planet at the departure date initial guess
(2) compute the state vector of the flyby planet at the flyby date initial guess
(3) compute the state vector of the arrival planet at the arrival date initial guess
(4) solve Lambert's problem for the departure-to-flyby leg and determine the initial velocity vector
of this leg
page 9
(5) compute the launch delta-v vector from the launch planet’s velocity vector and the initial
velocity vector of the first leg of the transfer trajectory
(6) solve Lambert's problem for the second heliocentric leg and determine the initial and final
velocity vectors of the second leg
(7) compute the arrival delta-v vector from the arrival planet’s velocity vector and the final velocity
vector of the second leg of the transfer trajectory
(8) estimate the flyby incoming v-infinity vector from the flyby planet’s velocity vector and the final
velocity vector of the first leg of the transfer trajectory
Lambert’s Problem
Lambert’s problem is concerned with the determination of an orbit that passes between two positions
within a specified time-of-flight. This classic astrodynamics problem is also known as the orbital two-
point boundary value problem (TPBVP).
The time to traverse a trajectory depends only upon the length of the semimajor axis a of the transfer
trajectory, the sum ri rf of the distances of the initial and final positions relative to a central body, and
the length c of the chord joining these two positions. This relationship can be stated as follows:
tof tof ri rf , c, a
a3
t t0 E e sin E
we can write
a3
t E E0 e sin E sin E0
where E is the eccentric anomaly associated with radius r, E0 is the eccentric anomaly at r0 , and t 0
when r r0 .
At this point we need to introduce the following trigonometric sun and difference identities:
a
sin sin 2sin cos
2 2
a
cos cos 2sin sin
2 2
a
cos cos 2 cos cos
2 2
page 10
If we let E and E0 and substitute the first trig identity into the second equation above, we have
the following equation:
a3 E E0 E E0
t E E0 2sin e cos
2 2
a3
t 2sin cos
2 2
r a 1 e cos E
x a cos E e
y a sin E 1 e 2
r r0 c r r0 c s
cos 1 1 1
2a 2a 2a a
r r0 c r r0 c sc
sin 1 1 1
2a 2a 2a a
This part of the derivation makes use of the following three relationships:
r r0
cos cos 1
2 2 2
E E0 E E0
2
sin sin sin 1 e cos
2 2 2 2
x x0 y y0 c
2 2 2 2
sin sin
2 2 2a 2a 2a
page 11
s sc
sin sin
2 2a 2 2a
and several additional substitutions, we have the time-of-flight form of Lambert’s theorem
a3
t sin sin
A discussion about the angles and can be found in “Geometrical Interpretation of the Angles and
in Lambert’s Problem” by J. E. Prussing, AIAA Journal of Guidance and Control, Volume 2,
Number 5, Sept.-Oct. 1979, pages 442-443.
The Lambert algorithm used in this MATLAB script is based on the method described in “A Procedure
for the Solution of Lambert’s Orbital Boundary-Value Problem” by R. H. Gooding, Celestial Mechanics
and Dynamical Astronomy 48: 145-165, 1990. This iterative solution is valid for elliptic, parabolic and
hyperbolic transfer orbits which may be either posigrade or retrograde, and involve one or more
revolutions about the central body.
The vector relationship between the incoming v-infinity vector v , the outgoing v-infinity vector v and
the two legs of the heliocentric transfer orbit are as follows:
v v f b v to1
v v to2 v f b
where
The turn angle of the planet-centered trajectory during the flyby is determined from
1 1 v v
1
sin 2sin 1
2 2 v v
1 rp v /
2
where rp is the periapsis radius of the flyby hyperbola, v is the magnitude of the incoming (or
outgoing) v-infinity vector and is the gravitational constant of the flyby planet.
The maximum turn angle possible during a gravity assist flyby occurs when the spacecraft just gazes the
planet’s surface. It is given by
page 12
1
max 2sin 1
1 rp v2 /
The semimajor axis and orbital eccentricity of the flyby hyperbola are given by
a / v / v
2 2
rp rp v2
e 1/ cos 1 1
a
where is the true anomaly at infinity which is determined from the following expression:
1 1 v v
sin
2 2 v v
The periapsis radius of the flyby hyperbola is determined from the expression r a 1 e and the flyby
altitude is h r rs where rs is the radius of the flyby planet..
The heliocentric speed gained during the flyby and the heliocentric delta-v vector caused by the close
encounter can be determined from the following two equations:
v fb 2v / e
v fb v h v h
In the second equation v h is the heliocentric velocity vector of the spacecraft prior to the flyby and v h is
the heliocentric velocity vector after the flyby. For any planet it can be shown that the maximum
heliocentric delta-v possible from a closes flyby is given by the expression
vmax
rs
This corresponds to a “grazing” flyby at the planet’s surface and is equal to the “local circular velocity”
for the flyby planet at the surface.
During the optimization analysis, the software enforces the following two nonlinear equality point
constraints:
v v 0
hl h f b hu
page 13
The first equation is the v-infinity matching equality constraint and the second equation is the flyby
altitude inequality constraint. In the second expression h f b is the actual flyby altitude computed by the
software, hl is the user-defined lower bound, and hu is the user-defined upper bound for the flyby
altitude. To enforce a “fixed” flyby altitude, set hl hu .
Departure trajectory
The orientation of the departure hyperbola is specified in terms of the right ascension and declination of
the asymptote of the launch hyperbola. These coordinates can be calculated using the components of the
planet-centered departure unit v-infinity vector.
tan 1 vˆ , vˆ
y x
and the geocentric declination of the asymptote is given by
In a typical “targeting spec” for an interplanetary mission, the right ascension is called RLA and the
declination is called DLA. The corresponding launch energy is called C3 which is equal to twice the
specific (per unit mass) orbital energy of the trajectory. The actual geocentric hyperbolic injection
delta-v depends on the size, orientation and other characteristics of the Earth park orbit.
In this MATLAB script the heliocentric planetary coordinates and therefore the v vectors are
computed in the J2000 mean ecliptic and equinox coordinate system. In order to determine the
orientation of the geocentric departure hyperbola, the heliocentric delta-v vector must be transformed to
the Earth mean equator and equinox of J2000 (EME2000) frame.
1 0 0 1 0 0
v eq 0 cos sin v ec 0 0.917482062069182 -0.397777155931914 v ec
0 sin cos 0 0.397777155931914 0.917482062069182
where v ec is the delta-velocity vector in the ecliptic frame, v eq is the delta-velocity vector in the
Earth equatorial frame and is the mean obliquity of the ecliptic at J2000.
The J2000 value of the mean obliquity of the ecliptic is equal to 23 2621.448 .
The conversion of coordinates from the equatorial system to the ecliptic system involves the transpose
of this fundamental matrix.
page 14
The B-plane
The derivation of B-plane coordinates is described in the classic JPL reports, “A Method of Describing
Miss Distances for Lunar and Interplanetary Trajectories” and “Some Orbital Elements Useful in Space
Trajectory Calculations”, both by William Kizner. The following diagram illustrates the fundamental
geometry of the B-plane coordinate system.
cos cos
Sˆ cos sin
sin
where and are the declination and right ascension of the asymptote of the incoming or outgoing
hyperbola.
The following computational steps summarize the calculation of the predicted B-plane vector from a
planet-centered position vector r and velocity vector v at the sphere-of-influence or closest approach.
page 15
semiparameter
p h2
semimajor axis
r
a
r v2
2
orbital eccentricity
e 1 p a
true anomaly
pr rh
cos sin
er e
B-plane magnitude
B pa
fundamental vectors
r v rr
zˆ
h
S vector
a b
S pˆ qˆ
a 2 b2 a 2 b2
B vector
b2 ab
B pˆ qˆ
a 2 b2 a 2 b2
T vector
S , S ,0
2 2 T
T
y x
S x2 S y2
R vector
R S T SzTy , SzTx , S xTy S yTx
T
page 16
Calculating Sphere-of-Influence Entrance and Closest Approach
This section describes the numerical methods used to predict the time at which the spacecraft enters the
sphere-of-influence (SOI) of the flyby planet and the time of closest approach to the flyby planet. These
calculations can be used to assess the difference between the patched-conic and numerically integrated
trajectory for the flyby hyperbola.
rp sc rSOI
where rp sc is the radius vector of the spacecraft relative to the flyby planet and rSOI is the SOI radius of
the flyby planet. The SOI radius is a function of the gravity of the flyby planet.
During the search for SOI conditions, the heliocentric equations of motion of the spacecraft subject to
the point-mass gravity of the sun are defined by
rs sc
a s 3
rs sc
where s is the gravitational constant of the sun and rs sc is the heliocentric position vector from the sun
to the spacecraft.
The following is the MATLAB source code from the main script that predicts SOI entrance. It uses the
root-finder or events option of the built-in ODE45 function to evaluate the two-body heliocentric
equations of motion defined in a MATLAB function named twobody_eqm while searching for the time
at which the flyby planet-to-spacecraft distance equals the SOI radius of the flyby planet.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% two-body integration of the trajectory from
% first impulse to SOI entrance at flyby planet
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% solve for flyby planet SOI entrance conditions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The following is the MATLAB source code that computes the scalar value of the SOI objective
function.
page 17
function [value, isterminal, direction] = soi_event(t, y)
% input
% output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute the position and velocity of the flyby planet (km & km/sec)
rsc = y(1:3);
vsc = y(4:6);
isterminal = 1;
direction = [];
The software uses a similar technique to predict the spacecraft flight conditions at closest approach to
the flyby planet. The flyby planet-centered initial conditions for the search are determined from the
state vector of the spacecraft at SOI entrance.
For close approach prediction, the script uses the following objective function
rp sc v p sc
sin
rp sc v p sc
which is simply the sine of the flight path angle . Since the spacecraft’s orbit within the SOI is
hyperbolic, periapsis occurs whenever the flight path angle is zero.
page 18
During the search for closest approach conditions, the flyby planet-centered equations of motion subject
to the point-mass gravity of the sun and the flyby planet are computed using
r r
rp sc
a a p as p 3
s s sc 3 p 3
rp sc r
s sc rp
In this equation, a is the total acceleration on the spacecraft and rs sc is the heliocentric position vector
of the spacecraft given by
rssc rpsc rp
These calculations are performed by a MATLAB function named peqm. Here is the source code.
% input
% output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% get current flyby planet heliocentric state vector (km & km/sec)
rp2s(1) = -rs2p(1);
rp2s(2) = -rs2p(2);
rp2s(3) = -rs2p(3);
rp2sm = norm(rp2s);
for i = 1:1:3
page 19
rs2sc(i) = y(i) + rs2p(i);
end
rs2scm = norm(rs2sc);
rmag = norm(y(1:3));
ydot(1) = y(4);
ydot(2) = y(5);
ydot(3) = y(6);
ydot = ydot';
Here’s the MATLAB source code that performs the closest approach calculations.
In this code, rp2sc and vp2sc represent the flyby planet-centered position and velocity vectors at
entrance to the SOI.
page 20
The MATLAB source code that evaluates the flight path event function is as follows.
% input
% output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rsc = y(1:3);
vsc = y(4:6);
isterminal = 1;
direction = [];
This section provides details about the part of the flyby_matlab MATLAB script that solves this
nonlinear programming (NLP) problem using the SNOPT algorithm. In this classic trajectory
optimization problem, the launch, flyby and arrival calendar dates are the control variables and the user-
specified scalar V is the objective function or performance index.
The SNOPT algorithm requires an initial guess for the control variables. For this problem they are given
by the following code
xg = xg';
page 21
where jdtdbi1, jdtdbi2 and jdtdbi3 are the initial user-provided launch, flyby and arrival calendar
date guesses, and jdtdb0 is a reference Julian date equal to 2451544.5 (January 1, 2000 TDB). This
offset value is used to scale the control variables.
The algorithm also requires lower and upper bounds for the control variables. These are determined
from the user-defined initial guesses and search boundaries as follows:
xlwr = xlwr';
xupr = xupr';
xlwr = xlwr';
xupr = xupr';
where ddays1, ddays2 and ddays3 are the user-defined launch, flyby and arrival search boundaries,
respectively.
The algorithm requires lower and upper bounds on the objective function. For this problem these
bounds are given by
% bounds on objective function
flow(1) = 0.0d0;
fupp(1) = +Inf;
flow(2) = fbalt_lwr;
fupp(2) = fbalt_upr;
flow(3) = 0.0;
fupp(3) = 0.0;
In this code fbalt, vinfm_in, and vinfm_out are the current computed flyby altitude, incoming and
outgoing v-infinity speeds, respectively. fbalt_lwr and fbalt_upr are the user-defined lower and
upper bounds on the flyby altitude.
page 22
where fbyfunc is the name of the MATLAB function that solves Lambert’s problem for both legs of
the interplanetary transfer trajectory and computes the current value of the objective function.
Algorithm resources
“Gravity-Assist Trajectory Design with MATLAB”, C. David Eagle, AAS 99-138, AAS/AIAA Space
Flight Mechanics Meeting, Breckenridge, CO, February 7-10, 1999.
“Optimal Interplanetary Orbit Transfers by Direct Transcription”, John T. Betts, The Journal of the
Astronautical Sciences, Vol. 42, No. 3, July-September 1994, pp. 247-268.
“Modern Astrodynamics”, Victor R. Bond and Mark C. Allman, Princeton University Press, 1996.
“Automated Design of Gravity-Assist Trajectories to Mars and the Outer Planets”, J.M. Longuski and
S.N. Williams, Celestial Mechanics and Dynamical Astronomy, 52: 207-220, 1991.
“Notes for the Gravitational Assisted Trajectories Lectures”, E. Barrabes, G. Gomez, and J. Rodriquez-
Canabal, Advanced Topics in Astrodynamics Summer Course, Barcelona, July 2004.
“Error Analysis of Multiple Planet Trajectories”, F. M. Sturms Jr., JPL Space Programs Summary, No.
37-27, Vol. IV.
“Trajectory Analysis of a 1970 Mission to Mercury via a Close Encounter with Venus”, E. Cutting, F.
M. Sturms Jr., AIAA Journal of Spacecraft and Rockets, Vol.3, No. 5, pp. 624-631, 1966.
“A Graphical Method for Gravity-assist Trajectory Design”, Nathan Strange, James Longuski,
Astrodynamics Specialist Conference, 2000.
“Mars Free Returns via Gravity Assist from Venus”, Masataka Okutsu, James Longuski, Astrodynamics
Specialist Conference, 2000.
“Application of Tisserand’s Criterion to the Design of Gravity Assist Trajectories”, James Miller,
Connie Weeks, AIAA/AAS Astrodynamics Specialist Conference and Exhibit, 2002.
“Fast Mars Free-Returns via Venus Gravity Assist”, Kyle M. Hughes, Peter J. Edelman, James
Longuski, Mike Loucks, John P. Carrico, Dennis A. Tito, AIAA/AAS Astrodynamics Specialist
Conference, 2014.
page 23
APPENDIX A
Contents of the Simulation Summary
This appendix is a brief summary of the information contained in the simulation summary screen
displays produced by the flyby_matlab software.
TDB julian date = julian date of trajectory event on TDB time scale
arglat (deg) = argument of latitude in degrees. The argument of latitude is the sum of
true anomaly and argument of perigee.
vmag (kps) = scalar magnitude of the spacecraft’s velocity vector in kilometers per
second
page 24
theta = orientation of the b-plane vector in degrees
page 25
APPENDIX B
Classical Orbital Elements of a Hyperbolic Flyby Orbit
This Appendix describes the MATLAB function and mathematical equations used to determine the
classical orbital elements of a hyperbolic flyby orbit at periapsis of the trajectory. The inputs to this
MATLAB function are the incoming and outgoing v-infinity velocity vectors, the periapsis radius of the
flyby hyperbola, and the gravitational constant of the flyby planet.
% input
% output
Algorithm equations
The semimajor axis can be determined from the gravitational constant of the flyby planet and the scalar
magnitude of the flyby v-infinity vector according to the expression
a
v2
rp rp v2
e 1 1
a
A unit vector parallel to the incoming asymptote is given by this next equation
v
sˆ
v
page 26
A unit vector perpendicular to the orbit plane of the flyby hyperbola is calculated from
v v
ˆ
w
v v
The orbital inclination relative to the ecliptic plane is determined from the z-component of this unit
vector as follows
i cos1 wz
A unit vector along the z-axis of the ecliptic coordinate system is given by
uˆ z 0,1,1
T
nˆ uˆ z w
ˆ
the right ascension of the ascending node can be computed using a four quadrant inverse tangent
function as follows
tan 1 n y , nx
A unit vector in the direction of the periapsis of the flyby hyperbola is given by
where
cos 1/ e
sin 1 1/ e2
and bˆ sˆ w
ˆ . In these expressions is the orbital true anomaly at infinity.
cos nˆ pˆ
sin 1 cos2
If pz 0 then sin sin . Finally, the argument of periapsis is determined from a four quadrant
inverse tangent function as tan 1 sin ,cos .
page 27