首页 > 解决方案 > 在 Python 中实现系列扩展的分析集成

问题描述

我非常想在我的计算器中实现分析集成。在此尝试之前,我已经成功使用了数值积分。但是,我似乎无法使我的代码工作。我正在使用 scipy.integrate 模块,我得到的错误代码是TypeError: can't convert expression to float

我的代码是:

from sympy.functions import sin,cos,tan
from sympy.abc import x
from sympy import series
import scipy.integrate as integrate
import scipy.special as special

function1   = str(input("The function that should be expanded: "))
x0          = float(input("Point of developement: "))
n           = 1 + int(input("Grade of polynomial: "))
# input 0 -> n=1 -> konstant  (1 term, konstant)
# input 1 -> n=2 -> lineær    (2 termer, konstant + linear)
# input 2 -> n=3 -> kvadratisk (3 termer, konstant + linear + kvadratisk)
# ...

function2 = series(function1, x, x0, n)
print(function2)

a = float(input("Integrate from: "))
b = float(input("Integrate to: "))

def f(x):
    return function2.removeO()

x = Symbol('x')
result = integrate.quad(lambda x: f(x), a, b)

print("...................")
print("Here is your answer: ")
print(result)

我相信这可能是由于代码使用了不同的模块(sympy 和 scipy),或者我只需要以某种方式转换表达式。我尝试使用lambdify,但没有成功。

def f(x):
    return function2.removeO()
lam_f = lambdify(x, f(x))
result = integrate.quad(lambda x: lam_f, a, b)

并得到了另一个 TypeError,因为 lambdify 将表达式变成了一个函数。

任何人都可以帮忙吗?

标签: pythonscipyintegrationsympytaylor-series

解决方案


推荐阅读