我想知道为什么有些数据点出现在1e-16,以及如何调整绘图以隐藏这些数据点
代码如下:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
变量f4是:
def f4(x):
if x>0.125 and x<0.125+1/3:
return 1
return 0
然后右和中点函数:
def rightrs (f, a, b, n):
h = (b-a)/n
s = 0.
x = a + h
for i in range (n):
s += f(x)
x += h
return s * h
def midpoint (f, a, b, n):
h = (b-a)/n
s = 0.
x = a + h/2.
for i in range(n):
s += f(x)
x += h
return s*h
最后,我用以下代码绘制了它:
ns = np.arange (1, 1000)
error_midpoint = np.zeros(len(ns))
error_rightrs = np.zeros(len(ns))
for i in range(len(ns)):
error_midpoint[i] = midpoint(f4, 0, 2, ns[i]) - 1./3.
error_rightrs[i]= rightrs(f4, 0, 2, ns[i]) - 1./3.
h = 2./ns
plt.loglog(h, error_rightrs, ".")
plt.loglog(h, error_midpoint, ".")
下面是图表:
有人能告诉我为什么有些数据点出现在1e-16上,以及如何调整绘图以隐藏这些数据点吗
有些数据点显示在1e-16,因为这就是它们的值。可以通过在绘图的y轴上设置适当的限制来隐藏它们。试试
plt.ylim
额外意见:
应使用绝对值或标准计算误差
对于这些类型的收敛测试,您应该选择对数标度上的
n
,即1,2,4,8,16相关问题 更多 >
编程相关推荐