Homework 5#
Due on Friday Nov 3 by midnight.
1. An adaptive Runge-Kutta integrator [Solution]#
In the exercise on integrating planetary orbits, we looked at a circular orbit. The same equations apply for eccentric orbits as well, if you choose different initial conditions. For example, you can set an initial distance
To address this issue, implement an adaptive-step-size RK4 integrator and use it to integrate an eccentric orbit over a time
To choose the step size you can do the following:
Take an RK4 step with the current step size
and compare that with the result you get if you instead take two steps with half the step-size . The difference between the results gives you a measure of the error.If the error is smaller than the desired tolerance, then you keep the result and increase
by a factor of 2. If the error is larger than the desired tolerance, reject this step and try again with smaller by a factor of 2.Repeat. You will need to adjust the final step-size so that your final value of time is exactly at
.
Plot the orbit to make sure you are indeed getting an ellipse and that the orbit goes back to its starting point after a time
2. Method of lines [Solution]#
The method of lines is a way to do time-evolution of partial differential equations. For example, consider the thermal diffusion equation
where we set the thermal diffusivity to 1 for simplicity. We want to solve the following 1D diffusion problem: A piece of metal is initially at a temperature
In the method of lines, we transform the PDE into an ODE by finite differencing the spatial derivative on a grid in
where
Use this method to solve the problem described above. You should write your own implicit integrator using the techniques discussed in the notes (do not use solve_ivp
for this problem).
You will need to take care at the boundaries: the values of
Plot the temperature profile