标签归档:runtime-error

ValueError:数学域错误

问题:ValueError:数学域错误

我只是从“ 使用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

我将其范围缩小到了日志,因为当我删除日志并添加其他功能时,它可以工作。我认为这是由于对底座的某种干扰,我不知道怎么做。谁能提出解决方案?

I was just testing an example from Numerical Methods in Engineering with 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)

When I run it, it shows the following error:

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

I have narrowed it down to the log as when I remove log and add a different function, it works. I assume it is because of some sort of interference with the base, I can’t figure out how. Can anyone suggest a solution?


回答 0

您的代码执行log的a小于或等于零。这在数学上是未定义的,因此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]值的来源,但希望这将引导您走上正确的道路。

Your code is doing a log of a number that is less than or equal to zero. That’s mathematically undefined, so Python’s log function raises an exception. Here’s an example:

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

Without knowing what your newtonRaphson2 function does, I’m not sure I can guess where the invalid x[2] value is coming from, but hopefully this will lead you on the right track.


回答 1

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

对数在被赋予一个数字和被赋予的底数后找出底数。log(0)意味着某种提升力量的东西20。指数永远不会导致0*,这意味着log(0)没有答案,因此抛出math domain error

*注意:0^0可以产生0,但也可以同时产生1。这个问题被激烈参数。

You are trying to do a logarithm of something that is not positive.

Logarithms figure out the base after being given a number and the power it was raised to. log(0) means that something raised to the power of 2 is 0. An exponent can never result in 0*, which means that log(0) has no answer, thus throwing the math domain error

*Note: 0^0 can result in 0, but can also result in 1 at the same time. This problem is heavily argued over.


回答 2

您也可以使用math.log1p

根据官方文件

math.log1p(x)

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

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

You may also use math.log1p.

According to the official documentation :

math.log1p(x)

Return the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero.

You may convert back to the original value using math.expm1 which returns e raised to the power x, minus 1.


回答 3

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

you are getting math domain error for either one of the reason : either you are trying to use a negative number inside log function or a zero value.