简体   繁体   English

使用matplotlib绘制导数,但情节混乱

[英]Draw derivatives using matplotlib, but the plot is out of order

I'm trying to plot 3 derivatives using numpy, scipy and matplotlib: 我正在尝试使用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()

I'm pretty sure that code is right ( apart matplotlib usages ), those derivatives should look like: 我非常确定代码是正确的(除了matplotlib用法),这些派生应如下所示:
http://i.stack.imgur.com/vk60t.jpg http://i.stack.imgur.com/vk60t.jpg
While my plot looks like: 虽然我的情节看起来像:
http://i.stack.imgur.com/Q4YRC.png http://i.stack.imgur.com/Q4YRC.png
What's interesting, the console output looks correct! 有趣的是,控制台输出看起来正确! Take a look: 看一看:

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

The question is, what am I doing wrong? 问题是,我在做什么错? Could it be a system issue? 可能是系统问题吗? ( Mac Os 10.9 ) (Mac OS 10.9)

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

output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM