我只是从测试数值方法的例子工程中使用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
我已经把范围缩小到日志中,当我删除日志,并添加不同的功能,它的工作原理。 我认为这是因为某种与底座干扰的,我无法弄清楚如何。 任何人都可以提出一个解决办法?
您的代码是做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
不知道您有什么newtonRaphson2
功能呢,我不知道我能猜到其中的无效x[2]
值是从哪里来的,但希望这会导致你在正确的轨道上。
我一直得到一个类似的错误。 它看起来像一个matplotlib问题。 重新启动我的IPython的会议解决了这个问题对我来说。
~/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