以变量积分作为积分限制(python)
Integration with variable as an integration limit ( python)
我需要执行与 python 的集成,但限制之一是变量,而不是数字(从 0 到 z)。
我尝试了以下方法:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad
def I(z,y,a): #function I want to integrate
I = (a*(y*(1+z)**3+(1-y))**(0.5))**(-1)
return I
def dl(z,y,a): #Integration of I
dl = quad(I, 0, z, args =(z,y,a))
return dl
我遇到的问题是 dl(z,y,a)
给了我一个数组,所以每当我想绘制或评估它时,我都会得到以下内容:
ValueError: The truth value of an array with more than one element is ambiguous.
不知道advancedthat.Thanks有没有解决方法
我认为 quad
不接受向量值积分边界。因此,在这种情况下,您实际上必须循环 z
或使用 np.vectorize
.
编辑: 在您的代码中,您应该只将 args
参数作为 agrs=(y, a)
,不应包含 z。然后你可以通过索引返回的元组的第一个元素来访问集成的结果。
实际上quad
returns一个元组。元组中的第一个元素是您想要的结果。由于我无法毫无问题地获取您的代码 运行,因此我写了一些短代码。我不确定这是不是你想要的:
def I(a):
return lambda z, y: (a*(y*(1+z)**3+(1-y))**(0.5))**(-1)
def dl(z, y, a):
return quad(I(a), 0, z, args=(y))
print(dl(1,2,3)[0])
结果:
0.15826362868629346
Python 考虑因素是空列表(或可迭代)在转换为布尔值时为 false。假设您正在列出一些东西。
在进行计算时,您可能会考虑零向量 [0,0,0],它在线性代数中可能被视为零但不是空列表。
您的错误似乎来自非空检查。
正如其他答案所暗示的那样,您错误地调用了一个函数并提供了一个数组,其中包含一个数字。
用 I
调用 quad
的正确方法是:
In [20]: quad(I, 0, 10, args=(1,2))
Out[20]: (0.6984886554222364, 1.1361829471531105e-11)
正如 Longwen 所指出的,I
的第一个参数是 quad
变化的 z
。 (y,a)
是 quad
原封不动地传递给 I
的参数。
但是你得到了错误,因为你尝试使用数组作为 z
边界
In [21]: quad(I, 0, np.arange(3), args=(1,2))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-fbbfa9c0cd3f> in <module>()
----> 1 quad(I, 0, np.arange(3), args=(1,2))
/usr/local/lib/python3.5/dist-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
313 if (weight is None):
314 retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
--> 315 points)
316 else:
317 retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel,
/usr/local/lib/python3.5/dist-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
362 def _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points):
363 infbounds = 0
--> 364 if (b != Inf and a != -Inf):
365 pass # standard integration
366 elif (b == Inf and a != -Inf):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
ValueError when using if commands in function - 另一个最近的问题试图做同样的事情,使用数组作为积分边界。 post 给出了更多的错误回溯,因此更容易识别问题。
如果不是因为这个 ValueError,你的第 3 个术语 args
会产生不同的错误:
In [19]: quad(I, 0, 10, args=(10,1,2))
....
TypeError: I() takes 3 positional arguments but 4 were given
我需要执行与 python 的集成,但限制之一是变量,而不是数字(从 0 到 z)。
我尝试了以下方法:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad
def I(z,y,a): #function I want to integrate
I = (a*(y*(1+z)**3+(1-y))**(0.5))**(-1)
return I
def dl(z,y,a): #Integration of I
dl = quad(I, 0, z, args =(z,y,a))
return dl
我遇到的问题是 dl(z,y,a)
给了我一个数组,所以每当我想绘制或评估它时,我都会得到以下内容:
ValueError: The truth value of an array with more than one element is ambiguous.
不知道advancedthat.Thanks有没有解决方法
我认为 quad
不接受向量值积分边界。因此,在这种情况下,您实际上必须循环 z
或使用 np.vectorize
.
编辑: 在您的代码中,您应该只将 args
参数作为 agrs=(y, a)
,不应包含 z。然后你可以通过索引返回的元组的第一个元素来访问集成的结果。
实际上quad
returns一个元组。元组中的第一个元素是您想要的结果。由于我无法毫无问题地获取您的代码 运行,因此我写了一些短代码。我不确定这是不是你想要的:
def I(a):
return lambda z, y: (a*(y*(1+z)**3+(1-y))**(0.5))**(-1)
def dl(z, y, a):
return quad(I(a), 0, z, args=(y))
print(dl(1,2,3)[0])
结果:
0.15826362868629346
Python 考虑因素是空列表(或可迭代)在转换为布尔值时为 false。假设您正在列出一些东西。
在进行计算时,您可能会考虑零向量 [0,0,0],它在线性代数中可能被视为零但不是空列表。
您的错误似乎来自非空检查。 正如其他答案所暗示的那样,您错误地调用了一个函数并提供了一个数组,其中包含一个数字。
用 I
调用 quad
的正确方法是:
In [20]: quad(I, 0, 10, args=(1,2))
Out[20]: (0.6984886554222364, 1.1361829471531105e-11)
正如 Longwen 所指出的,I
的第一个参数是 quad
变化的 z
。 (y,a)
是 quad
原封不动地传递给 I
的参数。
但是你得到了错误,因为你尝试使用数组作为 z
边界
In [21]: quad(I, 0, np.arange(3), args=(1,2))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-fbbfa9c0cd3f> in <module>()
----> 1 quad(I, 0, np.arange(3), args=(1,2))
/usr/local/lib/python3.5/dist-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
313 if (weight is None):
314 retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
--> 315 points)
316 else:
317 retval = _quad_weight(func, a, b, args, full_output, epsabs, epsrel,
/usr/local/lib/python3.5/dist-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
362 def _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points):
363 infbounds = 0
--> 364 if (b != Inf and a != -Inf):
365 pass # standard integration
366 elif (b == Inf and a != -Inf):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
ValueError when using if commands in function - 另一个最近的问题试图做同样的事情,使用数组作为积分边界。 post 给出了更多的错误回溯,因此更容易识别问题。
如果不是因为这个 ValueError,你的第 3 个术语 args
会产生不同的错误:
In [19]: quad(I, 0, 10, args=(10,1,2))
....
TypeError: I() takes 3 positional arguments but 4 were given