Training Numerical Methods Numerical ODEs — Euler & Runge-Kutta
4 / 5

Numerical ODEs — Euler & Runge-Kutta

25 min Numerical Methods

Numerical ODEs — Euler & Runge-Kutta

Initial Value Problem

Solve $y' = f(t,y)$, $y(t_0) = y_0$ by computing approximations $y_1, y_2, \ldots$ at $t_1, t_2, \ldots$

Euler's Method

$$y_{n+1} = y_n + h\,f(t_n, y_n)$$

First-order accurate: local error $O(h^2)$, global error $O(h)$.

Classical Runge-Kutta (RK4)

$$y_{n+1} = y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)$$

where

  • $k_1 = f(t_n, y_n)$
  • $k_2 = f(t_n+h/2, y_n+hk_1/2)$
  • $k_3 = f(t_n+h/2, y_n+hk_2/2)$
  • $k_4 = f(t_n+h, y_n+hk_3)$

Fourth-order: global error $O(h^4)$.

Example 1

Euler's method for $y' = y$, $y(0)=1$, $h=0.5$, two steps.

$y_1 = 1 + 0.5(1) = 1.5$.

$y_2 = 1.5 + 0.5(1.5) = 2.25$.

Exact $y(1) = e \approx 2.718$. Error $\approx 17\%$.

Example 2

Euler for $y' = -2y$, $y(0)=3$, $h=0.1$, one step.

$y_1 = 3 + 0.1(-6) = 2.4$. Exact: $3e^{-0.2} \approx 2.456$.

Example 3

Why is RK4 preferred over Euler's method?

RK4 has $O(h^4)$ accuracy vs. $O(h)$ for Euler. With $h=0.1$, RK4's error is roughly $10^{-4}$ per step versus $10^{-1}$ for Euler.

Practice Problems

1. Euler: $y' = t+y$, $y(0)=1$, $h=0.1$. Find $y_1$.
2. Two Euler steps for $y' = y$, $y(0)=1$, $h=0.25$.
3. What happens to Euler's accuracy as $h \to 0$?
4. Euler: $y' = -y$, $y(0)=2$, $h=0.5$, two steps. Compare to exact.
5. What is a stiff ODE?
6. RK4: $k_1$ for $y'=y$, $y(0)=1$, $h=0.5$?
7. For $y' = 2t$, $y(0)=0$, what is the exact solution?
8. Apply Euler with $h=0.5$ to Problem 7 for two steps.
9. Improved Euler (Heun's method) formula?
10. Why does smaller $h$ mean more computation?
11. Global error order for RK4?
12. Euler: $y' = \cos t$, $y(0)=0$, $h=\pi/4$. Find $y_1$.
Show Answer Key

1. $y_1 = 1+0.1(0+1) = 1.1$

2. $y_1 = 1.25$, $y_2 = 1.5625$. Exact $= e^{0.5} \approx 1.6487$.

3. Improves (converges to exact solution)

4. $y_1 = 2+0.5(-2)=1$; $y_2=1+0.5(-1)=0.5$. Exact: $2e^{-1} \approx 0.736$.

5. ODE where explicit methods need very small $h$ for stability

6. $k_1 = f(0,1) = 1$

7. $y = t^2$

8. $y_1 = 0+0.5(0)=0$; $y_2 = 0+0.5(1)=0.5$. Exact $y(1)=1$.

9. $\tilde{y} = y_n+hf(t_n,y_n)$; $y_{n+1} = y_n + \frac{h}{2}[f(t_n,y_n)+f(t_{n+1},\tilde{y})]$

10. More steps to cover the same interval

11. $O(h^4)$

12. $y_1 = 0 + (\pi/4)\cos(0) = \pi/4 \approx 0.785$. Exact $y(\pi/4) = \sin(\pi/4) \approx 0.707$.