Heun's Method 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.794894\displaystyle y(0.5) \approx 1.794894

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.002549\displaystyle |\text{error}| = 0.002549
Show the work
  1. Heun's method (improved Euler)

    Euler's method uses only the slope at the start of a step, so it lags behind a curving solution. Heun's method first PREDICTS the end of the step with Euler, takes the slope there too, and moves with the average of the two slopes.

    Using yn+1=yn+h2[f(xn,yn)+f(xn+1,yn+hf(xn,yn))]y_{n+1} = y_n + \frac{h}{2}\left[f(x_n, y_n) + f(x_{n+1}, y_n + h f(x_n, y_n))\right]

    k1=f(xn,yn)k2=f(xn+h, yn+hk1)yn+1=yn+h2(k1+k2)\begin{gathered} k_1 = f(x_n, y_n) \\ k_2 = f(x_n + h,\ y_n + h k_1) \\ y_{n+1} = y_n + \tfrac{h}{2}(k_1 + k_2) \end{gathered}
  2. Step 1

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

    k1=1, k2=f(0.1, 1.1)=1.2;y1=1+0.12(1+1.2)=1.11\begin{gathered} k_1 = 1,\ k_2 = f(0.1,\ 1.1) = 1.2; \\ y_1 = 1 + \tfrac{0.1}{2}\left(1 + 1.2\right) = 1.11 \end{gathered}
  3. Step 2

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

    k1=1.21, k2=f(0.2, 1.231)=1.431;y2=1.11+0.12(1.21+1.431)=1.24205\begin{gathered} k_1 = 1.21,\ k_2 = f(0.2,\ 1.231) = 1.431; \\ y_2 = 1.11 + \tfrac{0.1}{2}\left(1.21 + 1.431\right) = 1.24205 \end{gathered}
  4. Step 3

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

    k1=1.44205, k2=f(0.3, 1.386255)=1.686255;y3=1.24205+0.12(1.44205+1.686255)=1.398465\begin{gathered} k_1 = 1.44205,\ k_2 = f(0.3,\ 1.386255) = 1.686255; \\ y_3 = 1.24205 + \tfrac{0.1}{2}\left(1.44205 + 1.686255\right) = 1.398465 \end{gathered}
  5. Table of steps

    Every step with its two slopes.

    nxnynk1k2001⋅⋅10.11.1111.220.21.242051.211.43130.31.3984651.442051.68625540.41.5818041.6984651.96831250.51.7948941.9818042.279985\begin{array}{ccccc} n & x_n & y_n & k_1 & k_2 \\ \hline 0 & 0 & 1 & \cdot & \cdot \\ 1 & 0.1 & 1.11 & 1 & 1.2 \\ 2 & 0.2 & 1.24205 & 1.21 & 1.431 \\ 3 & 0.3 & 1.398465 & 1.44205 & 1.686255 \\ 4 & 0.4 & 1.581804 & 1.698465 & 1.968312 \\ 5 & 0.5 & 1.794894 & 1.981804 & 2.279985 \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.0025490.002549.

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

    The value after 55 steps.

    y(0.5)≈1.794894y(0.5) \approx 1.794894

Worked examples