Skip to content

Instantly share code, notes, and snippets.

@friedbrice
Created November 17, 2017 01:02
Show Gist options
  • Save friedbrice/63e0ee5afb9fa0f53496c13f92c5ebf0 to your computer and use it in GitHub Desktop.
Save friedbrice/63e0ee5afb9fa0f53496c13f92c5ebf0 to your computer and use it in GitHub Desktop.
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