2014-01-26 18 views
1

我试图使用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.4 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

使用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() 

输出:

enter image description here

+0

太谢谢你了! –