0

I need to compare the maximum deviation of the interpolated function to the true value of the functionf(x)=exp(x). I don't know how to find the x-value where this occurs as i used x=np.linspace() to plot the interpolation and the true function.

My task was first to interpolate linearly with f(x)=exp(x) given the following values x=[0,1,2] and then again with x=[0,0.5,1,1.5,2].(which I have done)

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)))
Myunghee
  • 1
  • 1

1 Answers1

1

If you want to find the x with the biggest error, from the sampled points, you'll need to find the index of the biggest error in a error array with the np.argmax function:

# 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]
memoselyk
  • 3,993
  • 1
  • 17
  • 28