Created
November 17, 2017 01:02
-
-
Save friedbrice/63e0ee5afb9fa0f53496c13f92c5ebf0 to your computer and use it in GitHub Desktop.
euler_approximations.py (adapted from https://gist.github.com/TheVizier/6e8289a52a72090618cea12a253dd8e1)
This file contains hidden or 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 | |
def f(T,Y): | |
"""An example first-order ODE, in the form y' = f(t,y). | |
Arguments: | |
T -- the independent variable | |
Y -- the dependent variable | |
""" | |
return math.exp(T)*math.sin(Y) | |
def euler(f,t_i,y_i,t_f,delta_t): | |
"""Compute the array of euler approximations of a first-order ODE. | |
Arguments: | |
f -- a first-order ODE, in the form y' = f(t,y) | |
t_i -- the initial value of the independent variable | |
y_i -- the initial value of the dependent variable | |
t_f -- the final value of the independent variable | |
delta_t -- the linearization step size | |
""" | |
t = float(t_i) | |
y = float(y_i) | |
dt = float(delta_t) | |
steps = round((float(t_f) - t)/dt) | |
approx = [ (t,y) ] | |
while len(approx) <= steps: | |
y = y + f(t,y)*dt | |
t = t + dt | |
approx.append( (t,y) ) | |
print(t,y) # added for debugging | |
return approx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment