/* ex1-9.java is for developing the divided-defference
* table for Newton Interpolation polynomial.
例題1-9 牛頓的多項式內插法 已知6點座標 求 x=0.2 f(x)= ??
已知6點座標
x f(x)
===============================
0.0 -6.0
0.1 -5.89483
0.3 -5.65014
0.6 -5.17788
1.0 -4.28172
1.1 -3.99583
===============================
求 x=0.2 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
5
0.0 -6.0
0.1 -5.89483
0.3 -5.65014
0.6 -5.17788
1.0 -4.28172
1.1 -3.99583
輸出畫面
$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 -6.00000 1.05170 0.57251 0.21497 0.06305 0.01413 1 0.10000 -5.89483 1.22345 0.70150 0.27802 0.07859 2 0.30000 -5.65014 1.57420 0.95171 0.35661 3 0.60000 -5.17788 2.24040 1.23700 4 1.00000 -4.28172 2.85890 5 1.10000 -3.99583
沒有留言:
張貼留言