/* ex1-13.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-13 Hermite內差法 已知3點座標
例題1-13 已知4點座標如下
x f(x) f'(x)
=====================
0 50.0 50.0
2 216.67 94.44
4 410.00 98.00
==================
求x=1.0 Hn(x)= ??
*/
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.2f f[%1d]=%3.2f ff[%1d]=%3.2f \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 50.0 50.0
2.0 216.67 94.44
4.0 410.00 98.00
輸出畫面
$javac Main.java $java -Xmx128M -Xms16M Main x[0]=0.00 f[0]=50.00 ff[0]=50.00 x[1]=2.00 f[1]=216.67 ff[1]=94.44 x[2]=4.00 f[2]=410.00 ff[2]=98.00 i z(i) f(i) f(i-1,i) f(i-2,i-1,i)... 0 0.0 50.0000 1 0.0 50.0000 50.0000 2 2.0 216.6700 83.3350 16.6675 3 2.0 216.6700 94.4400 5.5525 -5.5575 4 4.0 410.0000 96.6650 1.1125 -1.1100 1.1119 5 4.0 410.0000 98.0000 0.6675 -0.2225 0.2219 -0.2225
沒有留言:
張貼留言