First step interpolation, the subject is very simple, the key is the algorithm part, I used the matrix data structure to store each iteration after the new value. The circle of the angle mark may be a little complicated at first, it will be clear when you go through it yourself.
1 #CODING=GBK2 " "3 Created on 2014-8-314 5 @author: Administrator6 " "7 8 defNeville (xt,m,n,x):9 forIinchRange (1, N):Ten forJinchRange (1, N): OneW[i-j][i]= (X-xt[i-j])/(xt[i]-xt[i-J]) Am[i][j]=m[i-1][j-1]+w[i-j][i]* (m[i][j-1]-m[i-1][j-1]) - forIinchrange (n): - forJinchRange (0,i+1): the ifj%n==0: - Print("\ n") - Print('%f'%M[i][j]) - +n = Int (input ('Number of INSERT nodes:')) -x = float (Input ('enter the value of x:')) +m = [[0 forIinchRange (n)] forJinchrange (n)] #创建n *n Matrix Aw = [[0 forIinchRange (n)] forJinchrange (n)] atXT = [0]*N - forIinchrange (n): -M[i][0] = float (Input ('Insert%d y values:'% (i+1))) - forIinchrange (n): -Xt[i] = float (Input ('Insert%d x value:'% (i+1))) -Neville (xt,m,n,x)
The following is a Lagrangian interpolation algorithm, very simple, sharing for reference.
1 #CODING=GBK2 " "3 Created on 2014-8-314 5 @author: Administrator6 " "7 defLagrange (X,XT,YT):8y =09 forIinchRange (3):Tent = 1 One forJinchRange (3): A ifi!=J: -t = t* (X-xt[j])/(xt[i]-Xt[j]) -y = y+t*Yt[i] the Print("The result is:%f"%y) - -XT = [] -YT = [] +x = float (Input ("interpolation x;")) -n = Int (input ("number of nodes;")) + forIinchrange (n): AXt.append (Float (input ("the value of the first%d x"% (i+1)))) at forIinchrange (n): -Yt.append (Float (input ("the value of the first%d x"% (i+1)))) - -Lagrange (X,XT,YT)
On the python implementation of the Lagrange and Neville interpolation algorithms