Last active
December 2, 2018 23:52
-
-
Save mirianfsilva/7b17338eceefb7795801f691a52cfba1 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
#Midpoint Method for solutions approach | |
import math, sys | |
import numpy as np | |
def Midpoint(f, y0, T, n): | |
"""Solve y'=f(t,y), y(0)=y0, with n steps until t=T.""" | |
dt = T/float(n) | |
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 | |
for k in range(n): | |
t[k+1] = t[k] + dt | |
y[k+1] = y[k] + dt*f(y[k], t[k]+(dt/2) +(dt/2)*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