0

J'ai besoin de comparer l'écart maximum de la fonction interpolée à la vraie valeur de la fonction f(x)=exp(x). Je ne sais pas comment trouver la valeur x où cela se produit que j'ai utilisé x=np.linspace() pour tracer l'interpolation et la vraie fonction.trouve où l'écart maximum d'une fonction interpolée se produit?

Ma tâche a d'abord été interpoler linéairement avec f(x)=exp(x) étant donné les valeurs suivantes x=[0,1,2] puis à nouveau avec x=[0,0.5,1,1.5,2]. (Que je l'ai fait)

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))) 

Répondre

1

Si vous voulez trouver le x avec la plus grande erreur, de les points échantillonnés, vous devrez trouver l'index de la plus grosse erreur dans un tableau error avec la fonction np.argmax:

# 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]