[java]代码库
import java.util.Scanner;
/*拉格朗日插值*/
public class Lagrange_interpolation {
/*拉格朗日插值法*/
private static double[] Lag_method(double X[],double Y[],double X0[]){
int m=X.length;
int n=X0.length;
double Y0[]=new double[n];
for(int i1=0;i1<n;i1++){//遍历X0
double t=0;
for(int i2=0;i2<m;i2++){//遍历Y
double u=1;
for(int i3=0;i3<m;i3++){//遍历X
if(i2!=i3){
u=u*(X0[i1]-X[i3])/(X[i2]-X[i3]);
}
}
u=u*Y[i2];
t=t+u;
}
Y0[i1]=t;
}
return Y0;
}
public static void main(String[] args) {
/*输入插值点横纵坐标*/
System.out.println("Input number of interpolation point:");
Scanner scan=new Scanner(System.in);
int m=scan.nextInt();
System.out.println("Input number of test point:");
int n=scan.nextInt();
double X[]=new double[m];
double Y[]=new double[m];
double X0[]=new double[n];
System.out.println("Input the elements of X:");//已知插值点
for(int i=0;i<m;i++){
X[i]=scan.nextDouble();
}
System.out.println("Input the elements of Y:");//已知插值点的函数值
for(int i=0;i<m;i++){
Y[i]=scan.nextDouble();
}
System.out.println("Input the elements of X0:");//需要求的插值点的横坐标标值
for(int i=0;i<n;i++){
X0[i]=scan.nextDouble();
}
double Y0[]=Lag_method(X, Y, X0);//使用拉格朗日插值法求解得到需求插值点的纵坐标值
System.out.println("拉格朗日插值法求解得:");
for(int i=0;i<n;i++){
System.out.println(Y0[i]+" ");
}
System.out.println();
}
}
//源代码片段来自云代码http://yuncode.net