Tip:
Highlight text to annotate it
X
So now, we're going to look at the solution to this final problem set for Unit 1.
The first thing we have to do is fill in the definition for the acceleration function.
Now the acceleration function takes an argument that we've called spaceship position.
Well, we can define the position of the spaceship in relation to earth with this variable vector to earth.
A vector pointing from the earth to the spaceship is just the opposite
of a vector pointing from the spaceship to the earth.
So we define vector to earth as the negative of this argument, spaceship position.
Now since we're trying to define the acceleration,
what we want to return is an expression for acceleration.
So we rate return and then a translation of this equation over here that we talked about earlier.
So we have the gravitational constant which is G times the earth mass which is mE,
divided by this expression that looks pretty tricky, so let's dissect that briefly.
Now, the first thing we have right here is a method from a linear algebra library
which gives us the norm or the magnitude of whatever vector we put into it.
We, of course, are inputting the vector from the spaceship to the earth.
Now over here, we can see in the original equation that the denominator
is just the norm of that vector squared.
Within the code, we have it to the 3rd power.
Now, the reason for this is that we are also multiplying by that same vector again.
Whereas in the equation we're multiplying by the unit vector.
This means that in the Python version of the equation,
we effectively have the magnitude cubed in the denominator
and the magnitude to the 1st power, the numerator which gives overall
a factor of 1 over the magnitude squared which is exactly what we see in the original equation.
This makes our job a little bit easier because we don't have to define
a unit vector for the vector to earth.
Now we move on to the definition of the function ship trajectory.
Since we gave you all of this code, we skip down here to this final for loop.
This is something that we've seen several times before.
For example, in the last problem set these two lines implement the Forward Euler Method
to show how position and velocity are going to change with each step.
Once we have all of this code filled in, we can run the program, and our final product is this picture.
Now this picture looks a lot like what we would expect from elliptical orbit.
We have the earth here and the spaceship moving around it.
Now everything looks pretty normal until we get almost to the end
and we realized the ellipse actually won't close.
This is a great example of how the Forward Euler Method is a method using approximations.
If we are using more exact expressions for position and velocity,
then we would end up with a closed ellipse and we can also play with the number of steps that we used
to show how increasing the steps gives us a more exact approximation
of the trajectory of the spaceship.
Now we've done something that I think is pretty cool right here.
We've changed the number of steps from 13,000 to 1.3 million.
This means that since we cut the step size the same,
this picture shows what the position of the spaceship would be like
after much more time has passed after it has gone through many, many orbits.
So even though in that initial orbit, the trajectory only seems slightly off.
If we let that happen over and over and over again,
eventually the spaceship will be much further away from earth than it was originally.
Now, just to play with this example a little bit more, we've gone in and first change the step size,
so that's 1/10 of the size that it used to be.
This should give us a much more exact approximation
since we're taking steps more closely together.
However, we've also had to change the number of steps from the original 13,000 up to 130,000,
so that we still have the same number of seconds total that the spaceship is in orbit
which is a result of this changed code.
At first, it might look pretty similar to what we had after we ran the program the first time.
However, if you'd examine the end of the trajectory a little bit more closely,
you can see that this tail looks like it's actually going to intersect the beginning.
This time, it actually looks like the ellipse might close
which we remember is what the trajectory in real life would look like.
This is a great example of how changing the step size and using the Forward Euler Method
can greatly decrease the error and the results we produce.
In the next unit, we'll look at another numerical method
that deals with similar situations but in a slightly different way.