/* ex1-9.java is for developing the divided-defference
* table for Newton Interpolation polynomial.
例題1-7 牛頓的多項式內插法 已知3點座標 求 x=0.5 f(x)= ??
已知3點座標
x f(x)
================
0 0
1 -3
2 0
求 x=0.5 f(x)= ??
*/
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
float[] x ;
float[][] f ;
float l ,ff;
int i, j , n ;
x = new float[10]; // 利用new指令產生物件
f = new float[10][10]; // 利用new指令產生物件
n=scanner.nextInt();
for(i=0;i<=n;i++) {
x[i]=scanner.nextFloat();
f[i][0]=scanner.nextFloat();
}
System.out.printf("\n Divided Difference Table:\n");
System.out.printf(" =========================\n");
for(j=1;j<=n;j++) {
for(i=0;i<=n-j;i++) {
f[i][j]=(f[i+1][j-1]-f[i][j-1])/(x[i+j]-x[i]);
}
}
System.out.printf("i x(i) f(i) f(i,i+1) f(i,i+1.i+2),......\n");
for(i=0;i<=n;i++) {
System.out.printf("%d %8.5f ",i,x[i]);
for(j=0;j<=n-i;j++) {
System.out.printf("%8.5f ",f[i][j]);
}
System.out.println();
}
}
}
STDIN
2
0.0 0.0
1.0 -3.0
2.0 0.0
輸出畫面
$javac Main.java $java -Xmx128M -Xms16M Main Divided Difference Table: ========================= i x(i) f(i) f(i,i+1) f(i,i+1.i+2),...... 0 0.00000 0.00000 -3.00000 3.00000 1 1.00000 -3.00000 3.00000 2 2.00000 0.00000
沒有留言:
張貼留言