For a simple harmonic oscillator $\ddot{y} + y = 0$, with initial conditions $y(0) = 1, \dot{y}(0) = 0$, find $y(t)$ from $t = 0$ to $100 \pi$. Use an Euler method and a fixed-step fourth-order Runge-Kutta method. For each method check how the average local error, and the error in the final value and slope, depend on the step size.

We know that the exact solution of this system is:

$$y(t) = \cos{t} \tag{1}$$

To solve this numerically, we have two variables to integrate: $y$ and $\dot{y}$. We'll use this state vector:

$$z = \begin{bmatrix}y \\ \dot{y}\end{bmatrix}$$

The system can then be formulated in a matrix form:

$$\begin{bmatrix}\dot{y} \\ \ddot{y}\end{bmatrix} = f(t, z(t)) = \begin{bmatrix}0 & 1\\ -1 & 0\end{bmatrix} \begin{bmatrix}y \\ \dot{y}\end{bmatrix}$$

Let's solve this numerically with both an Euler integrator, and a Runge Kutta of order 4. Thanks to Python's callable objects, I can just pass a reference to either function and use it as the integrator: