Runge-Kutta Calculator

Type y' = f(x, y), the starting point, the step size and the end value.

Write y' = f(x, y), such as y' = x + y, or just the right side.

The starting value of x.

The value of y at the start.

The step size, more than 0. The distance from x₀ to the end must be a whole number of steps.

The value of x to stop at, more than x₀. At most 1000 steps.

Try

Result

You enteredy′=x+y,y(0)=1, h=0.1y' = x + y,\quad y(0) = 1,\ h = 0.1

y(0.5)≈1.797441\displaystyle y(0.5) \approx 1.797441

Exact solution

y=2ex−x−1\displaystyle y = 2e^{x} - x - 1

Exact value

y(0.5)=1.797443\displaystyle y(0.5) = 1.797443

Error

∣error∣=0.000001\displaystyle |\text{error}| = 0.000001
Show the work
  1. Fourth-order Runge–Kutta

    Each step takes four slopes: at the start, twice at the midpoint (using the first two estimates) and at the end, and uses their weighted average (k1+2k2+2k3+k4)/6(k_1 + 2k_2 + 2k_3 + k_4)/6.

    Using yn+1=yn+h6(k1+2k2+2k3+k4)y_{n+1} = y_n + \frac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4)

    k1=f(xn,yn), k2=f(xn+h2,yn+h2k1), k3=f(xn+h2,yn+h2k2), k4=f(xn+h,yn+hk3)yn+1=yn+h6(k1+2k2+2k3+k4)\begin{gathered} k_1 = f(x_n, y_n),\ k_2 = f\left(x_n + \tfrac{h}{2}, y_n + \tfrac{h}{2}k_1\right),\ k_3 = f\left(x_n + \tfrac{h}{2}, y_n + \tfrac{h}{2}k_2\right),\ k_4 = f(x_n + h, y_n + h k_3) \\ y_{n+1} = y_n + \tfrac{h}{6}(k_1 + 2k_2 + 2k_3 + k_4) \end{gathered}
  2. Step 1

    From (x0,y0)=(0,1)(x_0, y_{0}) = (0, 1).

    k1=1, k2=1.1, k3=1.105, k4=1.2105;y1=1+0.16(1+2⋅1.1+2⋅1.105+1.2105)=1.110342\begin{gathered} k_1 = 1,\ k_2 = 1.1,\ k_3 = 1.105,\ k_4 = 1.2105; \\ y_1 = 1 + \tfrac{0.1}{6}\left(1 + 2\cdot 1.1 + 2\cdot 1.105 + 1.2105\right) = 1.110342 \end{gathered}
  3. Step 2

    From (x1,y1)=(0.1,1.110342)(x_1, y_{1}) = (0.1, 1.110342).

    k1=1.210342, k2=1.320859, k3=1.326385, k4=1.44298;y2=1.110342+0.16(1.210342+2⋅1.320859+2⋅1.326385+1.44298)=1.242805\begin{gathered} k_1 = 1.210342,\ k_2 = 1.320859,\ k_3 = 1.326385,\ k_4 = 1.44298; \\ y_2 = 1.110342 + \tfrac{0.1}{6}\left(1.210342 + 2\cdot 1.320859 + 2\cdot 1.326385 + 1.44298\right) = 1.242805 \end{gathered}
  4. Step 3

    From (x2,y2)=(0.2,1.242805)(x_2, y_{2}) = (0.2, 1.242805).

    k1=1.442805, k2=1.564945, k3=1.571052, k4=1.69991;y3=1.242805+0.16(1.442805+2⋅1.564945+2⋅1.571052+1.69991)=1.399717\begin{gathered} k_1 = 1.442805,\ k_2 = 1.564945,\ k_3 = 1.571052,\ k_4 = 1.69991; \\ y_3 = 1.242805 + \tfrac{0.1}{6}\left(1.442805 + 2\cdot 1.564945 + 2\cdot 1.571052 + 1.69991\right) = 1.399717 \end{gathered}
  5. Table of steps

    Every step with its four slopes.

    nxnynk1k2k3k4001⋅⋅⋅⋅10.11.11034211.11.1051.210520.21.2428051.2103421.3208591.3263851.4429830.31.3997171.4428051.5649451.5710521.6999140.41.5836481.6997171.8347031.8414521.98386250.51.7974411.9836482.1328312.140292.297677\begin{array}{ccccccc} n & x_n & y_n & k_1 & k_2 & k_3 & k_4 \\ \hline 0 & 0 & 1 & \cdot & \cdot & \cdot & \cdot \\ 1 & 0.1 & 1.110342 & 1 & 1.1 & 1.105 & 1.2105 \\ 2 & 0.2 & 1.242805 & 1.210342 & 1.320859 & 1.326385 & 1.44298 \\ 3 & 0.3 & 1.399717 & 1.442805 & 1.564945 & 1.571052 & 1.69991 \\ 4 & 0.4 & 1.583648 & 1.699717 & 1.834703 & 1.841452 & 1.983862 \\ 5 & 0.5 & 1.797441 & 1.983648 & 2.132831 & 2.14029 & 2.297677 \end{array}
  6. Compare with the exact solution

    This equation has an exact solution, so the numerical value can be checked: y=2ex−x−1y = 2e^{x} - x - 1 gives y(0.5)=1.797443y(0.5) = 1.797443, an error of 0.0000010.000001.

    y(0.5)=1.797443∣error∣=0.000001\begin{gathered} y(0.5) = 1.797443 \\ |\text{error}| = 0.000001 \end{gathered}
  7. Result

    The value after 55 steps.

    y(0.5)≈1.797441y(0.5) \approx 1.797441

Worked examples