Numerical Methods

When analytical solutions are not possible, we can use numerical methods to approximate the solution of an ODE. This is common because the ODEs are derived from physical laws. Given any differential equation, for example,

we can convert to a first-order system (involving at most the first derivative). Let velocity and acceleration . Then we have

Let . The ODE becomes .

Forward Euler Method

We can discretize time into time-frames.

Then, we calculate the next state by using the current state and the derivative . The general idea is that

such that

Recall that is a vector-valued function that describes the change in the state at time .

Advantages

  • There is an explicit formula formula for the next state.
  • Computationally fast and easy to implement. Limitations
  • Not very accurate, unless is tiny. The error is proportional to from the Taylor expansion of .
  • Can be energy increasing (violates conservation of energy).

Backward Euler Method

The backward Euler method is an implicit method that calculates the next state using the derivative at the next state rather than the current state. The formula is given by

Advantages

  • Energy decreasing (dissipating). This “looks” physical, i.e. it is more realistic as we expect energy to dissipate over time.
  • Can take large time steps without instability.
  • Can incorporate collision.

Limitations

  • Not very accurate, unless is tiny.
  • We have to solve implicitly for at each step, which can be computationally expensive.

Runge-Kutta Method (RK4)

This method is accurate, stable, and explicit.

The idea is that we take a weighted average of the slopes at different points within the time step to get a better estimate of the next state. In most cases, the RK4 method works very well. There are other special algorithms (non-RK4) that are deigned to preserve energy or momentum.

  • Variational integrator
  • Symplectic integrator
  • Lie group integrator

Example 1

Consider the pendulum equation . Given at time , we can use the RK4 method to compute at time .

Recall from example that

(we omit for simplicity). Then, we can compute the intermediate slopes as follows:

To prepare for , we need to use to project halfway forward . This gives

such that

Then, we can compute using the projected state:

Next, we can compute by projecting forward again using :

such that

Then we can compute using the projected state:

Finally, we can compute by projecting forward using :

such that

Then, we can compute using the projected state:

Plugging everything into the RK4 formula, we get

The choice of is crucial for the accuracy and stability of the numerical solution. Graphically, if , the numerical solution follows the true solution closely. However, if , we preserve no information about the true solution and the numerical solution diverges significantly from the true solution.

Example 1.2 (2nd Order Discretization)

We can also use a second-order discretization method to solve the pendulum equation, or central difference approximation for a second derivative (in our case, acceleration). We consider

and take the average to find the acceleration:

But recall , so we can rearrange to get