temp_sum=1.0 for i in range(1,len(X)): #x的多项式 temp_sum=temp_sum*(init-X[i-1])
#计算均差 for j in range(i,len(X)): temp[j,i]=(temp[j,i-1]-temp[j-1,i-1])/(X[j]-X[j-i]) sum+=temp_sum*temp[i,i] return sum
#拉格朗日插值 deflagrange_interpolation(X,Y,init): sum=0.0 for i in range(len(X)): temp=Y[i] for j in range(len(X)): if(j!=i): temp=temp*((init-X[j])/(X[i]-X[j])) sum=sum+temp return sum
defmain(): X=[float(i) for i in (input("请输入X的对应值:").split())] Y=[float(i) for i in (input("请输入Y的对应值:").split())] init=float(input("请输入要计算的x:"))
if(choice==1): result=lagrange_interpolation(X,Y,init) for x in X_temp: Y_temp.append(lagrange_interpolation(X,Y,x)) plt.title("lagrange_interpolation") else: result=newton_interpolation(X,Y,init) for x in X_temp: Y_temp.append(newton_interpolation(X,Y,x)) plt.title("newton_interpolation")