我正在集成一些非常糟糕的函数,而scipy.integrate.quad并没有很好地处理这种情况。我计划将mpmath.quad与tanh-sinh方法一起使用,但我需要将一些参数传递给正在计算的函数,如下所示:
mpmath.quad(f,[0,mpmath.pi],method='tanh-sinh',args=(arg_1, arg_2))因为f被定义为
f(x,arg_1, arg_2)在医生上没有找到类似的东西。有什么建议吗?
谢谢!
发布于 2011-06-07 02:39:09
使用lambda:
import mpmath
arg_1 = 1
arg_2 = 9
print mpmath.quad(lambda x: f(x, arg_1, arg_2), ...)发布于 2020-05-16 19:01:46
只需提示一下,通过tanh_sinh (我的一个软件包),也可以在没有mpmath的情况下使用tanh-sinh求积。如果你的函数有额外的参数,你可以只包装函数,如下所示:
import tanh_sinh
import numpy
def fun(x, a):
return a * numpy.exp(x) * numpy.cos(x)
val, error_estimate = tanh_sinh.integrate(
lambda x: fun(x, 1),
0,
numpy.pi / 2,
1.0e-14,
# Optional: Specify first and second derivative for better error estimation
# f_derivatives={
# 1: lambda x: numpy.exp(x) * (numpy.cos(x) - numpy.sin(x)),
# 2: lambda x: -2 * numpy.exp(x) * numpy.sin(x),
# },
)
print(val, error_estimate)与传递参数相比,我更喜欢这种方法,因为它更明确。
https://stackoverflow.com/questions/6256249
复制相似问题