0% found this document useful (0 votes)
15 views14 pages

Kepler

Uploaded by

Youssef Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views14 pages

Kepler

Uploaded by

Youssef Yasser
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

University of Science and Technology at Zewail

City

Project Report

Kepler’s Problem and


Keplerian orbits

Yousef Zanny 202101010


Yousef Yasser 202101134
Shady Mohammed 202101231

supervised by
Dr. Ahmed Abdelsamea

May 2023
Kepler’s Problem and Keplerian orbits • May 2023

Contents
1 Abstract 2

2 Introduction 2

3 Methodology 2
3.1 Analytical Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.1.1 The Planet-Sun system is two-dimensional . . . . . . . . . . . . . . . . . . . . . . . . . 2
3.1.2 Equation of motion of the planet-Sun system . . . . . . . . . . . . . . . . . . . . . . . 3
3.1.3 Kepler’s Second Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.1.4 Kepler’s Fist Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.1.5 Kepler’s Third Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2 Numerical Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2.1 Euler’s method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.2.2 Runge-Kutta Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2.3 RK45 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

4 Results 7
4.1 Analytical Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1.1 First Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1.2 Second Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1.3 Third Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.2 Numerical Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2.1 Euler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.2.2 RK45 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5 Appendix 9
5.1 Code Snippets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1
Kepler’s Problem and Keplerian orbits • May 2023

1 Abstract
Kepler’s problem is a fundamental problem in celestial mechanics, which seeks to determine
the motion of a planet around a star under the influence of gravitational forces. Johannes
Kepler first formulated the problem in the early 17th century as part of his efforts to under-
stand the motion of the planets in the solar system. Kepler’s three laws of planetary motion,
which describe the motion of planets in terms of their elliptical orbits and varying speeds,
were derived from solutions to Kepler’s problem.
our goal is to illustrate properly the analysis of the three laws of Kepler and finally introduce
the Keplarian equation which is considered a big deal in celestial mechanics.
the project is based on the methods of solving differential equations and also vector analysis.
algorithms like that of Python are used to simulate the Keplarean orbital system.

2 Introduction
Statement of Kepler’s Laws
Johannes Kepler (1571-1630) had stated three laws about planetary motion. We state these laws below.
Isaac Newton (1643-1727) showed that these laws are consequences of the basic equation of motion. We first
state Kepler’s laws in an informal way, then discuss a more rigid formulation, and then give various proofs
of the fact that implies these laws.

With the aid of Tychobrahe astronomical data, Johannes Kepler was able to develop the laws that cor-
rectly describes the motion of planets around the sun. Stated as follows:

I. Kepler’s first law: Let q(t) be a maximal solution of the basic equation of motion . Its orbit
is either an ellipse which has one focal point at the the origin, a branch of a hyperbola which has one focal
point at the origin, a parabola whose focal point is the origin, or an open ray emanating from the origin.

II. Kepler’s second law: The area swept out by the vector joining the origin to the point q(t)
in a given time is proportional to the time.

III. Kepler’s third law: The squares of the periods of the planets are proportional to the cubes
of their semimajor axes.

3 Methodology
3.1 Analytical Solution
Our goal is to deduce Kepler’s laws starting from the universal gravitational law of SIR ISAAC NEWTON.
The first assumption is that the planet is only affected by the force of the gravitation of the Sun and neglect
the forces due to the other planets.
The universal gravitational law is:
Mm
F⃗ = −G 2 r̂
r
⃗ d2 ⃗
r
since F = m 2 & ⃗r = r · r̂, thus =⇒
dt
d2⃗r GM
= − 3 ⃗r (1)
dt2 r

3.1.1 The Planet-Sun system is two-dimensional


Before we continue the analysis, we first must explain why the motion of the planet-Sun system is two-
dimensional motion.
Gravity is the controlling force in our system and gravity is both conservative and central force.

2
Kepler’s Problem and Keplerian orbits • May 2023

• conservative: Thus, the gravitational force can be written as a gradient of a scalar field function

F⃗ = −∇u(r)

where u(r) is called the potential energy function and can be expressed as
Z Z
Mm Mm
u(r) = − F⃗ · d⃗r = − G 2 r̂ · d⃗r = −G
r r
The total energy of the system is conserved if there is no external forces. That is,

mv 2 GM m
E= − = constant
2 r
E
introducing the quantity ϵ which is the total energy per mass m, then we have

v2 GM
ϵ= − = constant
r r

• Central Force: This means that the total angular momentum of the system is conserved, the angular
momentum is defined by:
⃗l = m⃗r × ⃗v = constant

as we did with the energy, we will introduce ℓ which the total angular momentum per mass ml . Then
we have
⃗ℓ = ⃗r × ⃗v = constant

by taking the dot product of ⃗ℓ we get


⃗ℓ · ⃗r = 0

this is the equation of the plane that its normal is parallel to ⃗ℓ. Since ⃗ℓ is constant, then the direction
of the plane is constant, hence it will be two-dimensional

3.1.2 Equation of motion of the planet-Sun system


We can study the planet-Sun system by either Cartesian or polar coordinates. But it is more convenient to
express the system by polar coordinates.
We are not interested in the transformation from a Cartesian system to a polar system. So, we will just focus
on the result of the transformation. The transformation is

x = r cos θ

y = r sin θ
y
arctan = θ
x
The new coordinates are the following:
 
r cos θ
⃗r = (2)
r sin θ
 
cos θ
êr = r̂ = (3)
sin θ
 
− sin θ
êθ = θ̂ = (4)
cos θ
After mathematical manipulation, we get the velocity formula which is:

⃗v = ṙêr + rθ̇êθ (5)

Since a= dv
dt , then by differentiating equation (5) we get:

GM
(r̈ − rθ̇2 )êr + (rθ̈ + 2ṙ + θ̇)êθ = − êr (6)
r2

3
Kepler’s Problem and Keplerian orbits • May 2023

• Radial equation of motion:


GM
r̈ − rθ̇2 = − (7)
r2
• Tangential equation of motion:
rθ̈ + 2ṙθ̇2 = 0 (8)

The tangential equation of motion implies the conservation of angular momentum. By definition:

ℓ = |⃗ℓ| = |⃗r × ⃗v | = r(rθ̇) = r2 θ̇ (9)

using the result from equation (8)

dℓ
=⇒ = r2 θ̈ + 2rṙθ̇ = r(rθ̈ + 2ṙθ̇2 ) = 0
dt
This state that the angular momentum of the system is constant.[1]

3.1.3 Kepler’s Second Law


Using the formula of the area of the ellipse which is:
1 2
∆A ∼
= r ∆θ (10)
2
dividing by ∆t and taking the limit of ∆t goes to zero gives us:

dA r2 ∆θ r2 dθ 1 ℓ
= lim = = r2 θ̇ = = constant (11)
dt ∆t→0 2 ∆t 2 dt 2 2
Therefore, the position vector ⃗r swipes equal areas at equal times which is Kepler’s second law

3.1.4 Kepler’s Fist Law


Recall equation (7) & (9)
GM
r̈ − rθ̇2 = −
r2
ℓ = |⃗ℓ| = |⃗r × ⃗v | = r(rθ̇) = r2 θ̇
After mathematical manipulation, we get:

ℓ2 GM
r̈ − =− 2 (12)
r3 r
by substituting u = 1r , the analysis leads to:

d2 u GM
+u= 2 (13)
dθ2 ℓ
Using the method of undetermined coefficients to solve DE (11), u(θ) has the following general form

GM
u(θ) = [1 + e cos θ]
ℓ2
GM
Let uc = ℓ2 , thus
u(θ) = uc [1 + e cos θ] (14)
and thus
rc
r(θ) = (15)
1 + e cos θ

4
Kepler’s Problem and Keplerian orbits • May 2023

3.1.5 Kepler’s Third Law


Recall equations (10) & (11)
1 2
∆A ∼
= r ∆θ
2
dA ℓ
=
dt 2
and ℓ2 = GM rc
for a period T the planet will scan the whole elliptic area.
A πab ℓ
= =
T T 2
r2
2 4π 2 a2 b2 4π 2 a2 ( 1−e
c
2) 4π 2 3
T = = = a (16)
ℓ2 GM rc GM
T 2 ∝ a3 (17)

3.2 Numerical Solution


Kepler’s Problem refers to the mathematical problem of determining the motion of celestial bodies under the
influence of gravitational forces, particularly in the context of elliptical orbits described by Johannes Kepler’s
laws of planetary motion. It involves solving the differential equations that govern the motion of a particle
under a central force.

Keplerian orbits are a specific type of solution to Kepler’s Problem, describing the path followed by a
celestial body in the presence of a central mass. These orbits are characterized by their shape, which is an
ellipse, and their properties, such as the semi-major axis, eccentricity, and period.

Numerical solutions are often used to solve Kepler’s Problem and determine Keplerian orbits. One com-
mon approach is to discretize time and numerically integrate the equations of motion using techniques like
Euler’s method or more sophisticated methods like the Runge-Kutta method. By iteratively updating the
position and velocity of the particle, the orbit can be determined over a desired time span.

These numerical solutions provide accurate predictions of the motion of celestial bodies and are exten-
sively used in various fields such as astrodynamics, satellite navigation, and space mission planning.

here are several numerical methods that can be used to solve Kepler’s Problem and determine Keplerian
orbits. I will provide an overview of two common methods: Euler’s method and the Runge-Kutta method.

3.2.1 Euler’s method


Euler’s method is a simple and straightforward numerical integration method. It approximates the solution
by taking small time steps and updating the position and velocity based on the current values and the
governing equations. The equations of motion for a particle in Kepler’s Problem can be written as follows:

ṙ = v

GM
v̇ = − r
r3

Here, r represents the position vector, v represents the velocity vector, G is the gravitational constant, and
M is the mass of the central body. Using Euler’s method, the position and velocity at each time step can be
updated as follows:
r(t + ∆t) = r(t) + v(t)∆t
−GM
v(t + ∆t) = v(t) + r∆t
r3

5
Kepler’s Problem and Keplerian orbits • May 2023

By iterating these updates over small-time intervals, the trajectory of the particle can be approximated.

3.2.2 Runge-Kutta Method


The Runge-Kutta method is a more accurate numerical integration technique that improves upon Euler’s
method. One commonly used variant is the fourth-order Runge-Kutta method (RK4).
The equations of motion remain the same as in Euler’s method: Using RK4, the position and velocity can
be updated as follows:

k1 = ∆tv(t)
 
GM
l1 = ∆t − r(t)
|r(t)|3
 
1
k2 = ∆t v(t) + l1
2
 
GM 1
l2 = ∆t − (r(t) + k1 )
|r(t) + 12 k1 |3 2
 
1
k3 = ∆t v(t) + l2
2
 
GM 1
l3 = ∆t − (r(t) + k2 )
|r(t) + 12 k2 |3 2
k4 = ∆t (v(t) + l3 )
 
GM
l4 = ∆t − (r(t) + k 3 )
|r(t) + k3 |3
1
r(t + ∆t) = r(t) + (k1 + 2k2 + 2k3 + k4 )
6
1
v(t + ∆t) = v(t) + (l1 + 2l2 + 2l3 + l4 )
6
By iteratively applying these updates over small time intervals, the trajectory of the particle can be ac-
curately approximated.
These numerical methods provide a way to solve Kepler’s Problem and determine the Keplerian orbits of
celestial bodies by approximating the motion over time.[2]

3.2.3 RK45
The RK45 method can be applied to solve the equations of motion for Kepler’s Problem. The equations of
motion for a particle under the influence of gravitational force are given by:

ṙ(t) = v(t)
−G · M
v̇(t) = · r(t)
∥r(t)∥3
RK45 Method:
16 6656 28561 9 2
ri+1 = ri + hk1 + hk3 + hk4 − hk5 + hk6
135 12825 56430 50 55
25 1408 2197 1
vi+1 = vi + hl1 + hl3 + hl4 − hl5
216 2565 4104 5
Intermediate variables:
k1 = vi
1
k2 = vi + hl1
4
3 9
k3 = vi + hl1 + hl2
32 32

6
Kepler’s Problem and Keplerian orbits • May 2023

1932 −7200 7296


k4 = vi + hl1 + hl2 + hl3
2197 2197 2197
439 −8 3680 −845
k5 = vi + hl1 + hl2 + hl3 + hl4
216 1 513 4104
−8 2 −3544 1859 −11
k6 = vi + hl1 + hl2 + hl3 + hl4 + hl5
27 1 2565 4104 40
−G · M
l1 = · ri
∥ri ∥3
−G · M 1
l2 = 1 3
· (ri + hk1 )
∥ri + 4 hk1 ∥ 4
−G · M 3 9
l3 = 3 9 3
· (ri + hk1 + hk2 )
∥ri + + 32 hk2 ∥
32 hk1
32 32
−G · M 1932 −7200 7296
l4 = · (ri + hk1 + hk2 + hk3 )
∥ri + 1932
2197 hk1 + −7200
2197 hk2 +
7296
2197 hk3 ∥
3 2197 2197 2197
−G · M 439 −8 3680 −845
l5 = 439 −8 −845 · (ri + hk1 + hk2 + hk3 + hk4 )
∥ri + 216 hk1 + 1 hk2+ 3680
513 hk3 + 4104 hk4 ∥
3 216 1 513 4104
−G · M −8 2 −3544 1859 −11
l6 = −8 −3544 −11 ·(ri + hk1 + hk2 + hk3 + hk4 + hk5 )
∥ri + 27 hk1 + 21 hk2 + 2565 hk3 + 1859
4104 hk4 + 40 hk5 ∥
3 27 1 2565 4104 40

The Runge-Kutta-Fehlberg method, often referred to as RK45, is a widely used numerical integration method
for solving ordinary differential equations (ODEs) with adaptive step sizes. It combines the fourth-order and
fifth-order Runge-Kutta methods to achieve high accuracy.

Here’s an overview of the RK45 method:


Start with an initial step size h, initial time t0, initial position r0, and initial velocity v0. Initialize the
solution arrays r and v with r(1) = r0 and v(1) = v0. Repeat the following steps until the desired endpoint
tf is reached:
• Calculate the four intermediate positions k1, k2, k3, and k4 using the current position and velocity.

• Calculate the four intermediate velocities l1, l2, l3, and l4 using the current position, velocity, and
intermediate positions.
• Estimate the fifth-order position r5 and the fourth-order position r4 using the intermediate positions
and velocities.

• Estimate the local error as the difference between r4 and r5.


• Calculate the scale factor s to adjust the step size based on the error.
• Update the position r and velocity v using the fourth-order position r4 and the step size h.

• Update the time t by adding the step size h.


• Adjust the step size h based on the scale factor s.
• Store the updated position r and velocity v in the solution arrays.

4 Results
So far we have come to a set of equations analytically which describe the elliptical orbits of the planet or on
ca think of them as a 2-body problem. These equations are the following:

7
Kepler’s Problem and Keplerian orbits • May 2023

4.1 Analytical Results


4.1.1 First Law
The equation defining the path of the planet,
rc
r(θ) = 1+e cos θ
where,
1 ℓ2
rc = uc
= GM

Figure 1: Orbital path of eccentricity = 0.6.

This is the equation of a conic section with eccentricity e. If the energy E is negative [which is necessary
to make the system bounded], the eccentricity e is smaller than one and we have an ellipse. Similarly, we for
E = 0 we have e = 1 and the conic section is a parabola. Finally, if E > 0, we have e > 1 and the conic
section is a hyperbola.

4.1.2 Second Law


Planets in orbital motion scan equal areas at equal times.
dA ℓ
= = constant (18)
dt 2

4.1.3 Third Law


4π 2 3
T2 = a
GM
that is to say that T 2 ∝ a3 , The orbital period T is proportional to the cube of the semi-major axis.

8
Kepler’s Problem and Keplerian orbits • May 2023

4.2 Numerical Results


4.2.1 Euler

This graph is the result of Euler method which describes the path of the motion of the earth sun system by
using the real values in our model.

4.2.2 RK45

This graph is the result of RK45 method which describes the path of the motion of the earth sun system by
using the real values in our model.

RK45 adjusts the step size based on error estimates to control the accuracy of the solution. It calculates
two estimates of the solution using different orders, compares their differences, and adapts the step size ac-
cordingly. This approach allows RK45 to provide accurate solutions while efficiently managing computational
resources.[2]

5 Appendix
5.1 Code Snippets

9
Kepler’s Problem and Keplerian orbits • May 2023

Listing 1: elliptical orbit


import matplotlib . pyplot as plt
import numpy as np
def KPIII_semimajor ( orbital_period ) :
s e m i ma j o r _ a x i s _ c u b e d = orbital_period ** 2
semimajor_axis = s e m i m a j o r _ a x i s _ c u b e d ** (1. / 3.)

return semimajor_axis

def kepler_orbit ( eccentricity , orbital_period ) :


nStep = 500
tRange = np . linspace (0.0 , orbital_period , nStep )

theta = []
for time in tRange :
PsiDiff = 1.0
M = 2 * np . pi * time / orbital_period
PsiOld = M
theta0old = 180.0
while PsiDiff > 1e -10:
PsiNew = M + eccentricity * np . sin ( PsiOld )
PsiDiff = PsiNew - PsiOld
PsiOld = PsiNew
theta0 = 2 * np . arctan (((1 + eccentricity ) / (1 - eccentricity ) ) ←-
,→** (0.5) * np . tan ( PsiOld / 2.) )
theta . append ( theta0 )
return theta
orbital_period = 2.0
eccentricity = 0.6
list1 = kepler_orbit ( eccentricity , orbital_period )

def orbit ( semimajor_axis , eccentricity , true_anomaly ) :


r_orbit = semimajor_axis * (1 - eccentricity ** 2) / (1 + ←-
,→eccentricity * np . cos ( true_anomaly ) )
x_orbit = r_orbit * np . cos ( true_anomaly )
y_orbit = r_orbit * np . sin ( true_anomaly )

return x_orbit , y_orbit


semimajor_axis = KPIII_semimajor ( orbital_period )
xOrbit , yOrbit = orbit ( semimajor_axis , eccentricity , list1 )
plt . scatter (0 ,0 , color = ’ orange ’)
plt . scatter ( xOrbit , yOrbit )
plt . axis ( ’ equal ’)
plt . xlabel ( ’x ␣ position ␣ [ au ] ’)
plt . ylabel ( ’y ␣ position ␣ [ au ] ’)
plt . show ()

Listing 2: Sample Matlab code implementing the RK45 method


function [r , v ] = rk45Method (G , M , r0 , v0 , t0 , tf , h )
numSteps = ceil (( tf - t0 ) / h ) ; % Calculate the number of steps ←-
,→needed
r = zeros ( numSteps , 3) ;
v = zeros ( numSteps , 3) ;
r (1 , :) = r0 ;

10
Kepler’s Problem and Keplerian orbits • May 2023

v (1 , :) = v0 ;

t = t0 ;
i = 1;

while t < tf
% Calculate the intermediate positions
k1 = v (i , :) ;
k2 = v (i , :) + 0.25 * h * acceleration (G , M , r (i , :) ) ;
k3 = v (i , :) + (3/32) * h * acceleration (G , M , r (i , :) ) + (9/32) ←-
,→* h * acceleration (G , M , r (i , :) + (3/4) * h * k2 ) ;
k4 = v (i , :) + (1932/2197) * h * acceleration (G , M , r (i , :) ) + ←-
,→( -7200/2197) * h * acceleration (G , M , r (i , :) + (3/4) * h * ←-
,→k2 ) + (7296/2197) * h * acceleration (G , M , r (i , :) + (8/9) *←-
,→ h * k3 ) ;
k5 = v (i , :) + (439/216) * h * acceleration (G , M , r (i , :) ) + ( -8)←-
,→ * h * acceleration (G , M , r (i , :) + h * k1 ) + (3680/513) * h←-
,→ * acceleration (G , M , r (i , :) + (1/3) * h * k3 ) + ←-
,→( -845/4104) * h * acceleration (G , M , r (i , :) + (1/4) * h * ←-
,→k4 ) ;
k6 = v (i , :) + ( -8/27) * h * acceleration (G , M , r (i , :) ) + (2) * ←-
,→h * acceleration (G , M , r (i , :) + (1/2) * h * k1 ) + ←-
,→( -3544/2565) * h * acceleration (G , M , r (i , :) + (1/3) * h * ←-
,→k3 ) + (1859/4104) * h * acceleration (G , M , r (i , :) + (1/4) *←-
,→ h * k4 ) + ( -11/40) * h * acceleration (G , M , r (i , :) + (1/5)←-
,→ * h * k5 ) ;

% Calculate the intermediate velocities


l1 = acceleration (G , M , r (i , :) ) ;
l2 = acceleration (G , M , r (i , :) + 0.25 * h * k1 ) ;
l3 = acceleration (G , M , r (i , :) + (3/32) * h * k1 + (9/32) * h * ←-
,→k2 ) ;
l4 = acceleration (G , M , r (i , :) + (1932/2197) * h * k1 + ←-
,→( -7200/2197) * h * k2 + (7296/2197) * h * k3 ) ;
l5 = acceleration (G , M , r (i , :) + (439/216) * h * k1 + ( -8) * h *←-
,→ k2 + (3680/513) * h * k3 + ( -845/4104) * h * k4 ) ;
l6 = acceleration (G , M , r (i , :) + ( -8/27) * h * k1 + (2) * h * k2←-
,→ + ( -3544/2565) * h * k3 + (1859/4104) * h * k4 + ( -11/40) *←-
,→ h * k5 ) ;

% Estimate the fifth - order and fourth - order positions


r5 = r (i , :) + (16/135) * h * k1 + (6656/12825) * h * k3 + ←-
,→(28561/56430) * h * k4 + ( -9/50) * h * k5 + (2/55) * h * k6 ;
r4 = r (i , :) + (25/216) * h * k1 + (1408/2565) * h * k3 + ←-
,→(2197/4104) * h * k4 + ( -1/5) * h * k5 ;

% Estimate the local error


error = norm ( r5 - r4 ) ;

% Calculate the scale factor


s = 0.9 * ( tolerance / error ) ^(1/4) ;

% Adjust the step size based on the scale factor


if error < tolerance
h = s * h;
if h > maxStepSize

11
Kepler’s Problem and Keplerian orbits • May 2023

h = maxStepSize ;
end
i = i + 1;
t = t + h;

% Update the position and velocity arrays


r (i , :) = r5 ;
v (i , :) = l6 ;
else
h = s * h;
if h < minStepSize
error (" Step size became too small .")
end
end
end

% Trim the arrays to the actual number of steps taken


r = r (1: i , :) ;
v = v (1: i , :) ;
end

function a = acceleration (G , M , r )
rMag = norm ( r ) ;
rUnit = r / rMag ;
a = ( - G * M * rUnit ) / rMag ^2;
end

Listing 3: Sample Matlab code for implementing Euler’s method


function [r , v ] = eulerMethod (G , M , r0 , v0 , dt , numSteps )
r = zeros ( numSteps , 3) ;
v = zeros ( numSteps , 3) ;

r (1 , :) = r0 ;
v (1 , :) = v0 ;

for i = 2: numSteps
rMag = norm ( r (i -1 , :) ) ;
rUnit = r (i -1 , :) / rMag ;

r (i , :) = r (i -1 , :) + v (i -1 , :) * dt ;
v (i , :) = v (i -1 , :) - ( G * M * rUnit ) / rMag ^3 * dt ;
end
end

Listing 4: Sample Matlab code for implementing Fourth-order Runge-Kutta Method (RK4) method
function [r , v ] = rungeKuttaMethod (G , M , r0 , v0 , dt , numSteps )
r = zeros ( numSteps , 3) ;
v = zeros ( numSteps , 3) ;

r (1 , :) = r0 ;
v (1 , :) = v0 ;

for i = 2: numSteps
k1 = v (i -1 , :) ;
l1 = -G * M * r (i -1 , :) / norm ( r (i -1 , :) ) ^3;

12
Kepler’s Problem and Keplerian orbits • May 2023

k2 = v (i -1 , :) + 0.5 * l1 * dt ;
l2 = -G * M * ( r (i -1 , :) + 0.5 * k1 * dt ) / norm ( r (i -1 , :) + 0.5 ←-
,→* k1 * dt ) ^3;

k3 = v (i -1 , :) + 0.5 * l2 * dt ;
l3 = -G * M * ( r (i -1 , :) + 0.5 * k2 * dt ) / norm ( r (i -1 , :) + 0.5 ←-
,→* k2 * dt ) ^3;

k4 = v (i -1 , :) + l3 * dt ;
l4 = -G * M * ( r (i -1 , :) + k3 * dt ) / norm ( r (i -1 , :) + k3 * dt )←-
,→^3;

r (i , :) = r (i -1 , :) + (1/6) * ( k1 + 2* k2 + 2* k3 + k4 ) * dt ;
v (i , :) = v (i -1 , :) + (1/6) * ( l1 + 2* l2 + 2* l3 + l4 ) * dt ;
end
end

Bibliography
[1] B. W. Carroll and D. A. Ostlie. An Introduction to Modern Astrophysics. Cambridge University Press,
2021.
[2] Howard D. Curtis. Orbital Mechanics for Engineering Students. Elsevier, 2014.
[3] Herbert Goldstein, Charles P. Poole Jr, and John L. Safko. Classical Mechanics. 3rd. Addison Wesley,
2002.

13

You might also like