In my last post I mentioned how I've been busy setting up the Lagrangian for a particle in a gravitational field and solving the associated equations of motion. This is my first blog post where I use LaTeX, so bear with me. The Lagrangian for this system is reproduced below. As the gravitational potential energy depends on only on radius, I thought it best to set this up in polar coordinates. Note that solving this numerically in cartesian coordinates is no big either!
L=TV
L=12m(r2˙θ2+˙r2)+GMmr
˙r=drdt
˙θ=dθdt
We arrive at the associated equations of motion for θ and r simply by the applying the Euler-Lagrange equations so as to minimize the action.
m¨r=mr˙θ2GMmr2
mr2¨θ+2mr˙r˙θ=0
Holy moly this looks like a mess. I don't even know how to start solving this analytically. Luckily, I have a powerful laptop at my disposal, so I can solve this system of couple ODE's numerically. Before I do anything though, I need to turn this into a system of first order ODE's. This is a clever little trick to simplify this problem quite a bit. The way I'll go about this is by creating new variables that correspond to my generalized coordinates r and θ and their first time derivatives ˙r and ˙θ. I'll give everything a subscript just to keep track.
r1=r;r2=˙r
θ1=θ;θ2=˙θ
Now, instead of having two 2nd order ODE's, I have a system of four 1st order ODE's. I can thus rewrite my original equations of motion in terms of these new variables r1,r2,θ1 and θ2. I've gotten rid of the m term in both equations to make things look nice.
˙r1=r2˙r2=r1θ22GMr21˙θ1=θ2˙θ2=2r2θ2r1
With expressions for the first derivatives, I can now march my initial conditions through time using a simple numerical method for ODE's. I implemented this in Python using forward and backward Euler's method. Backward Euler is much harder to implement, but it ended up being faster than forward Euler (despite having to solve a system of nonlinear equations at each time step) because the time step was considerably (about 2 or 3 orders of magnitude) larger. Instead of writing out r1,r2,θ1 and θ2, I'll define a vector r whose elements are the variables I'm solving for.
r=[r1r2θ1θ2]
Similarly,
˙r=[˙r1˙r2˙θ1˙θ2]
With this notation, forward Euler becomes
rn+1=rn+h˙rn
and backward Euler becomes
rn+1=rn+h˙rn+1
Where h is the time step. Note the implication of writing the n+1 subscript in the backward Euler equation; we now have to solve a system of nonlinear equations at every time step. This amounts to a lot of calculations! Luckily SciPy has a really fast nonlinear solver. All I have to do is feed it the jacobian for the system and some good initial guess, and it returns the solution in a very small number of iterations. This is no doubt because the method is based off of Newton's method, and the initial guess I use is the rn+1 I would have calculated using Newton's method. If you want to see the code, just hit me up and I'll send it!