使用scipy.integrate.dblquad进行区域估算

时间:2015-07-17 20:34:37

标签: python scipy

我尝试使用scipy.integrate.dblquad计算区域,但我不理解这个结果(半径为1的圆的整数):

import scipy.integrate as integ

def cercle(x,y):
    if x**2+y**2<=1 : return 1
    else : return 0 


In [12]: integ.dblquad(cercle, -1,1, lambda y: -1,lambda y:1)
Out[12]: (1.5248947619635096, 4.083599258741799e-08)

为什么我没有找到合适的区域?

2 个答案:

答案 0 :(得分:2)

在这里工作“OKish”,但可能会使用更好的例程。

python 2.7.9

scipy 0.14.1

>>> def c(x,y):
...     if x**2+y**2<=1:
...         return 1
...     else:
...         return 0
... 
>>> c(2,2)
0
>>> c(.5,.5)
1
>>> from scipy.integrate import dblquad
>>> dblquad(c, -1, 1, lambda y: -1, lambda y:1)
/usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.py:321: IntegrationWarning: The maximum number of subdivisions (50) has been achieved.
  If increasing the limit yields no improvement it is advised to analyze 
  the integrand in order to determine the difficulties.  If the position of a 
  local difficulty can be determined (singularity, discontinuity) one will 
  probably gain from splitting up the interval and calling the integrator 
  on the subranges.  Perhaps a special-purpose integrator should be used.
  warnings.warn(msg, IntegrationWarning)
(3.1421278363209537, 0.0002045333175209052)
>>> 

答案 1 :(得分:0)

我在运行代码时得到了预期的答案。

>>> def cercle(x,y):
...     if x**2+y**2<=1 : return 1
...     else : return 0 
... 
>>> integ.dblquad(cercle, -1,1, lambda y: -1,lambda y:1)
(3.1421278363209537, 0.0002045333175209052)

检查您最初运行的代码;如果你的一个论点中有拼写错误,它可能会解释你的错误。 例如,将积分的第一个限制更改为.1而不是-1会导致:

>>> integ.dblquad(cercle, .1,1, lambda y: -1,lambda y:1)
(1.3711257717509415, 5.8123454403774075e-05)