I am trying to get the coefficients of a Taylor Series in an array. I need the value of the coefficients for an arbitrary function.
There are two ways I am trying, but I do not know which one is the correct one. Can anyone help me?
Here is my code
def function(x):
return swish(x)
def Maclaurin(func, n, order):
coefficients = []
for i in range(order+1):
coefficients.append(((-1)**i * (func(i))**(2*i+1)) / np.math.factorial(2*i+1)) #first try
coefficients.append(func(i) / math.factorial(i)) #second try
return coefficients
print(Maclaurin(function, 0, 3))
Here are example implementations of sine and cosine Taylor series in Python that don't require calls to a factorial function.
Here's how I'd do the Maclaurin series using SymPy:
As Mark Dickerson pointed out, the Maclaurin series expands a function about a point
x = 0. This function gives you a list of coefficients that will have to be evaluated and summed to give you the value. The Taylor series for sine and cosine above include plots of error. Note that the errors become larger the further you get from the pointx = 0. Series expansions aren't intended to wander far away from their expansion point.