definition of Lagrange interpolation method (quoted from Wikipedia)
For a polynomial function, it is known that there is a given K + 1 value point:
( x0,y0), (X1,y1), (x2,y2),??, (Xk,yk)
where XJ corresponds to the position of the argument, and YJ corresponds to the value of the function at that position.
Assuming that any two different x-J are not the same, then the Lagrangian interpolation polynomial obtained by the Lagrange interpolation formula is:
Each of these is a lagrand-day basic polynomial (or interpolation-based function ) whose expression is:
The specific Java code:
1 ImportJava.util.Scanner;2 3 4 Public classLagrange {5 Private Static Double[] Lag (DoubleX[],DoubleY[],Doublex0[]) {6 intm=x.length;7 intn=x0.length;8 Doubley0[]=New Double[n];9 for(intia=0;ia<n;ia++) {Ten DoubleJ=0; One for(intib=0;ib<m;ib++) { A DoubleK=1; - for(intIc=0;ic<m;ic++) { - if(ib!=IC) { theK=k* (X0[ia]-x[ic])/(x[ib]-X[ic]); - } - } -k=k*Y[ib]; +j=j+K; - } +y0[ia]=J; A } at returny0; - } - Public Static voidMain (string[] args) { -System.out.println ("Please enter the number of interpolated points given:"); -Scanner input=NewScanner (system.in); - intm=input.nextint (); inSystem.out.println ("Please enter the number of interpolated points for the demand solution:"); - intn=input.nextint (); to Doublex[]=New Double[m]; + Doubley[]=New Double[m]; - Doublex0[]=New Double[n]; theSystem.out.println ("Enter the given interpolation point in turn:"); * for(inti=0;i<m;i++){ $x[i]=input.nextdouble ();Panax Notoginseng } -System.out.println ("Enter the function value corresponding to the given interpolation point in turn:"); the for(inti=0;i<m;i++){ +y[i]=input.nextdouble (); A } theSystem.out.println ("Enter the interpolation point of the demand solution in turn"); + for(inti=0;i<n;i++){ -x0[i]=input.nextdouble (); $ } $ Doubley0[]=Lag (x, y, x0); -System.out.println ("Using Lagrange interpolation method to solve:"); - for(inti=0;i<n;i++){ theSystem.out.println (y0[i]+ ""); - }Wuyi System.out.println (); the input.close (); - } Wu}Code
The disadvantage of Lagrange interpolation method is that the calculated result is unstable when the interpolation point is more, and can be improved by defining a weight of center of gravity, the improved interpolation method is generally called the centroid Lagrange interpolation method.
Implementation of Lagrange interpolation method in Java