Solving by hand a first-order ODE using the fourth-order Runge-Kutta method.
Use the classical fourth-order Runge-Kutta method to solve the ODE \frac{d y}{d x}=-1.2 y+7 e^{-0.3 x} from x=0 to x=1.5 with the initial condition y=3 at x=0.
Solve by hand using h=0.5.
Compare the results with the exact (analytical) solution: y=\frac{70}{9} e^{-0.3 x}-\frac{43}{9} e^{-1.2 x}.
The first point of the solution is (0,3), which is the point where the initial condition is given. The values of x and y at the first point are x_{1}=0 and y_{1}=3.
The rest of the solution is done in steps. In each step the next value of the independent variable is calculated by:
x_{i+1}=x_{i}+h=x_{i}+0.5 (10.88)
The value of the dependent variable y_{i+1} is calculated by first evaluating K_{1}, K_{2}, K_{3} and K_{4} using Eq. (10.87):
\begin{aligned}& K_{1}=f\left(x_{i}, y_{i}\right) \\& K_{2}=f\left(x_{i}+\frac{1}{2} h, y_{i}+\frac{1}{2} K_{1} h\right) \\& K_{3}=f\left(x_{i}+\frac{1}{2} h, y_{i}+\frac{1}{2} K_{2} h\right) \\& K_{4}=f\left(x_{i}+h, y_{i}+K_{3} h\right)\end{aligned} (10.89)
and then substituting the Ks in Eq. (10.86):
y_{i+1}=y_{i}+\frac{1}{6}\left(K_{1}+2 K_{2}+2 K_{3}+K_{4}\right) h (10.90)
First step: In the first step i=1. Equations (10.88)-(10.90) give: x_{2}=x_{1}+0.5=0+0.5=0.5
K_{1}=-1.2 y_{1}+7 e^{-0.3 x_{1}}=-1.2 \cdot 3+7 e^{-0.3 \cdot 0}=3.4
x_{1}+\frac{1}{2} h=0+\frac{1}{2} \cdot 0.5=0.25 \quad y_{1}+\frac{1}{2} K_{1} h=3+\frac{1}{2} \cdot 3.4 \cdot 0.5=3.85
K_{2}=-1.2\left(y_{1}+\frac{1}{2} K_{1} h\right)+7 e^{-0.3\left(x_{1}+\frac{1}{2} h\right)}=-1.2 \cdot 3.85+7 e^{-0.3 \cdot 0.25}=1.874
y_{1}+\frac{1}{2} K_{2} h=3+\frac{1}{2} \cdot 1.874 \cdot 0.5=3.469
K_{3}=-1.2\left(y_{1}+\frac{1}{2} K_{2} h\right)+7 e^{-0.3\left(x_{1}+\frac{1}{2} h\right)}=-1.2 \cdot 3.469+7 e^{-0.3 \cdot 0.25}=2.331
y_{1}+K_{3} h=3+2.331 \cdot 0.5=4.166
K_{4}=-1.2\left(y_{1}+K_{3} h\right)+7 e^{-0.3\left(x_{1}+h\right)}=-1.2 \cdot 4.166+7 e^{-0.3 \cdot 0.5}=1.026
y_{2}=y_{1}+\frac{1}{6}\left(K_{1}+2 K_{2}+2 K_{3}+K_{4}\right) h=3+\frac{1}{6}(3.4+2 \cdot 1.874+2 \cdot 2.331+1.026) \cdot 0.5=4.069
At the end of the first step: x_{2}=0.5, y_{2}=4.069
Second step: In the second step i=2. Equations (10.88)-(10.90) give:
x_{3}=x_{2}+0.5=0.5+0.5=1.0K_{1}=-1.2 y_{2}+7 e^{-0.3 x_{2}}=-1.2 \cdot 4.069+7 e^{-0.3 \cdot 0.5}=1.142
x_{2}+\frac{1}{2} h=0.5+\frac{1}{2} \cdot 0.5=0.75 \quad y_{2}+\frac{1}{2} K_{1} h=4.069+\frac{1}{2} \cdot 1.142 \cdot 0.5=4.355
K_{2}=-1.2\left(y_{2}+\frac{1}{2} K_{1} h\right)+7 e^{-0.3\left(x_{2}+\frac{1}{2} h\right)}=-1.2 \cdot 4.355+7 e^{-0.3 \cdot 0.75}=0.3636
y_{2}+\frac{1}{2} K_{2} h=4.069+\frac{1}{2} \cdot 0.3636 \cdot 0.5=4.16
K_{3}=-1.2\left(y_{2}+\frac{1}{2} K_{2} h\right)+7 e^{-0.3\left(x_{2}+\frac{1}{2} h\right)}=-1.2 \cdot 4.16+7 e^{-0.3 \cdot 0.75}=0.5976
y_{2}+K_{3} h=4.069+0.5976 \cdot 0.5=4.368
K_{4}=-1.2\left(y_{2}+K_{3} h\right)+7 e^{-0.3\left(x_{2}+h\right)}=-1.2 \cdot 4.368+7 e^{-0.3 \cdot 1.0}=-0.0559
y_{3}=y_{2}+\frac{1}{6}\left(K_{1}+2 K_{2}+2 K_{3}+K_{4}\right) h=4.069+\frac{1}{6}[1.142+2 \cdot 0.3636+2 \cdot 0.5976+(-0.0559)] \cdot 0.5=4.32
At the end of the second step: x_{3}=1.0, y_{3}=4.32
Third step: In the third step i=3. Equations (10.88)-(10.90) give:
x_{4}=x_{3}+0.5=1.0+0.5=1.5K_{1}=-1.2 y_{3}+7 e^{-0.3 x_{3}}=-1.2 \cdot 4.32+7 e^{-0.3 \cdot 1.0}=0.001728
x_{3}+\frac{1}{2} h=1.0+\frac{1}{2} \cdot 0.5=1.25 \quad y_{3}+\frac{1}{2} K_{1} h=4.32+\frac{1}{2} \cdot 0.001728 \cdot 0.5=4.320
K_{2}=-1.2\left(y_{3}+\frac{1}{2} K_{1} h\right)+7 e^{-0.3\left(x_{3}+\frac{1}{2} h\right)}=-1.2 \cdot 4.32+7 e^{-0.3 \cdot 1.25}=-0.373
y_{3}+\frac{1}{2} K_{2} h=4.32+\frac{1}{2} \cdot(-0.373) \cdot 0.5=4.227
K_{3}=-1.2\left(y_{3}+\frac{1}{2} K_{2} h\right)+7 e^{-0.3\left(x_{3}+\frac{1}{2} h\right)}=-1.2 \cdot 4.227+7 e^{-0.3 \cdot 1.25}=-0.2614
y_{3}+K_{3} h=4.32+(-0.2614) \cdot 0.5=4.189
K_{4}=-1.2\left(y_{3}+K_{3} h\right)+7 e^{-0.3\left(x_{3}+h\right)}=-1.2 \cdot 4.189+7 e^{-0.3 \cdot 1.5}=-0.5634
y_{4}=y_{3}+\frac{1}{6}\left(K_{1}+2 K_{2}+2 K_{3}+K_{4}\right) h=4.32+\frac{1}{6}[0.001728+2 \cdot(-0.373)+2 \cdot(-0.2614)+(-0.5634)] \cdot 0.5=4.167
At the end of the third step: x_{4}=1.5, y_{4}=4.167
A comparison between the numerical solution and the exact solution is shown in the following table and figure. The error is 0.003 . The global truncation error in the second-order Runge-Kutta method is of the order of h^{4}. In this problem h^{4}=0.5^{4}=0.0625, which is smaller than the actual error. It should be remembered, however, that in the error term the h^{4} is multiplied by a constant whose value is not known. This shows that the estimates of truncation errors are good for comparing the accuracy of different methods, but they do not necessarily give an accurate numerical value for the error.
\begin{array}{lllll}i & 1 & 2 & 3 & 4 \\ x_{i} & 0.0 & 0.5 & 1.0 & 1.5 \\ y_{i} \text { (numerical) } & 3.0 & 4.069 & 4.32 & 4.167 \\ y_{i} \text { (exact) } & 3.0 & 4.072 & 4.323 & 4.170 \\ \text { Error } & 0.0 & 0.003 & 0.003 & 0.003\end{array}