顯示具有 JAVA語言數值分析 標籤的文章。 顯示所有文章
顯示具有 JAVA語言數值分析 標籤的文章。 顯示所有文章

2019年4月8日 星期一

例題4-2 利用梯形積分法 成常態分布f(x)的面積 n=10 a=-3.5 b=-3.0

/* ex4-2.java based on Trapezoidol Rule is
 * used for computing definite integral with
 * domain [a,b] with n even-grids.

 例題4-2 利用梯形積分法 成常態分布f(x)的面積
  f(x) = \frac{1}{\sqrt{2\pi}} \, \exp\left(-\frac{x^2}{2} \right)
  n=10  a=-3.5 b=-3.0
 
*/

import java.util.Scanner;
public class Main {
    double F(double x1) {
        return (1.0/Math.exp(x1*x1/2));
    }

    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        Main fun = new Main();
        int n,i;
        double  a,b,x,tn,h,sum=0.0;

        //n=scanner.nextInt();
        //a=scanner.nextFloat();
        //b=scanner.nextFloat();
        n=10;
        a=-3.5;
        b=-3.0;

        h=(b-a)/n;
        x=a;
        for(i=1;i<=n-1;i++) {
            x=x+h;
            sum=sum+fun.F(Math.abs(x));
        }
        tn=(1.0/Math.sqrt(2*Math.PI))*(h/2.0)*(fun.F(Math.abs(a))+fun.F(Math.abs(b))+2.0*sum);
        System.out.printf("T%d=%.7f\n",n,tn);
    }
}

輸出畫面
T10=0.0011194

[JAVA程式語言]例題4-1 利用梯形積分法 求 f(x)=exp(x) x=0~1 的面積

[JAVA程式語言]例題4-1 利用梯形積分法 求 f(x)=exp(x)  x=0~1  的面積

/* ex4-1.java based on Trapezoidol Rule is
 * used for computing definite integral with
 * domain [a,b] with n even-grids.

例題4-1 利用梯形積分法 求 f(x)=exp(x)  x=0~1  的面積

*/

import java.util.Scanner;
public class Main {
 
    double F(double x1) {
        return Math.exp(x1);
    }
 
    public static void main(String args[]){
        Scanner scanner = new Scanner(System.in);
        Main fun = new Main();
        int n,i;
        double  a,b,x,tn,h, real ,sum=0.0;
     
        //n=scanner.nextInt();
        //a=scanner.nextFloat();
        //b=scanner.nextFloat();
        n=10;
        a=0;
        b=1.0;
        h=(b-a)/n;
        x=a;
        for(i=1;i<=n-1;i++) {
            x=x+h;
            sum=sum+fun.F(Math.abs(x));
        }
        tn=((h/2.0)*(fun.F(Math.abs(a))+fun.F(Math.abs(b))+2.0*sum));
        real=Math.exp(1) -1;
        System.out.printf("T%d=%.6f , real value=%.6f error=%.6f \n",n,tn,real, Math.abs(tn-real));

    }
}


輸出畫面
T10=1.719713 , real value=1.718282 error=0.001432

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


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

[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 ;
        float  xa , sum=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");
        }

    }
}

輸入資料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

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


[JAVA程式語言]例題3-2 依據下表求f'(x)一階微分值=?

 [JAVA程式語言]例題3-2  依據下表求f'(x)一階微分值

 x           f(x)             f'(x)
======================
-0.3     -0.20431      ????
-0.1     -0.08993      ????
 0.1       0.11007      ????
 0.3       0.39569      ????
======================


/* ex3-2.java is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.
 例題3-2 一下表求f'(x)一階微分值

x           f(x)       f'(x)
================================
-0.3     -0.20431      ????
-0.1     -0.08993      ????
0.1       0.11007      ????
0.3       0.39569      ????

x=[-0.3 , -0.1 , 0.1 , 0.3]
f=[-0.20431 ,  -0.08993 , 0.11007 , 0.39569]

(A) 向前差近似 (三點)
fi' =  [ -f(i+2) + 4f(i+1) -3 f(i) ] / 2h ,
O(h) = 1/3 * h*h *  < fi 三次微分 >

(B) 向後差近似(三點)
fi' =  [ 3f(i) - 4f(i-1) + f(i-2) ] / 2h ,
O(h) = 1/3 * h*h * < fi 三次微分 >

(C) 中央近似 (雙點)
fi' = [f(i+1) - f(i-1)] / 2h  ,
O(h) = -1/6 * h *h *  < fi 三次微分 >
中央近似沒有三點

*/

public class Main {

    double FordDiff(int i ){ // 前差微分
        // 前差微分 fi' =  [ -f(i+2) + 4f(i+1) -3 f(i) ] / 2h ,
        double h1,h2,h3,s;
        double x[]={-0.3 , -0.1 , 0.1 , 0.3};
        double f[]={-0.20431 ,  -0.08993 , 0.11007 , 0.39569};
        h1=x[i+1];
        h2=x[i];
        h3=Math.abs(h1-h2);
        s= -f[i+2] + 4*f[i+1] -3*f[i];
        return ( s / (2* h3));
    }

    double BackDiff(int i ){ // 前差微分
        // // 後差微分 fi' =  [ 3f(i) - 4f(i-1) + f(i-2) ] / 2h ,
        double h1,h2,h3,s;
        double x[]={-0.3 , -0.1 , 0.1 , 0.3};
        double f[]={-0.20431 ,  -0.08993 , 0.11007 , 0.39569};
        h1=x[i];
        h2=x[i-1];
        h3=Math.abs(h1-h2);
        s= 3*f[i]-4*f[i-1] +f[i-2];
        return ( s / (2* h3));
    }

    public static void main(String args[]){
        Main fun = new Main();
        double x[]={-0.3 , -0.1 , 0.1 , 0.3};
        double f[]={-0.20431 ,  -0.08993 , 0.11007 , 0.39569};
        int i;
        //===========================
        //前差微分 fi' =  [ -f(i+2) + 4f(i+1) -3 f(i) ] / 2h ,
        for (i=0;i<=1;i++){
            System.out.printf("x= %2.2f , f(x)= %2.6f , FordDiff : %10.6f\n",x[i],f[i],fun.FordDiff(i));
        }
        System.out.printf("\n\n");
        for (i=3;i>1;i--){
            System.out.printf("x= %2.2f , f(x)= %2.6f , BackDiff : %10.6f\n",x[i] ,f[i],fun.BackDiff(i));
        }
    }
}


輸出畫面

x= -0.30 , f(x)= -0.204310 , FordDiff :   0.357850
x= -0.10 , f(x)= -0.089930 , FordDiff :   0.785950


x= 0.30 , f(x)= 0.395690 , BackDiff :   1.642150
x= 0.10 , f(x)= 0.110070 , BackDiff :   1.214050

[JAVA程式語言] 例題3-1 求sin(x)的微分

[JAVA程式語言] 例題3-1 求sin(x)的微分

向前差  f'(x) =  ( f(x+h) - f(x)) / h
向後差  f'(x) =  ( f(x) - f(x-h)) / h
中央差  f'(x) =  0.5 * ( f(x+h) - f(x-h)) / h

/* ex3-1.java is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.
[JAVA程式語言] 例題3-1 求sin(x)的微分
*/

public class Main {

    double fx(double x1) {
        return (Math.sin(x1));
    }

    double FordDiff(double x1, double h1){ // 前差微分
        return (( fx(x1+h1) - fx(x1) ) / h1);
    }

    double BackDiff(double x1, double h1){ // 後差微分
        return (( fx(x1) - fx(x1-h1) ) / h1);
    }

    double MidDiff(double x1, double h1){ // 中差微分
        return 0.5 * ( fx(x1+h1) - fx(x1-h1) ) / h1;
    }

    public static void main(String args[]){
        Main fun = new Main();
     
        double x,h,delta,delta2;
        x=1.0;
        double ans = Math.cos(x); // 答案
        double cal;
        double hx[] = {0.001 , 0.005 ,0.01 , 0.05 , 0.1} ;
        int i=0;
     
        for(i=0;i<=4;i++){
            h=hx[i];
            System.out.printf("The h=%2.3f\n",h);
            System.out.printf("The real value cos(1.0)=%2.5f\n",ans);
     
            cal = fun.FordDiff(x, h);
            delta = cal - ans;
            delta2= (ans-cal)/cal*100;
            System.out.printf("FordDiff : %2.6f, delta = %2.6f , error = %2.4f\n",cal,delta ,delta2);

            cal = fun.BackDiff(x, h);
            delta = cal - ans;
            delta2= (ans-cal)/cal*100;
            System.out.printf("BackDiff : %2.6f, delta = %2.6f , error = %2.4f\n",cal,delta ,delta2);
     
            cal = fun.MidDiff(x, h);
            delta = cal - ans;
            delta2= (ans-cal)/cal*100;
            System.out.printf("MidDiff  : %2.6f, delta = %2.6f , error = %2.4f\n\n\n",cal,delta ,delta2);
        } 
    }
}


輸出畫面
The h=0.001
The real value cos(1.0)=0.54030
FordDiff : 0.539881, delta = -0.000421 , error = 0.0779
BackDiff : 0.540723, delta = 0.000421 , error = -0.0778
MidDiff  : 0.540302, delta = -0.000000 , error = 0.0000


The h=0.005
The real value cos(1.0)=0.54030
FordDiff : 0.538196, delta = -0.002106 , error = 0.3913
BackDiff : 0.542404, delta = 0.002101 , error = -0.3874
MidDiff  : 0.540300, delta = -0.000002 , error = 0.0004


The h=0.010
The real value cos(1.0)=0.54030
FordDiff : 0.536086, delta = -0.004216 , error = 0.7865
BackDiff : 0.544501, delta = 0.004198 , error = -0.7710
MidDiff  : 0.540293, delta = -0.000009 , error = 0.0017


The h=0.050
The real value cos(1.0)=0.54030
FordDiff : 0.519045, delta = -0.021257 , error = 4.0955
BackDiff : 0.561110, delta = 0.020807 , error = -3.7082
MidDiff  : 0.540077, delta = -0.000225 , error = 0.0417


The h=0.100
The real value cos(1.0)=0.54030
FordDiff : 0.497364, delta = -0.042939 , error = 8.6332
BackDiff : 0.581441, delta = 0.041138 , error = -7.0753
MidDiff  : 0.539402, delta = -0.000900 , error = 0.1669

[JAVA程式語言]Program for Fixed Point Iteration Method using Java Programming Language

Program for Fixed Point Iteration Method using Java Programming Language

Algorithm of fixed point Iteration method



/* ex2-9.java is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.
*/

public class Main {

    double f(double x1) {
     return (Math.cos(x1)-3*x1+1);
 }

    double g(double x1) {
     return ( (1+Math.cos(x1))/ 3 );
 }


 public static void main(String args[]){
        Main fun = new Main();
        final int MAX = 20;  /* maximum iterations */
        final double TOL =0.00001;/* maximum iterations */
 
        int i=1;
        int step=1, N=10;
        double  x0, x1, e;
        x0=1.0;
        N=10;
        e=0.00001;
        /* Implementing Fixed Point Iteration */
    System.out.printf("\nStep\tx0\t\t   f(x0)\t\tx1\t\t   f(x1)\n");
     
        do {
    x1 = fun.g(x0);
    System.out.printf("%d\t%f\t%f\t%f\t%f\n",step, x0, fun.f(x0), x1, fun.f(x1));
    step = step + 1;
    if(step>N) {
    System.out.printf("Not Convergent.");
    break;
    }
        x0 = x1;

    }while( Math.abs(fun.f(x1)) > e);

    System.out.printf("\nRoot is %f", x1);
     
    }
}

輸出畫面
Step      x0    f(x0)     x1       f(x1)
1 1.000000   -1.459698 0.513434     0.330761
2 0.513434   0.330761 0.623688     -0.059333
3 0.623688   -0.059333 0.603910     0.011391
4 0.603910   0.011391 0.607707     -0.002162
5 0.607707   -0.002162 0.606986     0.000411
6 0.606986   0.000411 0.607124     -0.000078
7 0.607124   -0.000078 0.607097     0.000015
8 0.607097   0.000015 0.607102     -0.000003

Root is 0.607102

2019年4月6日 星期六

[JAVA程式語言]Fixed-point 定點迴路法 改寫的方程式不同而無法收斂的情況(振盪的情形+-+-....)

[JAVA程式語言]Fixed-point 定點迴路法 改寫的方程式不同而無法收斂的情況(振盪的情形+-+-....)



/* ex2-9.java is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.
*/

public class Main {

    double gx(double x1) {
     return (Math.pow(x1,3)+4*Math.pow(x1,2)-10);
 }
 
    double gx1(double x1) {
     return Math.sqrt((5-0.5*x1*x1*x1));
 }

    double gx2(double x1) {
     return Math.sqrt(10/(4+x1));
 }



 public static void main(String args[]){
        Main fun = new Main();
        final int MAX = 20;  /* maximum iterations */
        final double TOL =0.00001;/* maximum iterations */
 
        int i=1;
        double x0,x;
        x0=1.4;
        System.out.printf("x0=%1.2f , The function valuet=%10.7f\n",x0,fun.gx(x0));
        x0=1.3;
        System.out.printf("x0=%1.2f , The function valuet=%10.7f\n\n\n",x0,fun.gx(x0));
        System.out.printf(" i\t  x0\t       f(x0)\t      x\t      f(x)\n");
        x0=1.6;
        while(i<=MAX) {
            x=fun.gx1(x0);
            System.out.printf("%2d  %10.7f",i-1,x0);
            System.out.printf("\t %f \t %f \t %f\n", fun.gx(x0), x, fun.gx(x));
            if(Math.abs(x-x0) < TOL) {
                System.out.printf("%f \t %f \t %f \t %f\n",x0, fun.gx(x0), x, fun.gx(x));
                System.out.printf("Count=%2d , The Root=%10.7f  x-x0=%10.7f\n",i+1,x,Math.abs(x-x0));
                break;
            }
            else
            {
                i++;
                x0=x;
            }
        }
        if(i>MAX){
            System.out.printf("Fixed-point Method faileds!!!\n");
        } 
     
        System.out.printf("\n\n\n");
        System.out.printf(" i\t  x0\t       f(x0)\t      x\t      f(x)\n");
     
        i=1;
        x0=1.5;
        while(i<=MAX) {
            x=fun.gx2(x0);
            System.out.printf("%2d  %10.7f",i-1,x0);
            System.out.printf("\t %f \t %f \t %f\n", fun.gx(x0), x, fun.gx(x));
            if(Math.abs(x-x0) < TOL) {
                System.out.printf("Count=%2d , The Root=%10.7f  x-x0=%10.7f\n",i+1,x,Math.abs(x-x0));
                break;
         }
            else
            {
             i++;
             x0=x;
            }
        }
        if(i>MAX){
            System.out.printf("Fixed-point faileds!!!\n");
        } 
     
     
     
 }
}

 輸出畫面
x0=1.40 , The function valuet= 0.5840000
x0=1.30 , The function valuet=-1.0430000


 i   x0            f(x0)                x             f(x)
 0   1.6000000 4.336000 1.718139 6.879945
 1   1.7181385 6.879945 1.569722 3.723949
 2   1.5697221 3.723949 1.751023 7.633100
 3   1.7510228 7.633100 1.521713 2.786142
 4   1.5217133 2.786142 1.799486 8.779617
 5   1.7994865 8.779617 1.444470 1.359860
 6   1.4444705 1.359860 1.868973 10.500674
 7   1.8689729 10.500674 1.317491 -0.769990
 8   1.3174911 -0.769990 1.963813 12.999807
 9   1.9638128 12.999807 1.101462 -3.810813
10   1.1014619 -3.810813 2.081308 16.343273
11   2.0813080 16.343273 0.701463 -7.686645
12   0.7014627 -7.686645 2.197140 19.916212
13   2.1971396 19.916212 NaN NaN
14         NaN NaN NaN NaN
15         NaN NaN NaN NaN
16         NaN NaN NaN NaN
17         NaN NaN NaN NaN
18         NaN NaN NaN NaN
19         NaN NaN NaN NaN
Fixed-point Method faileds!!!



 i   x0        f(x0)       x       f(x)
 0   1.5000000 2.375000 1.348400 -0.275637
 1   1.3483997 -0.275637 1.367376 0.035481
 2   1.3673764 0.035481 1.364957 -0.004508
 3   1.3649570 -0.004508 1.365265 0.000574
 4   1.3652647 0.000574 1.365226 -0.000073
 5   1.3652256 -0.000073 1.365231 0.000009

Count= 7 , The Root= 1.3652306  x-x0= 0.0000050

EXCEL計算結果
1.5 3.3125 1.820027472
1.820027 1.985579499 1.409105922
1.409106 3.601054085 1.897644352
1.897644 1.583240028 1.258268663
1.258269 4.003929343 2.000982095
2.000982 0.994104538 0.997047911
0.997048 4.504415073 2.122360731
2.122361 0.220003166 0.46904495
0.469045 4.948404313 2.224500913
2.224501 -0.503864957 #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!
#NUM! #NUM! #NUM!

[JAVA程式語言]例題2-8 定點回路法 求非線性方程式 f(x)=exp(x) - 3x^2 = 0 找出f(x)=0的根=?

[JAVA程式語言]例題2-8 定點回路法 求非線性方程式  f(x)=exp(x) - 3x^2 = 0 找出f(x)=0的根=?

/* ex2-9.java is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.

例題2-8 定點回路法 求非線性方程式 f(x)=exp(x) - 3x^2 = 0 找出f(x)=0的根=?

*/

public class Main {

    double gx1(double x1) {
    return ( Math.pow( (Math.exp(x1)/3) , 0.5));
}

    double gx2(double x1) {
    return (-1* Math.pow( (Math.exp(x1)/3) , 0.5));
}



public static void main(String args[]){
    Main fun = new Main();
    final int MAX = 50;  /* maximum iterations */
    final double TOL =0.001;/* maximum iterations */
 
        int i=1;
        double x0,x;
        x0=0.57735;
        while(i<=MAX) {
            x=fun.gx1(x0);
            System.out.printf("%2d  %10.7f\n",i-1,x0);
            if(Math.abs(x-x0) < TOL) {
                System.out.printf("Count=%2d , The Root=%10.7f  x-x0=%10.7f\n",i+1,x,Math.abs(x-x0));
                break;
        }
            else
            {
            i++;
            x0=x;
            }
        }
        if(i>MAX){
            System.out.printf("Fixed-point Method faileds!!!\n");
        } 
     
        System.out.printf("\n\n\n");
     
        i=1;
        x0=0.0;
        while(i<=MAX) {
            x=fun.gx2(x0);
            System.out.printf("%2d  %10.7f\n",i-1,x0);
            if(Math.abs(x-x0) < TOL) {
                System.out.printf("Count=%2d , The Root=%10.7f  x-x0=%10.7f\n",i+1,x,Math.abs(x-x0));
                break;
        }
            else
            {
            i++;
            x0=x;
            }
        }
        if(i>MAX){
            System.out.printf("Fixed-point Method faileds!!!\n");
        } 
     
     
     
}
}




 輸出畫面
 0   0.5773500
 1   0.7705651
 2   0.8487220
 3   0.8825453
 4   0.8975975
 5   0.9043784
 6   0.9074499
 7   0.9088446
Count= 9 , The Root= 0.9094786  x-x0= 0.0006340



 0   0.0000000
 1  -0.5773503
 2  -0.4325829
 3  -0.4650559
 4  -0.4575660
 5  -0.4592828
Count= 7 , The Root=-0.4588887  x-x0= 0.0003941

[JAVA程式語言]例題2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0 找出f(x)=0的根=?

[JAVA程式語言]例題2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0 找出f(x)=0的根=?
/* ex2-9.java is used for solving nonlinear equation
 * based on Fixed-Point Algorithm g(x)=x with initial
 * approximation P0.

例題2-9 定點回路法 求非線性方程式 f(x)=1/5^x - x = 0 找出f(x)=0的根=?

*/

public class Main {

    double gx(double x1) {
    return (1/Math.pow(5,x1));
}

public static void main(String args[]){
    Main fun = new Main();
    final int MAX = 50;  /* maximum iterations */
    final double TOL =0.0001;/* maximum iterations */
 
        int i=1;
        double x0,x;
        x0=0.45;
        while(i<=MAX) {
            x=fun.gx(x0);
            System.out.printf("%2d  %10.7f\n",i-1,x0);
            if(Math.abs(x-x0) < TOL) {
                System.out.printf("The Root=%10.7f  x-x0=%10.7f\n",x,Math.abs(x-x0));
                break;
        }
            else
            {
            i++;
            x0=x;
            }
        }
        if(i>MAX){
            System.out.printf("Fixed-point Method faileds!!!\n");
        } 
}
}

輸出畫面
 0   0.4500000
 1   0.4846894
 2   0.4583705
 3   0.4782035
 4   0.4631803
 5   0.4745160
 6   0.4659374
 7   0.4724151
 8   0.4675155
 9   0.4712167
10   0.4684181
11   0.4705327
12   0.4689340
13   0.4701421
14   0.4692289
15   0.4699191
16   0.4693974
17   0.4697917
18   0.4694936
19   0.4697189
20   0.4695486
21   0.4696773
The Root= 0.4695801  x-x0= 0.0000973


[JAVA程式語言]例題2-6 已知方程式 e^x + x^-2 + 2 cosx -6 利用正割法 找出f(x)=0的根=? err=0.001

[JAVA程式語言]例題2-6 已知方程式 e^x + x^-2 + 2 cosx -6 利用正割法 找出f(x)=0的根=? err=0.001

/* ex2-6.java Secant Method is similar to Newton-Raphson
 *  Method used for find solutions to f(x)=0 given
 *  initial approximations x0 and x1.

例題2-6 已知方程式 e^x + x^-2 + 2 cosx -6 利用正割法 找出f(x)=0的根=?

*/

public class Main {

    double fx(double x1) {
    return (Math.exp(x1)+1/Math.pow(2,x1)+2*Math.cos(x1)-6);
}

public static void main(String args[]){
    Main fun = new Main();
    final int MAX = 50;  /* maximum iterations */
    final double TOL = 0.001;/* maximum iterations */
        int i=2;
        double x0,x1,x,q0,q1;
        // f(1.8) * f(2.0) < 0 有根位於1.8 ~ 2.0 之間
        x0=1.8;
        x1=2.0;
        q0=fun.fx(x0);
        q1=fun.fx(x1);
        System.out.printf("i       xi           f(x)\n");
        System.out.printf("%-2d   %10.6f   %10.6f\n",0,x0,q0);
        System.out.printf("%-2d   %10.6f   %10.6f\n",1,x1,q1);
        while(i<=MAX) {
            x=x1-q1*(x1-x0)/(q1-q0);
            System.out.printf("%-2d   %10.6f   %10.6f\n",i,x,fun.fx(x));
            if(Math.abs(x-x1) < TOL) {
                System.out.printf("The Root=%10.6f f(%10.6f)=%10.6f\n",x,x,fun.fx(x));
                break;
        }
            else
            {
            i++;
            x0=x1;
            q0=q1;
            x1=x;
            q1=fun.fx(x);
            }
        }
        if(i>MAX){
            System.out.printf("Secant Method faileds!!!\n");
        } 
}
}


輸出畫面
i       xi           f(x)
0      1.800000    -0.117582
1      2.000000     0.806762
2      1.825441    -0.016116
3      1.828860    -0.002147
4      1.829385     0.000007
The Root=  1.829385 f(  1.829385)=  0.000007

MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構

MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構 這張圖片展示了一個 結合 MQTT 協定與 Modbus 通訊的遠端監控與控制系統架構 (主要透過 Node-RED 進行資料整合)。 系統包含三個核心部分,其運作功能說明如下: 1. ESP32 終端設備(硬體控制層...