Python 3导数近似误差与loglog图



我需要一些帮助来理解这一点。我看了这个YouTube视频(https://www.youtube.com/watch?v=q5pwy1NZqbM)它展示了如何在loglog图上绘制导数近似误差。我知道最终的图表显示了什么,但我不确定脚本的for循环中发生了什么。以下是完整的脚本:

import numpy as np
import matplotlib.pyplot as plt
def f(x):
return np.exp(-x**2)
def f_diff(x):
return -2*x*np.exp(-x**2)
def center(x,h):
return(f(x+h)-f(x-h))/(2*h)
def forward(x,h):
return (f(x+h)-f(x))/h
def backward(x,h):
return (f(x)-f(x-h))/h
def third_approx(x,h):
return (2*f(x+h)+3*f(x)-6*f(x-h)+f(x-2*h))/(6*h)
x0 = 0.2
h_vector = [10**(-temp) for temp in np.arange(0,17,0.1)]
forward_result = np.zeros(len(h_vector))
center_result = np.zeros(len(h_vector))
backward_result = np.zeros(len(h_vector))
third_approx_result = np.zeros(len(h_vector))
true_result = np.zeros(len(h_vector))
for index, i in enumerate(h_vector):
forward_result[index] = forward(x0,i)
center_result[index] = center(x0,i)
backward_result[index] = backward(x0,i)
third_approx_result[index] = third_approx(x0,i)
true_result[index] = f_diff(x0)
plt.figure()
plt.loglog(h_vector, abs(forward_result-true_result),label ='Forward')
plt.loglog(h_vector, abs(center_result-true_result),label='Center')
plt.loglog(h_vector, abs(backward_result-true_result),label='Backward')
plt.loglog(h_vector, abs(third_approx_result-true_result),label='third_approx')
plt.grid()
plt.xlabel('h')
plt.ylabel('Absolute difference')
plt.legend()
plt.show()

for循环对我来说真的很困惑。这就是我习惯的:

x = np.arange(0,10,0.1) #could also use np.linspace
n = x.size
dx = 0.1
FD = np.zeros(n)
BD = np.zeros(n)
CD = np.zeros(n)
third = np.zeros(n)
exact = np.zeros(n)
for i in range(n):
FD[i] = forward(x[i],dx)
BD[i] = backward(x[i],dx)
CD[i] = center(x[i],dx)
third[i] = third_approx(x[i],dx)
exact[i] = df(dx)

我从来没有见过一个for循环会说"for index,I in enumerate(x(:"为什么它说的是"for index"而不是"for I in range"?枚举是什么?我该如何将其转化为我更熟悉的for循环?

在咨询了我的教授之后,我可以回答我自己的问题。最初的for循环对大多数人来说相当混乱,但它可以被制作成大多数人都能识别的循环。首先,为了与对数对数图一致,将x空间设为负值,并用作10的指数。然后,所有使用这个新空间的函数,称为h_vvector,都被索引到h_vvecor的长度,以及for循环。在for循环的第一行中,将变量dx设置为等于索引的h_vvector。以下是这个问题的完整脚本:

import numpy as np
import matplotlib.pyplot as plt
def f(x):
return np.exp(-x**2)
def df(x):
return -2*x*np.exp(-x**2)
def center(f,x,h):
return(f(x+h)-f(x-h))/(2*h)
def forward(f,x,h):
return (f(x+h)-f(x))/h
def backward(f,x,h):
return (f(x)-f(x-h))/h
def third_approx(f,x,h):
return (2*f(x+h)+3*f(x)-6*f(x-h)+f(x-2*h))/(6*h)
x = np.linspace(0,3,100)
x0 = 0.2
h_vector = [10**(-temp) for temp in np.arange(0,17,0.1)]
nh = len(h_vector)
FD = np.zeros(nh)
BD = np.zeros(nh)
CD = np.zeros(nh)
third = np.zeros(nh)
exact = np.zeros(nh)
for i in range(nh):
dx = h_vector[i]
FD[i] = forward(f,x0,dx)
BD[i] = backward(f,x0,dx)
CD[i] = center(f,x0,dx)
third[i] = third_approx(f,x0,dx)
exact[i] = df(x0)
plt.figure()
plt.title('e^(-x^2) and Derivative')
plt.plot(x,f(x),label='e^(-x^2)')
plt.plot(x,df(x),label='df/dx(e^(-x^2))')
plt.legend()
plt.xlabel('x')
plt.ylabel('y')
plt.grid()
plt.show()
plt.figure()
plt.title('Difference between Derivative Approximations and Exact Value')
plt.loglog(h_vector,abs(FD-exact),label='Forward difference')
plt.loglog(h_vector,abs(BD-exact),label='Backward difference')
plt.loglog(h_vector,abs(CD-exact),label='Center difference')
plt.loglog(h_vector,abs(third-exact),label='Exact value')
plt.legend()
plt.xlabel('10^(-x)')
plt.ylabel('10^(-y)')
plt.grid()
plt.show()

请注意此脚本与上面的原始脚本之间的差异。导数近似由三个变量表示:f、x和h。在for循环中,它们由f、x0和dx表示,分别对应于f、x、h。

这个脚本可以与x和y中的任何方程一起使用。只需将方程写在f(x(函数中,将其导数写在df(x(函式中即可。

相关内容

  • 没有找到相关文章

最新更新