ValueError:数学域错误

时间:2013-04-08 22:58:46

标签: python runtime-error logarithm

我只是用使用Python的工程中的数值方法来测试一个例子。

from numpy import zeros, array
from math import sin, log
from newtonRaphson2 import *

def f(x):
    f = zeros(len(x))
    f[0] = sin(x[0]) + x[1]**2 + log(x[2]) - 7.0
    f[1] = 3.0*x[0] + 2.0**x[1] - x[2]**3 + 1.0
    f[2] = x[0] + x[1] + x[2] -5.0
    return f

x = array([1.0, 1.0, 1.0])
print newtonRaphson2(f,x)

当我运行它时,它会显示以下错误:

File "example NR2method.py", line 8, in f
    f[0] = sin(x[0]) + x[1]**2 + log(x[2]) - 7.0
ValueError: math domain error

我已将其缩小到日志,因为当我删除日志并添加其他功能时,它可以正常工作。我认为这是因为对基地的某种干扰,我无法弄清楚如何。有人可以提出解决方案吗?

5 个答案:

答案 0 :(得分:100)

您的代码正在执行小于或等于零的数字log。这在数学上是未定义的,因此Python的log函数引发了异常。这是一个例子:

>>> from math import log
>>> log(-1)
Traceback (most recent call last):
  File "<pyshell#59>", line 1, in <module>
    log(-1)
ValueError: math domain error

在不知道newtonRaphson2函数的作用的情况下,我不确定我能猜出无效x[2]值的来源,但希望这会引导您走上正确的轨道。

答案 1 :(得分:2)

您也可以使用math.log1p

根据official documentation

math.log1p(x)

返回1 + x的自然对数(以e为底)。结果 是对x接近零的精确方法进行计算的。

您可以使用math.expm1转换回原始值,该值将把e的乘幂x减1。

答案 2 :(得分:0)

我一直遇到类似的错误。它看起来像是matplotlib问题。重新启动我的ipython会话可以为我解决问题。

~/uqing/forreal100/analysis.py in corrf(a, nam1, nam2)
     60    plt.axis('equal')
     61    plt.title(r'$\rho^2 = %f$' % a.corr()['second']['des'] ** 2)
---> 62    plt.savefig(nam1 + nam2 + '.pdf')
     63    plt.clf()
     64    plt.close('all')

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/pyplot.py in savefig(*args, **kwargs)
    695 def savefig(*args, **kwargs):
    696     fig = gcf()
--> 697     res = fig.savefig(*args, **kwargs)
    698     fig.canvas.draw_idle()   # need this if 'transparent=True' to reset colors
    699     return res

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/figure.py in savefig(self, *args, **kwargs)
   1570             self.set_frameon(frameon)
   1571 
-> 1572         self.canvas.print_figure(*args, **kwargs)
   1573 
   1574         if frameon:

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2242                 orientation=orientation,
   2243                 bbox_inches_restore=_bbox_inches_restore,
-> 2244                 **kwargs)
   2245         finally:
   2246             if bbox_inches and restore_bbox:

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/backends/backend_pdf.py in print_pdf(self, filename, **kwargs)
   2523                 RendererPdf(file, image_dpi, height, width),
   2524                 bbox_inches_restore=_bbox_inches_restore)
-> 2525             self.figure.draw(renderer)
   2526             renderer.finalize()
   2527         finally:

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     61     def draw_wrapper(artist, renderer, *args, **kwargs):
     62         before(artist, renderer)
---> 63         draw(artist, renderer, *args, **kwargs)
     64         after(artist, renderer)
     65 

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/figure.py in draw(self, renderer)
   1141 
   1142             mimage._draw_list_compositing_images(
-> 1143                 renderer, self, dsu, self.suppressComposite)
   1144 
   1145             renderer.close_group('figure')

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/image.py in _draw_list_compositing_images(renderer, parent, dsu, suppress_composite)
    137     if not_composite or not has_images:
    138         for zorder, a in dsu:
--> 139             a.draw(renderer)
    140     else:
    141         # Composite any adjacent images together

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/artist.py in draw_wrapper(artist, renderer, *args, **kwargs)
     61     def draw_wrapper(artist, renderer, *args, **kwargs):
     62         before(artist, renderer)
---> 63         draw(artist, renderer, *args, **kwargs)
     64         after(artist, renderer)
     65 

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py in draw(self, renderer, inframe)
   2345             self.apply_aspect(pos)
   2346         else:
-> 2347             self.apply_aspect()
   2348 
   2349         artists = self.get_children()

/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/axes/_base.py in apply_aspect(self, position)
   1463         if aspect_scale_mode == "log":
   1464             dL = self.dataLim
-> 1465             dL_width = math.log10(dL.x1) - math.log10(dL.x0)
   1466             dL_height = math.log10(dL.y1) - math.log10(dL.y0)
   1467             xr = 1.05 * dL_width

ValueError: math domain error

答案 3 :(得分:0)

您正在尝试对非正数做对数。

对数在被赋予一个数字和被赋予的幂后计算出底数。 short表示提升到log(0)的力量的是2。指数永远不会导致0 *,这意味着0没有答案,因此抛出了log(0)

*注意:math domain error可以产生0^0,但也可以同时产生0。这个问题被激烈争论。

答案 4 :(得分:0)

您由于以下任一原因而遇到数学域错误: 您正试图在对数函数中使用负数或零值。