Вы находитесь на странице: 1из 3

Solving sets of coupled ODEs

Goal
Build a RK4 method ODE solver that solves a set of n coupled first order ODEs from an initial time t0 to a final
time tf.

Background
The following differential equations describe the motion of a body in orbit about two much more massive
bodies for example, a spaceship in orbit about the earth and moon.
d2 x
dy
~( x + ) ( x ~ )
dx
=
2
+
x

f
2
3
3
dt
dt
r1
r2
dt
d2 y
dx
~y y
dy
=

2
+
y

3 f
2
3
dt
dt
r1
r2
dt
where
1
~ = 1
r12 = ( x + )2 + y 2
r22 = ( x ~ ) 2 + y 2
82.45
is the ratio of the moon mass/earth mass, r1 is the distance from the earth to the spaceship, r2 is the
distance from the moon to the spaceship, and f is a friction coefficient

The equations are derived from Newtons law of motion and the inverse square law of gravitation. We are
taking the perspective of someone in a reference frame that is rotating at the same angular speed as the earth
and moon about their center of mass, so the earth and moon appear fixed in this reference frame. It is
assumed that the three bodies move in the x-y plane and the x-axis forms a line through the earth and moon.
The origin is located at the earth-moon center of mass, the moons center of mass is located at point (1-,0),
and the earths center of mass is located at point (-,0).
The equation uses normalized units for space and time. A distance of 1 in this coordinate system is equal to
the distance between the earth and moon, which we will assume is 238,000 miles. I do not know what the
units of time are normalized to (I still need to figure this out, but it wont matter for the purposes of this
assignment).

Program Details
I will run the following MATLAB code (you dont include code this in your program):
clc; clear; clf;
x0 = 1.2; y0 = 0;
% initial position of spaceship (normalized units)
vx0 = 0; vy0 = -1.0493571;
% initial velocity of spaceship (normalized units)
t0 = 0; tf = 10;
% initial time, final time (normalized units)
f = 0
% friction coefficient
N = 1000;
% initial guess for number of steps
dt = (tf-t0)/N;
% dt = time step (normalized units)
eps = 0.001;
% termination criteria
% I will adjust the parameters above when grading your assignment
[x,y,vx,vy,t] = myrk4(t0,tf,dt,x0,y0,vx0,vy0);
% You will need to write an RK4 solver in a separate M-file named myrk4.m
% x and y are the coordinates of the spaceship at each time step
% vx and vy are the x and y components of the speed at each time step
disp(['The final (x,y) position is: ('

num2str(x(end))

','

num2str(y(end))

')'])

Your job is to create an M-file named myrk4.m that contains a function named myrk4. This function
calculates the position and speed at every time step using the RK4 method. The outline of your M-file is the
following:
function [x,y,vx,vy,t] = myrk4(t0,tf,dt,x0,y0,vx0,vy0,f);
% Place your solver here
% After your solver is finished, write code that will plot trajectory and phase space on
two subplots. Be sure to add labels so the user will know what the axes represent.
% If you want to get fancy, you can create an animation of the spaceship flying around
the earth and moon, but this is not required.
end

It is possible that the number of steps input by the user (N) is too large to get accurate results. Your solver will
double the number of steps (cut the step size in half) until the percent difference in the final position falls
below a tolerance eps. The termination criterion is:
| ( r1(end)i+1 r1(end)i ) / r1(end)i+1 | < eps

where r1(end)i is the distance from the earth to the spaceship at the end of the simulation using the previous
step size, and r1(end)i+1 is the distance from the earth to the spaceship at the end of the simulation using the
smaller step size (one-half the step size used to calculate r1(end)i ).
You may use ode45 to test the accuracy of your solver, but the code you submit to me on Blackboard must
contain your own RK4 solver.
Be sure to suppress all output in the function using semi-colons! I dont want to see a bunch of intermediate
steps junking up my screen.

Simulations
(1) Calculate the trajectory for the following initial conditions:
x(0) = 1.2
y(0) = 0
x'(0) = 0
y'(0) = -1.04935751

f=0

This corresponds to a spaceship initially on the far side of the moon. Calculate the position of the spaceship
from t = 0 to 10, then plot the trajectory. You should see that the spaceships orbit is periodic with a period of
about 6.192 time units. Also, plot the phase space (r1 vs total speed).
If the earth is approximated as a sphere with a radius of 3959 miles, how close does the spaceship come to the
surface of earth? This will require you to convert the normalized spatial units into miles after your simulation
is over. Write your answer on the plot with pen.
(2) Use the same initial conditions, except set f = 1. Plot the position of the spaceship from 0 to 10 time units.
You should see that the spaceship crashes into the surface of the earth. Plot the phase space (r1 vs total
speed) as well.
(3) Use the same initial conditions, except set f = 0.1. Plot the position of the spaceship from 0 to 10 time
units. Plot the phase space (r1 vs total speed) as well.
Note: You may want to vary eps to make sure your step size isnt influencing the results.

Finish this assignment quickly? Want to investigate something for me?


I dont know what the normalized time units are, but I think there is a way to find out.
Step 1: Place the spaceship on the x-axis 100 miles from the earth's surface (4059 miles from the earths
center), where earth's gravity dominates, with zero initial velocity and no friction.
Step 2: Determine the amount of time needed for the spaceship to freefall from 100 miles to 50 miles. Using
the projectile motion equations, determine the amount of time (seconds) it would take for a spaceship to
freefall the same distance.
Step 3: The ratio of simulation time/projectile motion time should be the conversion factor between the
normalized units in the equations and the real time in seconds.
Step 4: Repeat this experiment with the spaceship freefalling 200 miles to 50 miles and see if the same ratio
appears.

What do you submit?


You will submit one file on Blackboard: An m-file named myrk4.m that contains the function myrk4. If the
trajectory calculated by the solver are correct, you will receive 10 points.
You will turn in a hard copy of the six plots discussed in the Simulations sector above. If the plots looks correct,
you will receive 10 points.

Вам также может понравиться