## Heun's Method

Back to Programming

### Description

This method can be defined as an improvement over Euler’s method. The errors introduced by the use of Euler’s method and the buildup of these errors by the repeated application of the method are reduced by using the improved Euler method called Heun’s method.

DERIVATION

In order to solve the first-order differential equation:

The solution starts from the initial value  at  and in the first step it is taken as  and then the value of ${\mathrm{y}}_{1}$ of the solution is computed.

Using this value of ${\mathrm{y}}_{1}$ the next value ${\mathrm{y}}_{2}$ is computed at . In a similar way, the value of y is carried out at the discrete values of x i.e. .

Each step of the Euler method is performed using a formula which is derived from Taylor’s series as follows:

Here

Where

Now we take p = 1, which gives

Therefore,

We can write the formula to find the value of y1by using the initial values of ${\mathrm{x}}_{0}$ and ${\mathrm{y}}_{0}$ as:

And the value of  is evaluated as

Then we obtain,

Thus modified recursion relation can be given as:

COMPARISON WITH EULER’S METHOD

Considering the initial value problem as:

Then the error is given by

Now

And

Then

Now,

In the case of Euler’s method, the error is of the order ${\mathbf{h}}^{\mathbf{2}}$, hence we can say that the improved Euler method or Heun’s method is more accurate.

GRAPHICAL REPRESENTATION

### Algorithm

INPUT:

Initial value of x and f(x) and the value of x for which y is to be calculated

OUTPUT:

The value of y for given x

PROCESS:

``````Step 1: [Taking the inputs from the user]
Read x, y [the initial values of x and y]
Read h [the interval i.e. the gapping between two consecutive x]
Read xn [the value of x for which the y is to be calculated]

Step 2: [Heun’s Method]
While x + h ≤ xn repeat
Set l ← (h/2) × (f(x, y) + f(x + h, y + h × f(x, y)))
[f(x, y) is a function described in Step 3]
Set y ← y + l
Set x ← x + h
Print y,x
[End of ‘while’ loop]
[End of Heun’s method]

Step 3: [function f(x, y)]
Return 2y/x
[End of function ‘f(x, y)’]

Step 4: Stop.``````

### Code

1. It is better than the Euler method as the error is reduced.

2. It is a second-order convergent so that it is more efficient than Euler’s method.