# Example 1

Perform four steps of each of Euler's method, Heun's method, and 4th-order Runge Kutta on the IVP

y^{(1)}(*t*) = y(*t*) t + t - 1

y(0) = 1

In this case, the function f(*t*, *y*) = *y* *t* + *t* - 1.
Let *h* = (1 - 0)/4 = 0.25 and thus *t*_{0} = 0, *t*_{1} = 0.25, *t*_{2} = 0.5,
*t*_{3} = 0.75, *t*_{4} = 1 and *y*_{0} = 1. Then

### Euler's method

*y*_{1} = *y*_{0} + *h* f(*t*_{0}, *y*_{0}) = 0.75

*y*_{2} = *y*_{1} + *h* f(*t*_{1}, *y*_{1}) = 0.609375

*y*_{3} = *y*_{2} + *h* f(*t*_{2}, *y*_{2}) = 0.560546875

*y*_{4} = *y*_{3} + *h* f(*t*_{3}, *y*_{3}) = 0.603149414

### Heun's method

K_{0} = f(*t*_{0}, *y*_{0}) = -1

K_{1} = f(*t*_{0} + *h*, *y*_{0} + *h* K_{0}) = -0.5625000000

y_{1} = *y*_{0} + ½ *h* (K_{0} + K_{1}) = 0.8046875000

K_{0} = f(*t*_{1}, *y*_{1}) = -0.5488281250

K_{1} = f(*t*_{1} + *h*, *y*_{1} + *h* K_{0}) = -0.1662597656

y_{2} = *y*_{1} + ½ *h* (K_{0} + K_{1}) = 0.7153015137

K_{0} = f(*t*_{2}, *y*_{2}) = -0.1423492432

K_{1} = f(*t*_{2} + *h*, *y*_{2} + *h* K_{0}) = 0.259785652

y_{3} = *y*_{2} + ½ *h* (K_{0} + K_{1}) = 0.7299810648

K_{0} = f(*t*_{3}, *y*_{3}) = 0.297485799

K_{1} = f(*t*_{3} + *h*, *y*_{3} + *h* K_{3}) = 0.804352515

y_{4} = *y*_{3} + ½ *h* (K_{0} + K_{1}) = 0.8677108540

### 4th-order Runge Kutta

K_{0} = f(*t*_{0}, *y*_{0}) = -1

K_{1} = f(*t*_{0} + ½*h*, *y*_{0} + ½*h*K_{0}) = -0.7656250000

K_{2} = f(*t*_{0} + ½*h*, *y*_{0} + ½*h*K_{1}) = -0.7619628906

K_{3} = f(*t*_{0} + *h*, *y*_{0} + *h*K_{0}) = -0.5476226806

y_{1} = *y*_{0} + *h* (K_{0} + 2 K_{1} + 2 K_{2} + K_{3})/6 = 0.8082167307

K_{0} = f(*t*_{1}, *y*_{1}) = -0.5479458173

K_{1} = f(*t*_{1} + ½*h*, *y*_{1} + ½*h*K_{0}) = -0.3476036862

K_{2} = f(*t*_{1} + ½*h*, *y*_{1} + ½*h*K_{1}) = -0.3382126488

K_{3} = f(*t*_{1} + *h*, *y*_{1} + *h*K_{0}) = -0.1381682158

y_{2} = *y*_{1} + *h* (K_{0} + 2 K_{1} + 2 K_{2} + K_{3})/6 = 0.7224772847

K_{0} = f(*t*_{2}, *y*_{2}) = -0.1387613576

K_{1} = f(*t*_{2} + ½*h*, *y*_{2} + ½*h*K_{0}) = 0.065707572

K_{2} = f(*t*_{2} + ½*h*, *y*_{2} + ½*h*K_{1}) = 0.081681707

K_{3} = f(*t*_{2} + *h*, *y*_{2} + *h*K_{0}) = 0.307173284

y_{3} = *y*_{2} + *h* (K_{0} + 2 K_{1} + 2 K_{2} + K_{3})/6 = 0.7417768882

K_{0} = f(*t*_{3}, *y*_{3}) = 0.306332666

K_{1} = f(*t*_{3} + ½*h*, *y*_{3} + ½*h*K_{0}) = 0.557559912

K_{2} = f(*t*_{3} + ½*h*, *y*_{3} + ½*h*K_{1}) = 0.585037893

K_{3} = f(*t*_{3} + *h*, *y*_{3} + *h*K_{0}) = 0.888036361

y_{4} = *y*_{3} + *h* (K_{0} + 2 K_{1} + 2 K_{2} + K_{3})/6 = 0.8867587481

The correct answer is y(1) = 0.8867564079.

The plot of the solution and the field plot are shown in Figure 1.

Figure 1. The field plot and the solution.

The approximations are shown in Figure 2.

Figure 2. The solution and the three approximations: Euler (blue), Heun (light blue), and 4th-order Runge Kutta (red)

Copyright ©2005 by Douglas Wilhelm Harder. All rights reserved.