Menu Close

Milne’s Method | Python

Milne’s method is a numerical method used to solve ordinary differential equations (ODEs) of the form y'(x) = f(x, y(x)), where y(x) is the unknown function and f(x, y(x)) is a given function.

It is a predictor-corrector method, which means that it uses an initial approximation (the “predictor”) to estimate the value of the unknown function at a new point, and then uses this estimate to obtain a more accurate value (the “corrector”).

The method is based on Taylor series expansions of the unknown function, and consists of the following steps:

  1. Given an initial value y(x_0) = y_0 and a step size h, use a one-step method (such as Euler’s method) to estimate y(x_0 + h) = y_1.
  2. Use the estimate y_1 to compute a Taylor series expansion of the unknown function around x_0, up to and including the second derivative: y(x) = y_0 + y'(x_0)(x – x_0) + (1/2)y”(x_0)(x – x_0)^2 + O((x – x_0)^3)
  3. Use the estimate y_1 to compute an improved estimate of the value of the unknown function at x_0 + h, denoted y_2.
  4. Repeat the process to estimate y(x_0 + 2h), y(x_0 + 3h), and so on.

The method is a 4th order method, which means that the global truncation error of this method is of the order O(h^5) which means it is more accurate than Euler’s method which is of order O(h^2)

It is worth mentioning that Milne’s method is less common to use in practice than other methods like Runge-Kutta method and Adams-Bashforth method.

Here is a basic example of how to use Milne’s method to solve the simple ODE y'(x) = -y(x) with the initial value y(0) = 1.

Milne’s Method in Python

					import numpy as np

def milnes_method(f, y0, x0, x_end, h):
    # Initialize the solution array
    x = np.arange(x0, x_end+h, h)
    y = np.zeros(len(x))
    y[0] = y0
    #Milne's Method [By Bottom Science]
    # Iterate over the steps
    for i in range(1, len(x)):
        y_pred = y[i-1] + h*f(x[i-1], y[i-1])
        y_corrected = y[i-1] + (h/4)*(3*f(x[i], y_pred) - f(x[i-1], y[i-1]))
        y[i] = y_corrected
    return x, y

# Define the ODE function
def f(x, y):
    return -y

# Set the initial condition and the step size
y0 = 1
x0 = 0
x_end = 10
h = 0.1

# Solve the ODE
x, y = milnes_method(f, y0, x0, x_end, h)

for i in range(len(x)):
    print("x = ",x[i]," y = ",y[i])

More Related Stuff