我正在尝试使用numpy,scipy和matplotlib绘制3个衍生物:
import numpy as np
import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt
derivative1 = lambda x, h: (sp.sin(x + h) - sp.sin(x)) / h
derivative2 = lambda x, h: (sp.sin(x + (0.5 * h)) - sp.sin(x - (0.5 * h))) / h
derivative3 = lambda x, h: (sp.sin(x - 2 * h) - 8 * sp.sin(x - h) + 8 * sp.sin(x + h) - sp.sin(x + 2 * h)) / (12 * h)
f = lambda x: sp.log10(np.absolute(x))
precision = sp.cos(1.0)
h = np.logspace(-15, 0, 15000)
x = 1
for each in np.nditer(h):
# w1 = sp.log10(np.absolute(pochodna1(x, h) - precision))
w1 = derivative1(x, each) - precision
# w2 = sp.log10(np.absolute(pochodna2(x, h) - precision))
w2 = derivative2(x, each) - precision
# w3 = sp.log10(np.absolute(pochodna3(x, h) - precision))
w3 = derivative3(x, each) - precision
print "h: " + str(f(each)) + "\tw1: " + str(f(w1)) + "\tw2: " + str(f(w2)) + "\tw3: " + str(f(w3))
for x in np.nditer(h, op_flags=['readwrite']):
x[...] = sp.log10(x)
plt.xlim([-16, 0])
plt.ylim([-16, 0])
plt.plot(h, f(derivative1(x, h)), 'b')
plt.plot(h, f(derivative2(x, h)), 'g')
plt.plot(h, f(derivative3(x, h)), 'r')
plt.ylabel('some numbers')
plt.show()
我很确定代码是正确的(除了matplotlib用法),这些衍生物应该是这样的:
http://i.stack.imgur.com/vk60t.jpg
我的情节看起来像:
http://i.stack.imgur.com/Q4YRC.png
有趣的是,控制台输出看起来是正确的!看看:
h: -14.0259350623 w1: -2.00182231719 w2: -2.00182231719 w3: -2.09724184251
h: -14.0249349957 w1: -3.23630839896 w2: -3.23630839896 w3: -2.86031997278
h: -14.023934929 w1: -3.17801098526 w2: -3.17801098526 w3: -2.58187469552
h: -14.0229348623 w1: -2.72011796328 w2: -2.72011796328 w3: -2.41390146063
h: -14.0219347957 w1: -2.50261344062 w2: -2.50261344062 w3: -2.76389992399
(...)
h: -0.00800053336889 w1: -0.333826366348 w2: -1.66880928776 w3: -1.82637173239
h: -0.00700046669778 w1: -0.332799458932 w2: -1.66683332104 w3: -1.82260197262
h: -0.00600040002667 w1: -0.331772877536 w2: -1.66485746594 w3: -1.81883327888
h: -0.00500033335556 w1: -0.330746624583 w2: -1.66288172297 w3: -1.81506565611
h: -0.00400026668444 w1: -0.329720702513 w2: -1.66090609266 w3: -1.81129910927
h: -0.00300020001333 w1: -0.328695113776 w2: -1.65893057552 w3: -1.80753364333
h: -0.00200013334222 w1: -0.327669860835 w2: -1.65695517208 w3: -1.80376926331
h: -0.00100006667111 w1: -0.326644946168 w2: -1.65497988286 w3: -1.80000597424
h: 0.0 w1: -0.325620372264 w2: -1.65300470839 w3: -1.79624378116
问题是,我做错了什么?这可能是系统问题吗? (Mac Os 10.9)
答案 0 :(得分:0)
使用loglog()
:
import numpy as np
import scipy as sp
import matplotlib as mpl
import matplotlib.pyplot as plt
def derivative1(x, h):
return (sp.sin(x + h) - sp.sin(x)) / h
def derivative2(x, h):
return (sp.sin(x + (0.5 * h)) - sp.sin(x - (0.5 * h))) / h
def derivative3(x, h):
return (sp.sin(x - 2 * h) - 8 * sp.sin(x - h) + 8 * sp.sin(x + h) - sp.sin(x + 2 * h)) / (12 * h)
precision = sp.cos(1.0)
h = np.logspace(-15, 0, 15000)
for func in (derivative1, derivative2, derivative3):
plt.loglog(h, np.abs(func(1, h) - precision), label=func.__name__, alpha=0.6)
plt.legend()
输出: