Created
December 2, 2018 23:50
-
-
Save mirianfsilva/ca0a2d85e1a4bb3e845e314f44831789 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 | |
def Euler(f, y0, T, n): | |
"""Solve y’= f(y,t), 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] | |
y[0] = y0 | |
t[0] = 0 | |
dt = T/float(n) | |
for k in range(n): | |
t[k+1] = t[k] + dt | |
y[k+1] = y[k] + 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