Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

MATLAB tutorial 3: writing codes to solve a problem

An Orbit Around the Sun


We will try to compute the orbit of an object (assumed massless) around the Sun. Open a new text file
and start by clearing all variables:
clear
!ext let"s define the length of an astronomical unit (#$) and the number of seconds in a day:
au % &.'()(*e&& + m
day % ,-.,-./' + s
We are going to integrate the e0uations of motion in the xy1plane:
dx
dt
=v
x
;
dy
dt
=v
y
;
dv
x
dt
=a
x
;
dv
y
dt
=a
y
2he acceleration can be computed from the gravitational force:
a=
GM
r
/

r
where the vector r is a unit vector3 and the mass of the object cancels out. # unit vector is a vector of
length &3 obtained by ta4ing the position vector r and dividing it by its length 5r5. $sing this
information3 and the fact that r =( x, y) gives:
a=(a
x
, a
y
)=
GM
r
/
(x , y)
r
or
a
x
=
GM
r
6
x; a
y
=
GM
r
6
y
2he four differential e0uations re0uire four initial conditions in order to have a uni0ue solution. 7et"s
choose the following:
xx % & . au yy % - vx % - vy % /(*--
#s we integrate over the orbit3 we need to 4eep trac4 of the position of the object3 so that we can plot
out the orbit once it"s been calculated. 8t"s most efficient to 9declare: the arrays where we will store the
x ; y positions up1front:
npoints % &---
x<vect % =eros(&3npoints)
y<vect % =eros(&3npoints)
>inally3 we need to choose a time1step for the integration procedure.
dt % &.day
We are going to use the simplest integration method3 4nown as 9?uler"s method:. 2he basic idea is that
at each point in time you ta4e a small step in the direction of the local velocity3 and then update the
velocity according to the local force. 2he rest of the program loo4s something li4e:
x<vect(&) % xx@au y<vect(&) % yy@au
for 44 % /:npoints
Aax3ayB%orbit<func(xx3yy3vx3vy)
xx % xx C vx.dt yy % yy C vy.dt
vx % vx C ax.dt vy % vy C ay.dt
x<vect(44) % xx@au y<vect(44) % yy@au
end
plot(x<vect3y<vect3"14")
axis(A1&.) &.) 1&.) &.)B)
axis s0uare
Dere we ma4e use of an external function that computes the accelerations at a given point:
function Aax3ayB%orbit<func(xx3yy)
E % ,.,F'/e1&& + mG6 4gGH1&I sGH1/I
J<s % &.(**(e6- + 4g
r % s0rt(xxG/CyyG/)
><factor % 1E.J<s @ rG6
ax % ><factor.xx ay % ><factor.yy
Kun the program to see the orbit. 2ry changing the step si=e. 8s a smaller step si=e more accurateL Dow
can you tellL
2he above method can be greatly improved by using a more accurate method. 2he next simplest is
4nown as 9second order Kunge1Mutta:. 2he method involves ta4ing the average of the current velocity
with an estimate of the 9future: velocity:
xx& % xx C vx.dt
vx& % vx C ax.dt
xx % xx C -.).(vx C vx&).dt
Similar terms are re0uired for the y position and the x ; y velocities N the latter re0uiring additional
accelerations ax& ; ay& at the positions xx& ; yy&.
Oompare the accuracy of the ?uler ; KM/ methods for a number of orbits and a number of time1step
si=es.
TASKS
$se your code to answer the following:
&. Suppose a comet has a perihelion of -./ #$3 and an aphelion of &--#$. What speed will the
comet have at perihelionL
/. Suppose we want to send a spacecraft to Jars (assume a distance of &.) #$ from the Sun).
What is the minimum launch speed relative to the earth that could get the spacecraft to Jars.
6. What is the escape speed for the solar systemL
2ry the next two tas4s if you have time.
'. ?xtend your code to two finite masses orbiting around each other.
). Ja4e a movie of the two orbiting masses. 2he following code will be useful to create frames
for your movie:
outname % A"frame" num/str(time) ".png"B
print(gcf3"1dpng"3outname)

You might also like