ram*_*.pm 89 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)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它显示以下错误:
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
Run Code Online (Sandbox Code Playgroud)
我已将其缩小到日志,因为当我删除日志并添加其他功能时,它可以正常工作.我认为这是因为对基地的某种干扰,我无法弄清楚如何.谁有人建议解决方案?
Blc*_*ght 113
您的代码正在执行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
Run Code Online (Sandbox Code Playgroud)
在不知道你的newtonRaphson2功能是什么的情况下,我不确定我能猜出无效x[2]值的来源,但希望这会引导你走上正轨.
| 归档时间: |
|
| 查看次数: |
243290 次 |
| 最近记录: |