範例10-2:f(1.97)泰勒級數估計值大概為-76.03,請計算精確值 #多項式函數: f(x) = x**3 - x**2 - 40x -1 #以2為參考點(x-2),轉成泰勒級數 from sympy import * x = symbols('x') f = Function('y')(x) f = x**3 -x**2 -40*x -1 ty1 = series(f, x, x0=2, n=4) print('泰勒級數的精確值=', ty1.subs(x,1.97)) print('f(x=2)的精確值=', f.subs(x,1.97)) print('泰勒級數用一次項的估計值=', -76.03) #印出標準方程式 from sympy.interactive import printing printing.init_printing(use_latex=True) display(f) display(ty1) #畫出兩條圖 p1 = plot(f,ty1) p1[0].line_color = 'red' p1[1].line_color = 'blue' p1[0].label = '$f(x)$' p1[1].label = '$taylor series$' p1.legend = True p1.show()