ΥڡϤƤʥ֥åޡɲΥڡޤϤƤʥ֥åޡ Υڡlivedoor åפɲΥڡޤlivedoor å

ܼ

Ϥ

2ˤϡ2βθסعǽˤѤФ褤3ˤϥΤθ4ˤϥե顼θȤ¸ߤ롣5ʾβθ¸ߤʤȤǤ˥٥ˤäƾƤ롣

5ʾβϤʤ¸ߤʤǤǽŪ˵뤳ȤǤ륢르ꥺब¸ߤ롣Υ르ꥺΤҤȤĤʬˡǤ롣

ʬˡΥ르ꥺ

ޤ2Ĥxa,b롣ΤȤf(a)f(b)椬ۤʤ褦ˡab֡ȡabδ֤˲¸ߤ롣ͤˤ餫Ǥ롣
ˡȤꤢ2cᡢf(c)0ɤĴ٤롣⤷0Ǥä顢c򤽤ΤΤǤ롣0ǤʤСaޤbcȤäơ⤦ٷ֤ΤȤf(a)f(c)椬Ʊʤacf(b)f(c)椬Ʊʤbc롣
򷫤֤ƤСϰϤ򼡡Ⱦʬ˹ʤǤʤ줬ʬˡȤ̾ͳˡǽŪ˲Ǥ櫓Ǥ롣

ޤȤȡʬˡΥ르ꥺϼΤ褦ˤʤ롣

  • Inputa,bʡabעʡf(a)f(b)椬㤦ס
  • Outputcf(x)=0β
  1
  2
  3
  4
  5
while f(c)==0 do
	c(a+b)/2
	if f(a)f(c)椬ۤʤ then bc
	if f(b)f(c)椬ۤʤ then ac
output c
 

ʬˡϡϢ³ؿǤѤǤʤȤա

ޤưɽˤdoubleη׻ϸȼʤĤޤꡢif( f(x)==0 )Ȥ뤳ȤǡȽ꤬ǤʤŬ͡ʡthresholdˤơͤ꾮ʤä0ȹͤ뤳Ȥˤ롣οˤʤ뤳ȤͤơΤ褦ˤɬפ롣

double threshold = 1E-10;	// threshold = 0.0000000001פƱ
if( -threshold < f(x) || f(x) < threshold)	// 0Ƚ

ˡ֤ۤ0ˤʤä롼פλ뤳Ȥ򤷤Ƥ

double threshold = 1E-10;	// threshold = 0.0000000001פƱ
if(b-a < threshold){
	break;
}

ơϤcϤޤabδ֤ȤȤդߤ

Javaˤ

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 
 
 
 
 
 
-
|
-
|
|
!
|
|
-
|
|
|
|
-
|
|
-
|
|
!
|
-
|
|
!
|
-
|
|
!
|
-
|
|
!
|
-
|
|
!
|
|
|
-
|
-
|
!
|
|
|
-
|
!
|
|
|
|
-
|
-
|
!
|
|
|
|
|
|
|
|
|
!
|
|
|
|
!
!
/*
 * ʬˡβ
 * ϡܤϰa,bab
 * ϡabϰϤβ
 */
 
public class Bisection {
    //ؿFunc
    static double Func(double x){
        double y = (x-1)*(x+2)*(x-3);    //ؿ׻
        return y;
    }
 
    //main᥽å
    public static void main(String[] args) {
        double threshold = 1E-10;
        double a=0,b=1;
        double c=0;
        
        try{
            a=Double.parseDouble(args[0]);
            b=Double.parseDouble(args[1]);
        }catch(NumberFormatException e){
            System.out.println("ϥ顼");
            return;
        }
        
        if(a>b){
            System.out.println("a<bˤʤ褦ͤǤ");
            return;
        }
        
        if(Func(a)*Func(b)>0){
            System.out.println("f(a)f(b)椬ƱǤ");
            return;
        }
        
        if(Func(a)==0){
            System.out.println(a+"ϲǤ");
            return;
        }
        
        if(Func(b)==0){
            System.out.println(b+"ϲǤ");
            return;
        }
        
        //ʬˡDz
        int i=0;    //֤Υ
        while(true){
            //֤ۤ0ˤʤä롼׽λ
            if(b-a < threshold){
                break;
            }
            
            c=(a+b)/2;    //ͤ
            double fc = Func(c);
            if(-threshold <= fc && fc <= threshold){
                break;
            }
            
            //ζ֤ꤹ
            double fa=Func(a);
            double fb=Func(b);
            if(fa*fc < 0){
                b=c;
            }else if(fb*fc < 0){
                a=c;
            }
            i++;
            
            //вɽ
            String si = new java.text.DecimalFormat("00").format(i);
            String sa = new java.text.DecimalFormat("0.00000").format(a);
            String sb = new java.text.DecimalFormat("0.00000").format(b);
            String sc = new java.text.DecimalFormat("0.00000").format(c);
            String sf = new java.text.DecimalFormat("0.00000").format(fc);
            System.out.println(si+" a="+sa+" b="+sb+" c="+sc+" f(c)="+sf);
        }
        
        //᤿ɽ
        String s = new java.text.DecimalFormat("0.00000").format(c);
        System.out.println(i+"ܡ"+s);
    }
}

Υץϡ2ĤΰͿȤϰβ뤳ȤǤ롣ؿf(x)ϴؿFuncǻꤷƤ롣Ǥf(x)=(x-1)(x+2)(x-3)ʤΤǡ-2,1,3Ȥ3Ĥβġ

ʬˡǤϲҤȤĤĤĤʤĤޤꤢ餸3Ĥ뤳ȤΤʤСϰϤλ꤬Ǥʤ㤨Сsin(x)cos(x)̵¤β򤬤󤢤롣Τᡢ̤δؿβ뤿ˤϡ褹Ȥߤͤɬפ롣

ʬˡѤ٤Ƥβ륢르ꥺ

٤ƤβȤäƤ⡢sin(x)ʤɤ̵¤β¸ߤƤޤΤǡ֤Ǥ褦ˤƤζΤ٤ƤβϤ褦ʥ르ꥺͤ롣

  • Inputminʶ֤κǾ͡ˡmaxʶ֤κ͡
  • OutputCʲΥꥹȡ
  1
  2
while (a=min || a<=max) do
	ba+ʶ֤ڤ
	if f(a)f(b)椬ۤʤ then
		Bisection(a,b);

BisectionʬˡΥ르ꥺǤ롣ɽϤ˷̡ʤҤȤĤβˤϤȤ롣ޤ顼å⤤ʤ

Javaˤ

Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 
 
 
 
 
-
|
|
-
|
|
!
|
|
|
-
|
|
|
|
-
|
!
|
-
|
!
|
|
-
|
-
|
!
|
|
|
-
|
!
|
|
|
|
-
|
-
|
!
!
|
|
!
|
|
-
|
|
|
-
|
-
|
|
-
|
|
!
|
|
-
|
|
|
!
!
|
|
|
-
|
|
|
-
|
|
|
|
!
!
!
!
/*
 * ʬˡβץ
 * ϡ֤κǾͤȺ
 * ϡĤä٤Ƥβ
 */
public class Bisection2 {
 
    //ؿFunc
    static double Func(double x){
        double y = (x-1)*(x+2)*(x-3);    //ؿ׻
        return y;
    }
    
    //DoBisection
    //ʬˡǶa,bδ֤ˤ
    static double DoBisection(double a,double b){
        double threshold = 1E-10;
        double c=0;
        
        //aˤʤäƤ
        if(Func(a)==0){
            return a;
        }
        //bˤʤäƤ
        if(Func(b)==0){
            return b;
        }
        
        //ʬˡDz
        while(true){
            //֤ۤ0ˤʤä롼׽λ
            if(b-a < threshold){
                break;
            }
            
            c=(a+b)/2;    //ͤ
            double fc = Func(c);
            if(-threshold <= fc && fc <= threshold){
                break;
            }
            
            //ζ֤ꤹ
            double fa=Func(a);
            double fb=Func(b);
            if(fa*fc < 0){
                b=c;
            }else if(fb*fc < 0){
                a=c;
            }
        }            
        //᤿֤
        return c;
    }
        
    //main᥽å
    public static void main(String[] args) {
        double step=0.1;    //֤ڤ
        double min=-100,max=100;    //֤κǾͤȺ͡ʻꤷʤС줬ǥեͤȤꤵ
        
        if(args.length==2){
            //ޥɥ饤󤫤֤Ϥ롣
            try{
                min=Double.parseDouble(args[0]);
                max=Double.parseDouble(args[1]);
            }catch(NumberFormatException e){
                System.out.println("ϥ顼");
                return;
            }
            
            //Ǿͤ͡ʤ촹롣
            if(min>max){
                double tmp=min;
                min=max;
                max=tmp;
            }
        }
        
        //ʬˡDz
        int ansNo=0;    //ο
        for(double a=min;a<=max;a+=step){
            double b=a+step;
            
            //֤ξüǴؿͤۤʤäƤʤ
            if(Func(a)*Func(b)<0){
                ansNo++;
                double c=DoBisection(a,b);
                String sc=new java.text.DecimalFormat("0.0####").format(c);
                System.out.println(ansNo+":"+sc);
            }
        }
    }
}

ʸ

  • EclipseˤθؽJavaǤϤ륢르ꥺ