找到插值函数的最大偏差发生在哪里?

时间:2015-12-08 22:17:07

标签: python interpolation numerical-methods linear-interpolation xvalue

我需要将插值函数的最大偏差与函数f(x)=exp(x)的真值进行比较。我不知道如何找到发生这种情况的x值,因为我使用x=np.linspace()来绘制插值和真实函数。

我的任务是首先使用f(x)=exp(x)线性插入x=[0,1,2],然后再使用x=[0,0.5,1,1.5,2]进行插值。(我已经完成了)

x_1=np.linspace(0,1,num=20)
x_2=np.linspace(1,2,num=20)
x_3=np.linspace(0,2,num=20)
y_1=np.empty(20)
y_2=np.empty(20)
y_3=np.empty(20)

def interpolation(x,a,b):
    m=(f(b)-f(a))/(b-a)
    z=f(a)
    y=m*(x-a)
    return y+z

n=0
for k in x_1:
    y_1[n]=interpolation(k,0,1)
    n+=1

n_1=0
for l in x_2:
    y_2[n_1]=interpolation(l,1,2)
    n_1+=1


x1=np.linspace(0,1,num=20)
x2=np.linspace(1,2,num=20)
y1=np.empty(20)
y2=np.empty(20)


n1=0
for p1 in x1:
    y1[n1]=f(p1)#true value of f(x)=exp(x)
    n1+=1

n2=0
for p2 in x2:
    y2[n2]=f(p2)
    n2+=1

#only gives the distance of the deviation, only idea I've got so far
print(max(abs(y1-y_1)))
print(max(abs(y2-y_2)))

1 个答案:

答案 0 :(得分:1)

如果要查找具有最大错误的x,从采样点开始,您需要找到error数组中# Given the following variables # x - x values # y_int - y values interpolated in the given range # y_eval - y values obtained by evaluating the function abs_error = abs(y_eval - y_int) index_max_error = abs_error.argmax() x_max_error = x[index_max_error] 数组中最大错误的索引功能:

{{1}}