1. Implementing and Comparing ODE Methods#

We implemented and compared three numerical methods for solving ordinary differential equations (ODEs).

1.1. Numerical Methods#

1.1.1. Explicit Euler#

\[ x_{n+1} = x_n + h\, f(t_n, x_n) \]

1.1.2. Implicit Euler#

\[ x_{n+1} = x_n + h\, f(t_{n+1}, x_{n+1}) \]

1.1.3. Improved Euler (Heun’s Method)#

\[ x_{n+1} = x_n + \frac{h}{2} \left( f(t_n, x_n) + f\bigl(t_{n+1}, x_n + h\, f(t_n, x_n)\bigr) \right) \]

1.2. Test Problem#

To investigate the long-time stability of these methods, we solve the second-order ODE

\[ \ddot{y} = -y \]

which describes a simple harmonic oscillator.

Rewriting it as a first-order system yields

\[ \dot{y} = v \]
\[ \dot{v} = -y \]

The exact solution conserves the total energy

\[ E = \frac{1}{2}\left(v^2 + y^2\right) \]

making this problem well suited for studying numerical stability and energy conservation.


1.3. Numerical Results#

The following plots show the behavior of the numerical solutions for larger time intervals:

  • Phase portrait: velocity vs. position

  • Position vs. time

  • Velocity vs. time

1.3.1. Phase Portrait#

Phase portrait

1.3.2. Position vs. Time#

Position vs Time

1.3.3. Velocity vs. Time#

Velocity vs Time


1.4. Discussion#

The plots indicate that:

  • The explicit Euler method adds energy to the system, leading to an unstable spiral in phase space.

  • The implicit Euler method removes energy, causing the solution to decay over time.

  • The improved Euler method conserves energy much better and closely reproduces the expected periodic motion.