2019年4月7日 星期日

[JAVA程式語言]例題3-3 不等距的函數f(x)的第一到第三項 微分近似值

/* 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


沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...