牛顿插值java_java实现牛顿插值法

import java.util.Scanner;

public class Newton_interpolation {

/*拷贝向量*/

private static void copy_vector(double from[],double to[]){

int k=from.length;

int k2=to.length;

if(k!=k2){

System.out.println("the two vector's length is not equal!");

System.exit(0);

}

for(int i=0;i

to[i]=from[i];

}

}

/*牛顿插值法*/

private static double[] Newton_inter_method(double[] X,double[] Y,double X0[]){

int m=X.length;

int n=X0.length;

double[] Y0=new double[n];

double[] cp_Y=new double[m];

for(int i1=0;i1

double t=0;

int j=0;

copy_vector(Y, cp_Y);

int kk=j;

/*求各级均差*/

while(kk

kk=kk+1;

for(int i2=kk;i2

cp_Y[i2]=(cp_Y[i2]-cp_Y[kk-1])/(X[i2]-X[kk-1]);

}

}

/*求插值结果*/

double temp=cp_Y[0];

for(int i=1;i<=m-1;i++){

double u=1;

int jj=0;

while(jj

u*=(X0[i1]-X[jj]);

jj++;

}

temp+=cp_Y[i]*u;

}

Y0[i1]=temp;

}

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

X[i]=scan.nextDouble();

}

System.out.println("Input the elements of Y:");//已知插值点的函数值

for(int i=0;i

Y[i]=scan.nextDouble();

}

System.out.println("Input the elements of X0:");//需要求的插值点的横坐标标值

for(int i=0;i

X0[i]=scan.nextDouble();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值