python - ValueError: math domain error -
i testing example numerical methods in engineering python.
from numpy import zeros, array math import sin, log 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 run it, shows 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 down log when remove log , add different function, works. assume because of sort of interference base, can't figure out how. can suggest solution?
your code doing log
of number less or equal zero. that's mathematically undefined, python's log
function raises exception. here's example:
>>> 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 newtonraphson2
function does, i'm not sure can guess invalid x[2]
value coming from, lead on right track.
Comments
Post a Comment