python - 在 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 将表达式变成了一个函数。
任何人都可以帮忙吗?
解决方案
推荐阅读
- javascript - 如何根据列值突出显示工作表中的重复行?
- r - 转换数据框并在 R 中汇总
- c# - Factory.StartNew 阻止了代码执行
- python - 使用字符串连接+eval() 来构建 SQLAlchemy 查询可以吗,还是有更好的选择?
- java - 如何从 backStack 中删除堆栈?
- c# - 触摸后按钮保持按下状态(突出显示)
- c++ - 为什么我的类的复制初始化不适用于使用字符串文字的 string_view?
- ios - No Such Module 'Alamofire','SwiftyJson','UiKiT'
- php - transform data collection into laravel
- google-apps-script - onSubmit in apps script embedded in the new google site suddenly has issues