13
UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION Minor Project II Report Minjie Lu 11450458 1 Modelling and Simulation MATLAB Simulation 2014 Minjie Lu (11450458) School of Civil Structural and Environmental Engineering

Modelling and Simulations

Embed Size (px)

Citation preview

Page 1: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

1

Modelling and Simulation

MATLAB Simulation

2014

Minjie Lu (11450458)

School of Civil Structural and Environmental Engineering

Page 2: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

2

In this report, the code used in Matlab practice is shown in the black text boxes.

Problem: 3-body planet problem

This problem asked us to set up the equations of motion for the planet earth taking into account the

gravitational effects of the sun and of the planets Jupiter and Neptune. Produce a simulation for the motion

of the three planets for a period of at least three Jovian years. Visualise by creating an animation, i.e. a

movie file or .avi file.

As this is a group project, we decide to divide the work to each team members as follow:

Ryan McLaughlin: Focuses on development of and presentation of the analysis. His responsibility is

looking up of certain standard data such as the relevant planetary masses.

Minjie Lu(Myself): Focuses on numerical solution of the resulting differential equations by some method

presented in the module. We used Fourth Order Runge Kutta Method as the numerical method in this project.

Luke O'Doherty: Focuses on the presentation of results, i.e. the animation.

Development of function equations & Research of relevant information:

Newton’s Universal Law of Gravitation states ‘that every point mass in the universe attract every other point

mass with a force that is directly proportional to the product of their masses and inversely proportional to the

square of the distance between them’.

𝐹 = 𝐺𝑚1𝑚2

𝑟2

Equation 1.1 Newton’s law of Gravitation

F is the force between the masses,

G is the gravitational constant

m1 is the first mass,

m2 is the second mass, and

r is the distance between the centres of the masses

In order to determine the movements of the 3 planets orbiting the Sun, equation 1.1 can be rearranged in

order to express each planets motion orbiting the Sun.

We know from Newton’s second law of Motion that acceleration is produced when a force acts on a mass

object.

𝐹 = 𝑚𝑎

Equation 1.2 Newton’s second Law of Motion

Page 3: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

3

Solving Equation 1.2 in terms of 𝑎 and then subbing Equation 1.1 in for 𝐹 allows us to express acceleration

as

𝑎 = 𝐺𝑚

𝑟2

Equation 1.3 Acceleration of planets

To plot the movements of the planets, the motion will have to be represented in vector form. Equation 1.3

must be developed into vector form for the movement of each planet in the x and y direction.

�̅� = 𝐺 𝑚1

|𝑅12|𝑅12̂

Where |𝑅12| = |𝑅2 − 𝑅1| is the distance between the two planets

𝑅12̂ = 𝑅2−𝑅1

|𝑅2−𝑅1| is the unit vector from one planet to another

�̅� = 𝐺𝑚(𝑅2 − 𝑅1)

|𝑅2 − 𝑅1|3

Equation 1.4 Acceleration in vector form

This is the equation that will be used for the numerical analysis of this 3-body problem.

The next step is to find the initial conditions for each planet allowing the above differential equations to be

used.

The Mean Radius and Mass of both the Sun and Planets are found on the NASA website [1].

Using Jet propulsion laboratory [2] allows the user to select a target body and observer its location in a vector

table. This showed both the position and velocity of the target on a certain day.

The figures in Table 1.1 are from the 17𝑡ℎof May 2014.

Sun Earth Jupiter Neptune

Mean Radius (AU) 4.654478× 10−3 4.26352× 10−5 4.778913× 10−4 1.6555× 10−4

Mass (kg) 1.98854× 1030 5.97219× 1024 1.89813× 1027 1.0241× 1026

Position ( x-direction) (AU) 0 -5.66141252× 10−1 -2.3021578957 2.72424616× 101

Position ( y-direction) (AU) 0 -8.38492422× 10−1 4.70662422303 -1.2504889× 101

Velocity ( x-direction) (AU) 0 1.395322746× 10−2 -6.86942442× 10−3 1.2883944× 10−3

Velocity ( y-direction) (AU) 0 -9.72277019× 10−3 -2.9587172× 10−3 1.731366× 10−3

Table 1.1 Initial conditions for the 3-body problem

Page 4: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

4

The numerical solution:

It is clear that Fourth Order Runge Kutta Method is a very powerful method to solve ordinary differential

equations. Therefore, Our team decided to use this method to slove the ODE functions involved in this

problem. For this three body problem, we are assuming that the earth, Jupiter and Neptune orbit a on one

plane as the sun, i.e the z co-ordinates remain constant. The Fourth Order Runge Kutta Method was made

from the following equations:

𝑦𝑛+1 = 𝑦𝑛 + (𝑤1𝑘1 + 𝑤2𝑘2 + 𝑤3𝑘3 + 𝑤4𝑘4)

𝑥𝑛+1 = 𝑥𝑛 + ℎ

𝑘1 = ℎ𝑓(𝑥𝑛, 𝑦𝑛)

𝑘2 = ℎ𝑓(𝑥𝑛 + 𝛼1ℎ, 𝑦𝑛 + 𝛽1𝑘1)

𝑘3 = ℎ𝑓(𝑥𝑛 + 𝛼2ℎ, 𝑦𝑛 + 𝛽2𝑘2)

𝑘4 = ℎ𝑓(𝑥𝑛 + 𝛼3ℎ, 𝑦𝑛 + 𝛽3𝑘3)

Where 𝑤1 =1

6, 𝑤2 =

2

6, 𝑤3 =

2

6, 𝑤4 =

1

6 , 𝛼1 =

1

2, 𝛼2 =

1

2, 𝛼3 = 1, 𝛽1 =

1

2, 𝛽2 =

1

2, 𝛽3 = 1

The fourth Order Runge Kutta Method above is used for solving the ODE functions for Jupiter, Neptune and

Earth. The equation for Jupiter involves the gravitational force between the planet Jupiter and Sun.

The equation for solving the planet Neptune is slightly different to the above, as we have to take account

gravitational effects of both the Sun and Jupiter. The eqution for solving Earth is even more complicated,

since we have to take account the gravitational effects of Jupiter, Neptune and the Sun. In order to solve the

distance between the planets, the norm command is used. This command can help us to get the distance

between the planets by using pythagoras theorm. The basic idea of the norm command (Fig.MP2-1):

Figure MP2-1

As AB represent Distance in y-direction

and BC represent distance in x-direction,

the norm command is used to find the

distance between AC (i.e. the radius)

Page 5: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

5

Another team member, Ryan has already gathered the information about the initial conditions of Jupiter,

Neptune, Earth and Sun (includes their Radius, Mass, Position and Velocity in both x and y direction, the

Distance between the planets). Three of our team members will be consistent collaborated throughout the

duration of this project, as I need those basic informations to solve the ODE equation. And Luke needs the

M-files I will be creating to animate the results.

Before developing the numerical solution, the following assumptions will be made:

1. The centre of the mass (The Sun) will remain as a fixed point and will be placed at the origin of the

co-ordinate system.

2. All masses are considered as point mass.

3. As the angle of inclination from the centre mass is negilible, the z co-ordinates will be neglected.

M-file to solve ordinary differential equation for Jupiter:

This function is used to solve ODE of Newton's law of universal gravitation by using Fourth Order Runge-

Kutta method in order to find the acceleration of Jupiter. It does so by taking four inputs which includes:

Ms(Mass of Sun),H(The stepsize),N(The final time), and the initial conditions of Jupiter,namely Jupiters'

position and velocity in the x and y direction. The function is used to solve their derivatives, velocity and

acceleration. And once these parameters have been solved, they will be updated to the initial condition

vector.The solutions for each step size are stored in order to visualise the results.

Function [Storage]=planet_Jupiter(Ms,H,N,Jupiter_initial_condition)

%Function operates to solve ODE of Newton's law of universal gravitation by

%using Fourth Order Runge-Kutta method in order to find the acceleration of Jupiter.

%This function requires four inputs

%which includes: Ms(Mass of Sun),H(The stepsize),N(The final time), and the

%initial conditions of Jupiter,namely Jupiters' position and velocity in

%the x and y direction.

%The function finds the solutions by using Newtons Law of Gravitation:

% F=M*A

% A = -(G*mass/|R2-R1|^2)*(R2-R1/|R2-R1|)

%Simplfied to A = -(G*mass*(R2-R1)/|R2-R1|^3)

%The function will opereate as:

% |Vx| = |0 0 1 0| * | x | + | 0 |

% |Vy| |0 0 0 1| | y | | 0 |

% |Ax| |0 0 0 0| | Vx| | -G*Ms/R^2*|Rx| |

% |Ay| |0 0 0 0| | Vy| | -G*Ms/R^2*|Ry| |

%Function created by : Minjie Lu (11450458)

% Ryan McLaughlin (11486182)

% Luke O'Doherty (11434628)

% University College Dublin

%Created for Minor Project 2: EEEN30150 - MODELLING AND SIMULATION

%Version No:MP2_1_1

if nargin~=4

error('Require four input values: Mass of Sun, The stepsize H, The final time N, and the

initial conditions of Jupiter')

end

G=1.4879e-34 %Universal Gravitation constant in AU^3/(kg*Day^2)

Storage=zeros(2,N+1);%create a storage matrix

Storage(:,1)=Jupiter_initial_condition(1:2);

%store the initial condition in the first column of matrix

Page 6: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

6

M-file to solve ordinary differential equation for Neptune:

This function is used to solve ODE of Newton's law of universal gravitation by using Fourth Order Runge-

Kutta method in order to find the acceleration of Neptune. For the planet Neptune, we have to take account

gravitational effects of both the Sun and Jupiter. It does so by taking six inputs which includes: Jupiter(the

resulting conditions of the first m-file),Mj(mass of Jupiter), Ms(mass of Sun),H(the stepsize),N(the final

time),and the initial conditions of Neptune, namely Neptune's position and velocity in the x and y direction.

The function is used to solve their derivatives, velocity and acceleration. And once these parameters have

been solved, they will be updated to the initial condition vector.The solutions for each step size are stored in

order to visualise the results.

function [Storage]=planet_Neptune(Jupiter,Mj,Ms,H,N,Neptune_initial_condition)

%Function operates to solve ODE of Newton's law of universal gravitation by

%using Fourth Order Runge-Kutta method in order to find the acceleration of Neptune.

%For the planet Neptune, we have to take account gravitational effects of both the Sun and

Jupiter.

%This function requires six inputs which includes:

%Jupiter(the resulting conditions of the first m-file),Mj(mass of Jupiter),

%Ms(mass of Sun),H(the stepsize),N(the final time),and the initial conditions of Neptune,

%namely Neptune's position and velocity in the x and y direction.

%The function finds the solutions by using Newtons Law of Gravitation:

Matrix_jup=zeros(4,4);

%Creating a four by four matrix that will be multiplied by the initial conditions

Matrix_jup(1,3)=1;%Filling row one, element three with a one

Matrix_jup(2,4)=1;%Filling row two, element four with a one

% Solve the ODE by using Forth order Runge-kutta method

for count = 1:N %Set up loop to begin

Init_temp = Jupiter_initial_condition;%temporary storage for the initial conditions

Accel_temp= -(G*Ms)*[0;0;Init_temp(1:2)]/(norm(Init_temp(1:2))^3);

%temporary storage for acceleration

k1=H*((Matrix_jup*Init_temp)+Accel_temp);%Evaluate k1 using old x and y

Init_temp = Jupiter_initial_condition + 0.5*k1;%Updating the initial condition vector

Accel_temp = -(G*Ms)*[0;0;Init_temp(1:2)]/(norm(Init_temp(1:2))^3);

%Updating temporary storage for acceleration

k2=H*((Matrix_jup*Init_temp)+Accel_temp);%Evaluate k2 using old x and y

Init_temp = Jupiter_initial_condition + 0.5*k2;%Updating the initial condition vector

Accel_temp = -(G*Ms)*[0;0;Init_temp(1:2)]/(norm(Init_temp(1:2))^3);

%Updating temporary storage for acceleration

k3=H*((Matrix_jup*Init_temp)+Accel_temp);%Evaluate k3 using old x and y

Init_temp = Jupiter_initial_condition + k3;%Updating the initial condition vector

Accel_temp = -(G*Ms)*[0;0;Init_temp(1:2)]/(norm(Init_temp(1:2))^3);

%Updating temporary storage for acceleration

k4=H*((Matrix_jup*Init_temp)+Accel_temp);%Evaluate k4 using old x and y

Jupiter_initial_condition=Jupiter_initial_condition+((1/6)*(k1+(2*k2)+(2*k3)+k4));

%Update InitialConditions

Storage(:,count+1)= Jupiter_initial_condition(1:2);%Store the positions

end%end to for loop end%end to main function

Page 7: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

7

% F=M*A

% A = -(G*mass/|R2-R1|^2)*(R2-R1/|R2-R1|)

%Simplfied to A = -(G*mass*(R2-R1)/|R2-R1|^3)

%Function created by : Minjie Lu (11450458)

% Ryan McLaughlin (11486182)

% Luke O'Doherty (11434628)

% University College Dublin

%Created for Minor Project 2: EEEN30150 - MODELLING AND SIMULATION

%Version No:MP2_2_1

if nargin~=6

error('Require six input values: Jupiter(the resulting condition of Jupiter orbit),Mass of

Jupiter,Mass of Sun, The stepsize H, The final time N, and the initial conditions of

Neptune')

end

G=1.4879e-34; %Universal Gravitation constant in AU^3/(kg*Day^2)

Storage=zeros(2,N+1);%create a storage matrix

Storage(:,1)=Neptune_initial_condition(1:2);

%store the initial condition in the first column of matrix

% Solve the ODE by using Fourth order Runge-kutta method

for count = 1:N %Set up loop to begin

Init_temp = Neptune_initial_condition;%temporary storage for the initial conditions

Accel_temp=[Init_temp(3);

Init_temp(4);

((-G*Ms*Init_temp(1))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(1,N)-Init_temp(1)))/(norm((Init_temp(1)-

Jupiter(1,N))+(Jupiter(2,N)-Init_temp(2)))^3);

((-G*Ms*Init_temp(2))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(2,N)-Init_temp(2)))/(norm((Init_temp(1)-

Jupiter(1,N))+(Jupiter(2,N)-Init_temp(2)))^3)];

k1=H*(Accel_temp);%Evaluate k1 using old x and y

Init_temp = Neptune_initial_condition + 0.5*k1;%Updating the initial condition vector

Accel_temp=[Init_temp(3);

Init_temp(4);

((-G*Ms*Init_temp(1))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(1,N)-Init_temp(1)))/(norm((Init_temp(1)-

Jupiter(1,N))+(Jupiter(2,N)-Init_temp(2)))^3);

((-G*Ms*Init_temp(2))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(2,N)-Init_temp(2)))/(norm((Init_temp(1)-

Jupiter(1,N))+(Jupiter(2,N)-Init_temp(2)))^3)];

k2=H*(Accel_temp);%Evaluate k2 using old x and y

Init_temp = Neptune_initial_condition + 0.5*k2;%Updating the initial condition vector

Accel_temp=[Init_temp(3);

Init_temp(4);

((-G*Ms*Init_temp(1))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(1,N)-Init_temp(1)))/(norm((Init_temp(1)-

Jupiter(1,N))+(Jupiter(2,N)-Init_temp(2)))^3);

((-G*Ms*Init_temp(2))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(2,N)-Init_temp(2)))/(norm((Init_temp(1)-

Jupiter(1,N))+(Jupiter(2,N)-Init_temp(2)))^3)];

k3=H*(Accel_temp);%Evaluate k3 using old x and y

Init_temp = Neptune_initial_condition + k3;%Updating the initial condition vector

Accel_temp=[Init_temp(3);

Init_temp(4);((-G*Ms*Init_temp(1))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(1,N)-Init_temp(1)))/(norm((Init_temp(1)-

Jupiter(1,N))+(Jupiter(2,N)-Init_temp(2)))^3);

((-G*Ms*Init_temp(2))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(2,N)-Init_temp(2)))/(norm((Init_temp(1)-

Jupiter(1,N))+(Jupiter(2,N)-Init_temp(2)))^3)];

k4=H*(Accel_temp);%Evaluate k4 using old x and y

Page 8: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

8

M-file to solve ordinary differential equation for Earth:

This function is used to solve ODE of Newton's law of universal gravitation by using Fourth Order Runge-

Kutta method in order to find the acceleration of Earth. For the planet Earth, we have to take account

gravitational effects of Jupiter, Neptune and the Sun. It does so by taking eight inputs which includes:

Jupiter(the resulting conditions of the first m-file),Mj(mass of Jupiter), Neptune(the resulting conditions of

the second m-file),Mn(mass of Neptune), Ms(mass of Sun),H(the stepsize),N(the final time)and the initial

conditions of Earth, namely Earth's position and velocity in the x and y direction.

function [Storage]=planet_Earth(Jupiter,Mj,Neptune,Mn,Ms,H,N,Earth_initial_condition)

%Function operates to solve ODE of Newton's law of universal gravitation by

%using Fourth Order Runge-Kutta method in order to find the acceleration of Earth.

%For the planet Earth, we have to take account

%gravitational effects of Jupiter, Neptune and the Sun.

%This function requires eight inputs which includes:

%Jupiter(the resulting conditions of the first m-file),Mj(mass of Jupiter),

%Neptune(the resulting conditions of the second m-file),Mn(mass of Neptune),

%Ms(mass of Sun),H(the stepsize),N(the final time)and the initial conditions of Earth,

%namely Earth's position and velocity in the x and y direction.

%The function finds the solutions by using Newtons Law of Gravitation:

% F=M*A

% A = -(G*mass/|R2-R1|^2)*(R2-R1/|R2-R1|)

%Simplfied to A = -(G*mass*(R2-R1)/|R2-R1|^3)

%Function created by : Minjie Lu (11450458)

% Ryan McLaughlin (11486182)

% Luke O'Doherty (11434628)

% University College Dublin

%Created for Minor Project 2: EEEN30150 - MODELLING AND SIMULATION

%Version No:MP2_3_1

if nargin~=8

error('Require eight input values: Jupiter(the resulting condition of Jupiter

orbit),Mass of Jupiter,Neptune(the resulting conditions of the second m-file),Mass of

Neptune,Mass of Sun, The stepsize H, The final time N, and the initial conditions of

Neptune')

end

G=1.4879e-34; %Universal Gravitation constant in AU^3/(kg*Day^2)

Storage=zeros(2,N+1);%create a storage matrix

Storage(:,1)=Earth_initial_condition(1:2);

%store the initial condition in the first column of matrix

% Solve the ODE by using Fourth order Runge-kutta method

for count = 1:N %Set up loop to begin

Neptune_initial_condition=

Neptune_initial_condition+((1/6)*(k1+(2*k2)+(2*k3)+k4));

%Update InitialConditions

Storage(:,count+1)= Neptune_initial_condition(1:2);

%Store the positions

end%end to for loop end%end to main function

Page 9: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

9

Init_temp = Earth_initial_condition;%temporary storage for the initial conditions

Accel_temp=[

Init_temp(3);

Init_temp(4);((-G*Ms*Init_temp(1))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(1,N)-Init_temp(1)))/(norm((Init_temp(1)-Jupiter(1,N))+(Jupiter(2,N)-

Init_temp(2)))^3)-...

(G*Mn*(Neptune(1,N)-Init_temp(1)))/(norm((Init_temp(1)-Neptune(1,N))+(Neptune(2,N)-

Init_temp(2)))^3);...

((-G*Ms*Init_temp(2))/(norm(Init_temp(1:2))^3))-(G*Mj*(Jupiter(2,N)-

Init_temp(2)))/(norm((Init_temp(1)-Jupiter(1,N))+...

(Jupiter(2,N)-Init_temp(2)))^3)-(G*Mn*(Neptune(2,N)-

Init_temp(2)))/(norm((Init_temp(1)-Neptune(1,N))+(Neptune(2,N)-Init_temp(2)))^3)];

k1=H*(Accel_temp);%Evaluate k1 using old x and y

Init_temp = Earth_initial_condition + 0.5*k1;%Updating the initial condition vector

Accel_temp=[

Init_temp(3);

Init_temp(4);((-G*Ms*Init_temp(1))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(1,N)-Init_temp(1)))/(norm((Init_temp(1)-Jupiter(1,N))+(Jupiter(2,N)-

Init_temp(2)))^3)-...

(G*Mn*(Neptune(1,N)-Init_temp(1)))/(norm((Init_temp(1)-Neptune(1,N))+(Neptune(2,N)-

Init_temp(2)))^3);...

((-G*Ms*Init_temp(2))/(norm(Init_temp(1:2))^3))-(G*Mj*(Jupiter(2,N)-

Init_temp(2)))/(norm((Init_temp(1)-Jupiter(1,N))+...

(Jupiter(2,N)-Init_temp(2)))^3)-(G*Mn*(Neptune(2,N)-

Init_temp(2)))/(norm((Init_temp(1)-Neptune(1,N))+(Neptune(2,N)-Init_temp(2)))^3)];

k2=H*(Accel_temp);%Evaluate k1 using old x and y

Init_temp = Earth_initial_condition + 0.5*k2;%Updating the initial condition vector

Accel_temp=[

Init_temp(3);

Init_temp(4);((-G*Ms*Init_temp(1))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(1,N)-Init_temp(1)))/(norm((Init_temp(1)-Jupiter(1,N))+(Jupiter(2,N)-

Init_temp(2)))^3)-...

(G*Mn*(Neptune(1,N)-Init_temp(1)))/(norm((Init_temp(1)-Neptune(1,N))+(Neptune(2,N)-

Init_temp(2)))^3);...

((-G*Ms*Init_temp(2))/(norm(Init_temp(1:2))^3))-(G*Mj*(Jupiter(2,N)-

Init_temp(2)))/(norm((Init_temp(1)-Jupiter(1,N))+...

(Jupiter(2,N)-Init_temp(2)))^3)-(G*Mn*(Neptune(2,N)-

Init_temp(2)))/(norm((Init_temp(1)-Neptune(1,N))+(Neptune(2,N)-Init_temp(2)))^3)];

k3=H*(Accel_temp);%Evaluate k3 using old x and y

Init_temp = Earth_initial_condition + k3;%Updating the initial condition vector

Accel_temp=[

Init_temp(3);

Init_temp(4);((-G*Ms*Init_temp(1))/(norm(Init_temp(1:2))^3))-...

(G*Mj*(Jupiter(1,N)-Init_temp(1)))/(norm((Init_temp(1)-Jupiter(1,N))+(Jupiter(2,N)-

Init_temp(2)))^3)-...

(G*Mn*(Neptune(1,N)-Init_temp(1)))/(norm((Init_temp(1)-Neptune(1,N))+(Neptune(2,N)-

Init_temp(2)))^3);...

((-G*Ms*Init_temp(2))/(norm(Init_temp(1:2))^3))-(G*Mj*(Jupiter(2,N)-

Init_temp(2)))/(norm((Init_temp(1)-Jupiter(1,N))+...

(Jupiter(2,N)-Init_temp(2)))^3)-(G*Mn*(Neptune(2,N)-

Init_temp(2)))/(norm((Init_temp(1)-Neptune(1,N))+(Neptune(2,N)-Init_temp(2)))^3)];

k4=H*(Accel_temp);%Evaluate k4 using old x and y

Earth_initial_condition=Earth_initial_condition+((1/6)*(k1+(2*k2)+(2*k3)+k4));

%Update InitialConditions

Storage(:,count+1)= Earth_initial_condition(1:2);%Store the positions

end%end to for loop end%end to main function

Page 10: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

10

Visulisation:

The final part of the project was to develop a method by which the results could be visualised. By creating a

function M-File, which used the function M-File ODE’s which Minjie had created, the paths of the planets

could be visualised. Implementing the Matlab commands plot and surf, a sphere could be created for the

planets and the sun. This could then be animated, showing the planets and their paths orbiting the sun for 3

Jovian years.

The first step of the M-File was to assign the initial positions, initial velocities and masses of each planet

(shown in Table 1) to a variable so they could be used in the calculation of the visualisation. Both the initial

positions and velocities used Cartesian co-ordinates excluding the z-direction. Also the step size, H, and

final time, N, had to be chosen.

The same process was undertaken for Earth and Neptune.

The next step was to plot the sun which is fixed at the centre of the orbit. Again using Table 1, selecting the

value for the radius of the sun in Au and the surf command, the sun could be created as a sphere and c then

coloured yellow. The hold on command is then used so further plots can be added. The background is

coloured black to mimic the colour is space.

function [Animation] = Orbit

%Function is used to calculate the orbit of Earth, Jupiter and Neptune

%around the sun. Newton's law of universal gravitation is combined with the

%fourth order Runge-Kutta method. The initial conditions of each planet's

%mass, position and velocites are inputted. The ODEs are solved by

%implementing the function M-Files for each planet; planet_Jupiter,

%planet_Earth and planet_Neptune which use the above conditions. Each

%planet is created used using the surf function and position vectors are

%assigned which depicts their circular motion. The planets are visualised

%graphically.

%Function created by: Luke O'Doherty (11434628)

% Minjie Lu (11450458)

% Ryan McLaughlin (11486182)

%Date: 17th May 2014

%Version No:MP2_4_1

H=7; %this is our step size for the rung kutta method in days N=3*12*52; %time in 3 Jovian years Mj=1.89813e27; %Mass of Jupiter in kg %Jupiter Initial Conditions Jupx=-2.3021578957356; %position in the X direction (AU) Jupy=4.70662422303194; %position in the Y direction (AU) Velocity_Jupx=-6.86942440147889e-3; %Velocity in the X direction (AU/day) Velocity_Jupy=-2.95871721235009e-3; %Velocity in the y direction (AU/day) Jupiter_initial_condition=[Jupx,Jupy,Velocity_Jupx,Velocity_Jupy]';

Page 11: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

11

Next, the paths of each planet were created using the Function M-File ODE’s created by me. The plot

command was used to visualise this.

The planets could now be plotted by in the same way as the sun was plotted. A for loop was used, starting at

one day and ending with final time, N, which is three Jovian years.

%SUN PLOTTING

Rs=0.00465448*120;%Radius of sun (AU)

angleZ=linspace(0,3.14,30); %the range of the angle with respect to the z-axis

angleX=linspace(0,6.28,40); %the range of the angle with respect to the z-axis

[angleZ,angleX]=meshgrid(angleZ,angleX);%Change the values into array form used

%for the surface plot

x=Rs*sin(angleZ).*cos(angleX); %The x co-ordinate for sphere

y=Rs*sin(angleZ).*sin(angleX); %The y co-ordinate for sphere

z=Rs*cos(angleZ); %The z co-ordinate for sphere

surf(x,y,z,'EdgeColor','yellow','FaceColor','yellow')%Plots the sphere based on

previous co-ordinates

set(gca, 'color', [0 0 0]);

set(gcf, 'color', [0 0 0]);

axis off;

hold on

%%%% Calling the ODE functions %%%%

%Starts the ODE function, implementing the fouth order Runge-Kutta [Jupiter]=planet_Jupiter(Ms,H,N,Jupiter_initial_condition); plot(Jupiter(1,:),Jupiter(2,:),'m'); %Plots the orbit of the Jupiter's course hold on

[Neptune]=planet_Neptune(Jupiter,Mj,Ms,H,N,Neptune_initial_condition); plot(Neptune(1,:),Neptune(2,:),'c') %Plots the orbit of the Neptune's course hold on

[Earth]=planet_Earth(Jupiter,Mj,Neptune,Mn,Ms,H,N,Earth_initial_condition); plot(Earth(1,:),Earth(2,:),'b'); %Plots the orbit of the Earth's course hold on

leg=legend('Sun','Jupiters Orbit','Neptunes Orbit','Earths Orbit'); %inserts a legend set(leg,'Textcolor','white') %turns the letters white

%Plotting the Planets

for count=1:N

%JUPITER PLOTTING Rj=4.77895e-4*750;%Radius of jupiter in (AU) angleZ=linspace(0,3.14,30); angleX=linspace(0,6.28,40); [angleZ,angleX]=meshgrid(angleZ,angleX);

x=Rj*sin(angleZ).*cos(angleX); y=Rj*sin(angleZ).*sin(angleX); z=Rj*cos(angleZ);

plothandle=surf(x+Jupiter(1,count),y+Jupiter(2,count),z,'EdgeColor','m',

'FaceColor','m');%coloured in pink hold on

Page 12: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

12

All the planets have now been visualised at each step and can now be put into an animation. A frame is

saved for each step in the for loop and it is then delerted each time before a new frame is displayed. This

gives the effect of the planet orbiting the sun.

%NEPTUNE PLOTTING Rn=1.6555e-4*1250; angleZ=linspace(0,3.14,30); angleX=linspace(0,6.28,40); [angleZ,angleX]=meshgrid(angleZ,angleX);

x=Rn*sin(angleZ).*cos(angleX); y=Rn*sin(angleZ).*sin(angleX); z=Rn*cos(angleZ);

plothandle1=surf(x+Neptune(1,count),y+Neptune(2,count),z,'EdgeColor','c',

'FaceColor','c');%coloured in light blue

%EARTH PLOTTING Re=4.26352e-5*1500; angleZ=linspace(0,3.14,30); angleX=linspace(0,6.28,40); [angleZ,angleX]=meshgrid(angleZ,angleX);

x=Re*sin(angleZ).*cos(angleX); y=Re*sin(angleZ).*sin(angleX); z=Re*cos(angleZ);

plothandle2=surf(x+Earth(1,count),y+Earth(2,count),z,'EdgeColor','g', 'FaceColor','g');

%coloured in green

axis equal; %equal axis length is all directions

Animation(count)=getframe; %Saves a frame for each step

delete(plothandle);%Deletes the previous plot of Jupiter so only one sphere appears

in each frame delete(plothandle1);%Deletes the previous plot of Neptune so only one sphere appears

in each frame delete(plothandle2);%Deletes the previous plot of Earth so only one sphere appears

in each frame

end%end to for loop end%end to main function

Page 13: Modelling and Simulations

UNIVERSITY COLLEGE DUBLIN EEEN30150 MODELLING AND SIMULATION

Minor Project II Report Minjie Lu 11450458

13

The animation can be seen by the link below (created by Luke O’Doherty):

https://www.youtube.com/watch?v=yGQoFhS6xxg&feature=youtu.be

Website Reference:

[1]:http://nasasearch.nasa.gov/search?utf8=%E2%9C%93&sc=0&query=mass+of+earth&m=&affiliate=nasa &commit=Search

[2]: http://ssd.jpl.nasa.gov/horizons.cgi#results

Figure: picture of the animation for the orbiting planets