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?
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.
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.
I kept getting a similar error. It looks like a matplotlib problem. Restarting my ipython session solved the problem for me.
~/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
来源:https://stackoverflow.com/questions/15890503/valueerror-math-domain-error