Skip to content

Instantly share code, notes, and snippets.

@mirianfsilva
Last active December 2, 2018 23:51
Show Gist options
  • Save mirianfsilva/54329dbf3cda2bd2f5105112ce2c1a0f to your computer and use it in GitHub Desktop.
Save mirianfsilva/54329dbf3cda2bd2f5105112ce2c1a0f to your computer and use it in GitHub Desktop.
Method to find solution of ordinary differential equations
#Forward Euler
import math, sys
import numpy as np
def ForwardEuler(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
# Problem: y'=y
def f(t, y):
return y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment