What is Heun's method?

Heun’s method is a second-order Runge-Kutta method that is also known as the improved Euler Method. Heun’s method is a method to solve Ordinary Differential Equations, given an initial condition.

Heun’s method is built upon the Euler method. The Euler method uses the tangent to the curve at the initial point to check for the next estimate. The ideal point would be where the tangent cuts the curve. However, the assumption of small step size producing small errors doesn’t always hold. In the case of a larger step size or concave up curve, the Euler method fails to converge, as it overestimates the next term.

Heun’s method overcomes this problem by taking two tangents on both sides of the curve. One tangent overestimates while the other underestimates. Heun’s method then uses the Euler method to estimate the next term from both of these tangents.

Formula

y(i+1)=yi+(k1+k2)(h)2y_{(i+1)}=y_{i}+ \frac{(k_{1}+k_{2})(h)}{2}

k1=f(xi,yi)k_{1}=f(x_{i},y_{i})

k2=f(xi+h,yi+k1h)k_{2}=f(x_{i}+h, y_{i}+k_{1}h)

Where:

  • k1k_{1} and k2k_{2} are over and underestimates.
  • hh is the step size.
  • xix_{i} and yiy_{i} are initial conditions.
import math
def func(x,y):
func = (3*math.exp(-x))-(0.4*y) #Example of ordinary differential equation
return func
def heun():
##Initial Conditions
x = 0 #Independent variable
y = 5
##Integration Limit
xi=0
xf=3
h=1.5 #stepsize
print("Iteration",0)
print("x:", x)
print("y:",y)
i=1
n=(xf-xi)/h #number of iterations
while i <= n: #This loop will run until the number of iterations are completed
k1 = func(x,y) #K1
k2 = func(x+h,y+(k1*h)) #K2
y = y+((k1+k2)*(h/2)) #Heun's formula to update y
x = x+h #updating x
print("Iteration",i)
print("x:", x)
print("y:",y)
i=i+1
def main():
heun()
main()

Free Resources