4

次の関数を検討してください。

import numpy as np
from scipy.special import erf

def my_func(x):
    return np.exp(x ** 2) * (1 + erf(x))

この関数の積分を からの関数-14-4使用して評価すると、次の結果が得られます。scipyquad

In [3]: from scipy import integrate

In [4]: integrate.quad(my_func, -14, -4)
/usr/local/lib/python2.7/dist-packages/scipy/integrate/quadpack.py:289: UserWarning: 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)
Out[4]: (0.21896647054443383, 0.00014334175850538866)

つまり、約0.22.

ただし、この積分をWolfram Alphaに送信すると、非常に異なる結果が得られます。

-5.29326 X 10 ^ 69.

どうしたんだ?scipyこれは、私に与えられた警告と関係があると思います。でこの積分を評価する最良の方法は何pythonですか?

: 値を大きくするlimitと警告は変わりますが、scipy結果は変わりません。

In [5]: integrate.quad(my_func, -14, -4, limit=10000)
/usr/local/lib/python2.7/dist-packages/scipy/integrate/quadpack.py:289: UserWarning: The occurrence of roundoff error is detected, which prevents 
  the requested tolerance from being achieved.  The error may be 
  underestimated.
  warnings.warn(msg)
Out[5]: (0.21894780966717864, 1.989164129832358e-05)
4

1 に答える 1