Skip to content

Instantly share code, notes, and snippets.

@AnuragAnalog
Created July 21, 2021 07:12
Show Gist options
  • Save AnuragAnalog/470df451f613e47a7619f2a973fd7a8f to your computer and use it in GitHub Desktop.
Save AnuragAnalog/470df451f613e47a7619f2a973fd7a8f to your computer and use it in GitHub Desktop.
Milane Method Implementation in Python
#!/usr/bin/python3.6
# A finite-difference method for the solution of the Cauchy problem for systems of first-order ordinary differential equations:
#
# dy/dx = f(x, y)
#
# The method uses the finite-difference formula:
# yi - yi-1 = 2hf(xi-1, yi-1).
# xi = x0+ih, i = 0,1,2,3,.....
#
# The predictor-corrector Milne method uses a pair of finite-difference formulas:
# a predictor
# yi = yi-4 + 4h(2y'i-3 - y'i-2 + 2y'i-1)/3, i = 4,5,6,7,....,
# and a corrector
# yi = yi-2 + h(y'i-2+4y'i-1+y'i)
def dybydx(x, y):
fx = (x + y)
return fx
def milnep(x, y, delx, ydash, n):
for i in range(4):
ydash.append(dybydx(x[i], y[i]))
i = 4
while i < n:
tmp = y[i-4] + (4*delx*(2*ydash[i-3]-ydash[i-2]+2*ydash[i-1])/3)
y.append(tmp)
x.append(x[i-1] + delx)
ydash.append(dybydx(x[i], y[i]))
print("x%i = %f and y%i = %f."% (i, x[i], i, y[i]))
i = i + 1
def milnec(x, y, delx, ydash, n):
print("\nBy corrector's method: ")
i = 4
while i < n:
ycheck = y[i-2] + delx*(ydash[i-2]+4*ydash[i-1]+ydash[i])/3
print("y%i = %f" % (i, ycheck))
i = i + 1
x = list()
y = list()
ydash = list()
for i in range(4):
tmp = float(input("Enter the value of x%i: "% i))
x.append(tmp)
tmp = float(input("Enter the value of y%i: "% i))
y.append(tmp)
delx = float(input("Enter the value of Δx: "))
n = int(input("Enter the no. of iterations: ")) + 4
milnep(x, y, delx, ydash, n)
milnec(x, y, delx, ydash, n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment