我只是在用 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
我已将其范围缩小到日志,因为当我删除日志并添加不同的功能时,它可以工作。我认为这是因为对基地的某种干扰,我不知道是怎么回事。任何人都可以提出解决方案吗?