Skip to content

Instantly share code, notes, and snippets.

@anthonymorast
Last active October 3, 2021 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonymorast/c549e7c84091c87fe55baf37d0597cea to your computer and use it in GitHub Desktop.
Save anthonymorast/c549e7c84091c87fe55baf37d0597cea to your computer and use it in GitHub Desktop.
coefficients.py
from scipy.misc import derivative
import math
class TaylorSeries():
def __init__(self, function, order, center=0):
self.center = center
self.f = function
self.order = order
self.d_pts = order*2
self.coefficients = []
# number of points (order) for scipy.misc.derivative
if self.d_pts % 2 == 0: # must be odd and greater than derivative order
self.d_pts += 1
self.__find_coefficients()
def __find_coefficients(self):
for i in range(0, self.order+1):
self.coefficients.append(round(derivative(self.f, self.center, n=i, order=self.d_pts)/math.factorial(i), 5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment