Last active
December 2, 2018 23:51
-
-
Save mirianfsilva/e48f896e0b7a13bf25c2496f2c81a0e9 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
#Heun Method for solutions approach | |
import math, sys | |
import numpy as np | |
def Heun(f, y0, T, n): | |
"""Solve y'=f(t,y), y(0)=y0, with n steps until t=T.""" | |
t = np.zeros(n+1) | |
y = np.zeros(n+1) # y[k] is the solution at time t[k] | |
dt = T/float(n) | |
y[0] = y0 | |
t[0] = 0 | |
for k in range(n): | |
t[k+1] = t[k] + dt | |
y[k+1] = y[k] + (dt/2)*(f(y[k],t[k]) + f(y[k],t[k+1] + dt*f(y[k],t[k]))) | |
return y, t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment