泰勒展開式筆記
透過python 套件可以處理泰勒展開式,Taylor series,用來approach 所有函數。
泰勒展開式公式:
$$ f(x)= \lim_{n \to \infty} f(0) + \frac {df}{dx} \frac{x^{1}}{1!} + \frac{d^{2}f}{dx} \frac{x^{2}}{2!} + …+ \frac{d^{n}f}{dx} \frac{x^{n}}{n!}$$
透過python 以Sin(x) 為例:
使用python 透過 泰勒展開式計算sinx(3),重複50個循環。
from __future__ import division
from sympy import *
from sympy import diff, Symbol, sin
x, k= symbols('x k ', commutative=True)
m = 3
iter = 50
f0= sin(x)
fx= sin(x)
sol = f0.evalf(subs={'x': 0})
real= f0.evalf(subs={'x':m})
for i in range(1,iter):
fx= diff(fx,x)
k = fx.evalf(subs={'x': 0})/math.factorial(i)
sol = sol + (3**i)*k
print('real=',real )
print('sol=',sol)