Last active
December 2, 2018 23:53
-
-
Save mirianfsilva/e96bfdd9bfa694192c2aa44fc572e0ed 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
#Taylor's Approach | |
import math, sys | |
import numpy as np | |
def Taylor(f, y0, T, n): | |
"""Solve y'=f(t,y), 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 #t0 | |
dt = T/float(n) | |
print('h',dt) | |
for k in range(n): | |
t[k+1] = t[k] + dt | |
fk = f(y[k],t[k]) # f em t(k), y(k) | |
dfk = dy(y[k],t[k]) # dy de f em t(k), y(k) | |
ddfk = dyy(y[k],t[k]) # dyy de f em t(k), y(k) | |
t1 = dt * fk | |
t2 = ((dt**2)/2) * dfk * fk | |
t3 = ((dt**3)/6) * ((ddfk * (fk**2)) + ((dfk**2) * fk)) | |
y[k+1] = y[k] + t1 + t2 + t3 | |
return y, t | |
# Problem: y'=y | |
def f(t, y): | |
return y #define f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment