正弦函数的解析导数和近似导数的比较

comparison of analytical derivative and approximative derivatives of a sine function

我尝试比较正弦函数的解析导数和使用后向、前向、中心的近​​似导数,3.order 向后差异 method.I 它们之间有很大的差异。一定是错误的。你能告诉我我在哪里犯了错误吗?提前致谢!

%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt

from math import pi
from numpy import *



# spatial domain
xmin = 0
xmax = 1
n = 50 # num of grid points

# x grid of n points
x = np.linspace(xmin, xmax, n+1);

k=2


def f1(x):
    return np.sin(2*pi*k*x)
def f2(x):
    return np.pi*k*2*cos(2*pi*k*x)   #analytical derivative of f1 function

f = zeros(n+1,dtype=float)        # array to store values of f
fd=zeros(n+1,dtype=float)         # array to store values of fd
dfrwd = zeros(n+1,dtype=float)    # array to store values of calulated derivative with forward difference
dbwd = zeros(n+1,dtype=float)     # array to store values of calulated derivative with backward difference
dzd = zeros(n+1,dtype=float)      # array to store values of calulated derivative with central difference
ddo = zeros(n+1,dtype=float)      # array to store values of calulated derivative with 3.order backward difference

for i in range(0,n): # adds values to arrays for x and f(x)

    f[i] = f1(x[i])
    fd[i] = f2(x[i])
step=f[2]-f[1]

# periodic boundary conditions


dfrwd[n] = (f[0]-f[n])/step

dbwd[0] = (f[0]-f[n])/step

dzd[0]=(f[1]-f[n])/(2*step)
dzd[n]=(f[0]-f[n-1])/(2*step)

ddo[n] = (2*f[0]+3*f[n]-6*f[n-1]+f[n-2])/(6*step)
ddo[1] = (2*f[2]+3*f[1]-6*f[0]+f[n])/(6*step)
ddo[0] = (2*f[1]+3*f[0]-6*f[n]+f[n-1])/(6*step)


for i in range(0,n-1): # add values to array for derivative with forward difference
    dfrwd[i] = (f[i+1]-f[i])/step
for i in range(1,n): # add values to array for derivative with backward difference
    dbwd[i] = (f[i]-f[i-1])/step
for i in range(1,n-1): # add values to array for derivative with central difference
    dzd[i] = (f[i+1]-f[i-1])/(2*step)
for i in range(2,n-1): # add values to array for derivative with 3.order backward difference
    ddo[i] = (2*f[i+1]+3*f[i]-6*f[i-1]+f[i-2])/(6*step)




plt.plot(x,fd)
plt.plot(x,dzd)

您可能希望将 step 计算为 x[1]-x[0]


清理你的导入。您将 numpy 作为 np 导入和使用,但也作为所有内容的一般导入,np.sinf1 中,但仅在 cos 中导入 f2。您在同一公式中使用数学中的 pinp.pi。这一切都没有错,但令人困惑并使调试变得更加困难。

您可以使用 numpy 向量运算,f = f1(x); fd = f2(x)。同样,数值导数可以通过索引移位来计算。或者您可以将它们实现为 dfrwd = (f1(x+step)-f1(x))/step 等。效率稍低,但阅读起来更清晰。