/* ex1-12.java To generate the coeffficients for
* Hermite Interpolating Polynomial H on the distinct numbers
* x0,x1,x2,...with f0,f1,f2,... and f'0,f'1,f'2...
* based on the Newton backward divided-difference Algorithm
* and output the divided_difference table for Hn(x).
[JAVA程式語言]例題1-12 Hermite內差法 已知3點座標
例題1-12 已知3點座標如下
x f(x) f'(x)
=====================
0.0 1.0000 1.0000
1.0 2.7183 2.7183
2.0 7.3891 7.3891
==================
求 Hermite差除表
*/
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
float[] x ,z ,f ,ff ;
float[][] q ;
int i, j , k, n ;
x = new float[30]; // 利用new指令產生物件
z = new float[30]; // 利用new指令產生物件
f = new float[30]; // 利用new指令產生物件
ff = new float[30]; // 利用new指令產生物件
q = new float[30][30]; // 利用new指令產生物件
n=scanner.nextInt();
for(i=0;i<=n;i++) {
x[i]=scanner.nextFloat();
f[i]=scanner.nextFloat();
ff[i]=scanner.nextFloat();
System.out.printf("x[%1d]=%3.4f f[%1d]=%3.4f ff[%1d]=%3.4f \n",i,x[i],i,f[i],i,ff[i]);
}
System.out.printf("\n\ni z(i) f(i) f(i-1,i) f(i-2,i-1,i)...\n");
for(i=0;i<=n;i++){
z[2*i]=x[i];
z[2*i+1]=x[i];
q[2*i][0]=f[i];
q[2*i+1][0]=f[i];
q[2*i+1][1]=ff[i];
if(i != 0){
q[2*i][1]=(q[2*i][0]-q[2*i-1][0])/(z[2*i]-z[2*i-1]);
}
if(i==0){
System.out.printf("%d %3.1f %7.4f\n",i,z[i],q[i][0]);
}
if(i==1) {
System.out.printf("%d %3.1f %7.4f %7.4f\n",i,z[i],q[i][0],q[i][1]);
}
}
//-------------------------------------
for(i=2;i<=2*n+1;i++) {
System.out.printf("%d %3.1f %7.4f %7.4f",i,z[i],q[i][0],q[i][1]);
for(j=2;j<=i;j++) {
q[i][j]=(q[i][j-1]-q[i-1][j-1])/(z[i]-z[i-j]);
System.out.printf(" %7.4f",q[i][j]);
}
System.out.printf("\n");
}
}
}
STDIN:
2
0.0 1.0000 1.0000
1.0 2.7183 2.7183
2.0 7.3891 7.3891
輸出畫面
$javac Main.java $java -Xmx128M -Xms16M Main x[0]=0.0000 f[0]=1.0000 ff[0]=1.0000 x[1]=1.0000 f[1]=2.7183 ff[1]=2.7183 x[2]=2.0000 f[2]=7.3891 ff[2]=7.3891 i z(i) f(i) f(i-1,i) f(i-2,i-1,i)... 0 0.0 1.0000 1 0.0 1.0000 1.0000 2 1.0 2.7183 1.7183 0.7183 3 1.0 2.7183 2.7183 1.0000 0.2817 4 2.0 7.3891 4.6708 1.9525 0.4763 0.0973 5 2.0 7.3891 7.3891 2.7183 0.7658 0.1448 0.0237
沒有留言:
張貼留言