/* ex3-2.java is used for solving nonlinear equation
* based on Fixed-Point Algorithm g(x)=x with initial
* approximation P0.
例題3-3 不等距的函數f(x)的第一到第三項 微分近似值
不等距的函數f(x)的微分近似值只能
先使用牛頓向前的內插多項式
P'n(x)= f0,1+ f0,1,2 [(x-x0)+(x-x1)] + f0,1,2[(x-x0)(x-x1)+(x-x1)(x-x2)+(x-x2)(x-x0)]+...........
=====================================
x f(x) 第一項P'n(x) 前二項P'n(x) 前三項P'n(x)
0.5 0.4794
0.6 0.5646
0.8 0.7174
1.05 0.8674
=====================================
*/
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
float[] x ;
float[][] y ;
double[] xa1={0.5 , 0.6 ,0.8 ,1.05};
double xa, sum=0 ,sum1=0 ;
int i, j , n ;
x = new float[10]; // 利用new指令產生物件
y = new float[10][10]; // 利用new指令產生物件
n=scanner.nextInt();
//no. of items printf("Enter n : ");
for(i=0;i<=n;i++){
x[i]=scanner.nextFloat();
y[i][0]=scanner.nextFloat();
}
//forward difference table
for(j=1;j<=n;j++){
for(i=0;i<=(n-j);i++){
y[i][j]= (y[i+1][j-1]-y[i][j-1])/(x[i+j]-x[i]);
}
}
System.out.printf("\n ***********Forward Difference Table ***********\n");
System.out.printf(" ================================================\n");
System.out.printf(" i\t x(i)\t f(i)\t f(i,i+1) f(i,i+1.i+2), ......\n");
//display Forward Difference Table
for(i=0;i<=n;i++){
System.out.printf("%2d\t %.2f",i,x[i]);
for(j=0;j<=(n-i);j++){
System.out.printf("\t %.5f",y[i][j]);
}
System.out.printf("\n");
}
System.out.printf("\n\nP'n(x)= 0.852 + -0.2933 * [(x-0.6) + (x- 0.5)]");
System.out.printf("+ -0.1293 *[(x-0.5)(x- 0.6) + (x-0.6)(x- 0.8)+(x-0.8)(x-0.6)] +..........\n\n");
for(i=0;i<=n;i++){
xa=xa1[i];
//calculate the P(xa) =??
sum1=y[0][1];
System.out.printf("xa=%.2f\n",xa);
System.out.printf("real value cos(%.2f)=%.5f\n",xa,Math.cos(xa));
System.out.printf("1). P'(%.2f)=%.5f , cos(%.2f)==%.5f , error=%.5f \n",xa ,sum1, xa,Math.cos(xa) ,Math.abs(sum1- Math.cos(xa)));
sum=y[0][2]*( (xa-x[0]) + (xa-x[1]));
sum1=sum1+sum;
System.out.printf("2). P'(%.2f)=%.5f , cos(%.2f)==%.5f , error=%.5f \n",xa ,sum1, xa,Math.cos(xa) ,Math.abs(sum1- Math.cos(xa)));
sum=y[0][3]*( (xa-x[0])*(xa-x[1])+(xa-x[1])*(xa-x[2])+(xa-x[2])*(xa-x[0]) );
sum1=sum1+sum;
System.out.printf("3). P'(%.2f)=%.5f , cos(%.2f)==%.5f , error=%.5f \n\n\n",xa ,sum1, xa,Math.cos(xa) ,Math.abs(sum1- Math.cos(xa)));
}
}
}
/*
P'n(x)= 0.852 + -0.2933 * [(x-0.6) + (x- 0.5) ]
+ -0.1293 *[(x-0.5)(x- 0.6) + (x-0.6)(x- 0.8)+(x-0.8)(x-0.6)] +..........
x=0.5
第一項P'n(x)
P'n(0.5)=0.852
前二項P'n(x)
P'n(0.5)=0.852 + -0.2933 * [ (0.5-0.6) + (0.5-0.5) = 0.8813
前三項P'n(x)
P'n(0.5)= 0.852 + -0.2933 * [(0.5-0.6) + (0.5- 0.5) ] + -0.1293 *[ (0.5-0.8) + ( 0.5 - 0.6) + ( 0.5 - 0.5) = 0.87745
x=0.6
.第一項P'n(x)
P'n(0.6)=0.852
前二項P'n(x)
P'n(0.6)=0.852 + -0.2933 * [ (0.6-0.6) + (0.6-0.5) = 0.82267
前三項P'n(x)
P'n(0.6)= 0.852 + -0.2933 * [(0.6-0.6) + (0.6 - 0.5) ] + -0.1293 *[ (0.6-0.8) + ( 0.6 - 0.6) + ( 0.6 - 0.5) = 0.82526
x=0.8
.第一項P'n(x)
P'n(0.8)=0.852
前二項P'n(x)
P'n(0.8)=0.852 + -0.2933 * [ (0.8-0.6) + (0.8-0.5) = 0.70534
前三項P'n(x)
P'n(0.8)= 0.852 + -0.2933 * [(0.8-0.6) + (0.8 - 0.5) ] + -0.1293 *[ (0.8-0.8) + ( 0.8 - 0.6) + ( 0.8 - 0.5) = 0.69758
*/
STDIN輸入資料
3
0.5 0.4794
0.6 0.5646
0.8 0.7174
1.05 0.8674
輸出畫面
***********Forward Difference Table ***********
================================================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......
0 0.50 0.47940 0.85200 -0.29333 -0.12930
1 0.60 0.56460 0.76400 -0.36444
2 0.80 0.71740 0.60000
3 1.05 0.86740
xa=0.50
real value cos(0.50)=0.87758
1). P'(0.50)=0.85200 , cos(0.50)==0.87758 , error=0.02558
2). P'(0.50)=0.88133 , cos(0.50)==0.87758 , error=0.00375
3). P'(0.50)=0.87745 , cos(0.50)==0.87758 , error=0.00013
xa=0.60
real value cos(0.60)=0.82534
1). P'(0.60)=0.85200 , cos(0.60)==0.82534 , error=0.02666
2). P'(0.60)=0.82267 , cos(0.60)==0.82534 , error=0.00267
3). P'(0.60)=0.82525 , cos(0.60)==0.82534 , error=0.00008
xa=0.80
real value cos(0.80)=0.69671
1). P'(0.80)=0.85200 , cos(0.80)==0.69671 , error=0.15529
2). P'(0.80)=0.70533 , cos(0.80)==0.69671 , error=0.00863
3). P'(0.80)=0.69758 , cos(0.80)==0.69671 , error=0.00087
xa=1.05
real value cos(1.05)=0.49757
1). P'(1.05)=0.85200 , cos(1.05)==0.49757 , error=0.35443
2). P'(1.05)=0.55867 , cos(1.05)==0.49757 , error=0.06110
3). P'(1.05)=0.49434 , cos(1.05)==0.49757 , error=0.00323
訂閱:
張貼留言 (Atom)
Messaging API作為替代方案
LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
• 認 識 PreFix、InFix、PostFix PreFix(前序式):* + 1 2 + 3 4 InFix(中序式): (1+2)*(3+4) PostFix(後序式):1 2 + 3 4 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言