我正在尝试用Python创建泰勒系列,但我不知道如何消除此错误:

x=Symbol("x")

def f(x):
    return ((math.e)**x)*sin(x)

y=f(x)

print(y.diff(x))

def Taylor(f,x,m,a):
    y=f(x)
    y2=f
    yargliige=0
    viga = 10**(-m)
    n=0
    while True:
        if n>10:
           return yargliige,n
        else:
           yargliige+=(y(x)*(x-a)**n)/(factorial(n))
           y=y.diff(x)
           if abs(yargliige(x)-f(x))<viga:
              return yargliige,n
        n+=1

print(Taylor(f,-0.3,3,-1))

我收到错误消息:
Traceback (most recent call last):
  File "C:\Users\arman\Desktop\Numbrilised meetodid\praktikum10.py", line 31, in <module>
    print(Taylor(f,-0.3,3,-1))
  File "C:\Users\arman\Desktop\Numbrilised meetodid\praktikum10.py", line 25, in Taylor
    yargliige+=(y(x)*(x-a)**n)/(factorial(n))
TypeError: 'Float' object is not callable

似乎原始函数不接受float,这似乎很荒谬。

最佳答案

您已经调用了y = f(x),它存储了从函数float返回的f
您无法执行y(),因为无法调用y
y = f(x)更改为y = f,它应该可以解决您的用例。

09-28 11:14