Last active
December 2, 2018 23:51
-
-
Save mirianfsilva/2538fe7883425e7fc3ba84809ab3d1da to your computer and use it in GitHub Desktop.
Method to find solution of ordinary differential equations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math, sys | |
import numpy as np | |
#Adams-Bashforth technique: Predictor | |
#Adams-Moulton technique: Corrector | |
def PredictorCorrector(f, y0, T, n): | |
#Use RungeKutta4 | |
"""Solve y'=f(t,y), y(0)=y0, with n steps until t=T.""" | |
t = np.zeros(n+2) | |
y = np.zeros(n+2) | |
y[0] = y0 | |
t[0] = 0 | |
dt = T/float(n) | |
print("\nH value:",dt) | |
y, t = RungeKutta4(f, y0, T, n) | |
f0 = f(y[0],t[0]) | |
f1 = f(y[1],t[1]) | |
f2 = f(y[2],t[2]) | |
f3 = f(y[3],t[3]) | |
#f4 = f(y[4],t[4]) | |
for k in range(n-1,0,-1): | |
#Predictor: The fourth-order Adams-Bashforth technique, an explicit four-step method, is defined as: | |
y[k+1] = y[k] + (dt/24) *(55*f3 - 59*f2 + 37*f1 - 9*f0) | |
f4_AB = f(y[k+1],t[k+1]) | |
#Corrector: The fourth-order Adams-Moulton technique, an implicit three-step method, is defined as: | |
y[k+1] = y[k] + (dt/24) *(9*f(y[k+1],t[k+1]) + 19*f3 - 5*f2 + f1) | |
y[k+1] = y[k] + (dt/24) *(9*f(y[k+1],t[k+1]) + 19*f3 - 5*f2 + f1) | |
return y, t | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment