home > さまざまな言語で数値計算 > 非線形方程式 >

さまざまな言語で数値計算

Only Do What Only You Can Do

二分法

非線形方程式の解法(二分法)を利用して2の平方根を求める .

1. まず, 条件 $ a < b, f(a) < 0, f(b) > 0 $ を満たす点 $ a, b $ を考えると,
関数 $ f(x) $ の解は, 区間 $ (a,b) $ の中に存在する.

2. 次に, 区間 $ (a,b) $ の中点 $ c = (a + b) / 2 $ を考えると,
$ f(c) < 0 $ であれば, 解は, 区間 $ (c,b) $ の中に存在し,
同様に, $ f(c) > 0 $ であれば, 区間 $ (a,c) $ の中に存在する.

3. この作業を繰り返して, 区間を絞り込んで行くことで解を求める.

※ ちなみに, 区間 $ (a,b) $ とは, 両端を含まない数の集合で, 開区間という.
$ (a,b) = \{x | a < x < b \} $
また, 両端を含む数の集合は, 閉区間という.
$ [a,b] = \{x | a \leq x \leq b \} $

VBScript

Option Explicit

Dim a: a = 1.0
Dim b: b = 2.0
WScript.StdOut.Write Right(Space(12) & FormatNumber(bisection(a, b), 10, -1, 0, 0), 12) & vbNewLine

Private Function bisection(ByVal a, ByVal b)
    Dim c
    Do While(True)
        '区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2
        WScript.StdOut.Write Right(Space(12) & FormatNumber(c,          10, -1, 0, 0), 12) & vbTab
        WScript.StdOut.Write Right(Space(12) & FormatNumber(c - Sqr(2), 10, -1, 0, 0), 12) & vbNewLine

        Dim fc: fc = f(c)
        If Abs(fc) < 0.0000000001 Then Exit Do

        If fc < 0 Then
            'f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c
        Else
            'f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c
        End If
    Loop

    bisection = c
End Function

Private Function f(ByVal x)
    f = x * x - 2.0
End Function
Z:\>cscript //nologo Z:\0901.vbs
1.5000000000    0.0857864376
1.2500000000    0.1642135624
1.3750000000    0.0392135624
1.4375000000    0.0232864376
1.4062500000    0.0079635624
1.4218750000    0.0076614376
1.4140625000    0.0001510624
1.4179687500    0.0037551876
1.4160156250    0.0018020626
1.4150390625    0.0008255001
1.4145507813    0.0003372189
1.4143066406    0.0000930783
1.4141845703    0.0000289921
1.4142456055    0.0000320431
1.4142150879    0.0000015255
1.4141998291    0.0000137333
1.4142074585    0.0000061039
1.4142112732    0.0000022892
1.4142131805    0.0000003818
1.4142141342    0.0000005718
1.4142136574    0.0000000950
1.4142134190    0.0000001434
1.4142135382    0.0000000242
1.4142135978    0.0000000354
1.4142135680    0.0000000056
1.4142135531    0.0000000093
1.4142135605    0.0000000019
1.4142135642    0.0000000019
1.4142135624    0.0000000000
1.4142135624

JScript

var a = 1
var b = 2
WScript.StdOut.Write(("            " + bisection(a, b).toFixed(10) ).slice(-12) + "\n")

function bisection(a, b) {
    var c
    while (true) {
        // 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2
        WScript.StdOut.Write(("            " + c.toFixed(10)                  ).slice(-12) + "\t")
        WScript.StdOut.Write(("            " + (c - Math.sqrt(2)).toFixed(10) ).slice(-12) + "\n")

        var fc = f(c)
        if (Math.abs(fc) < 0.0000000001) break

        if (fc < 0){
            // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c
        } else {
            // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c
        }
    }
    return c
}

function f(x) {
    return x * x - 2
}
Z:\>cscript //nologo Z:\0901.js
1.5000000000    0.0857864376
1.2500000000    0.1642135624
1.3750000000    0.0392135624
1.4375000000    0.0232864376
1.4062500000    0.0079635624
1.4218750000    0.0076614376
1.4140625000    0.0001510624
1.4179687500    0.0037551876
1.4160156250    0.0018020626
1.4150390625    0.0008255001
1.4145507813    0.0003372189
1.4143066406    0.0000930783
1.4141845703    0.0000289921
1.4142456055    0.0000320431
1.4142150879    0.0000015255
1.4141998291    0.0000137333
1.4142074585    0.0000061039
1.4142112732    0.0000022892
1.4142131805    0.0000003818
1.4142141342    0.0000005718
1.4142136574    0.0000000950
1.4142134190    0.0000001434
1.4142135382    0.0000000242
1.4142135978    0.0000000354
1.4142135680    0.0000000056
1.4142135531    0.0000000093
1.4142135605    0.0000000019
1.4142135642    0.0000000019
1.4142135624    0.0000000000
1.4142135624

PowerShell

function bisection($a, $b)
{
    while ($true)
    {
        # 区間 (a, b) の中点 c = (a + b) / 2
        $c = ($a + $b) / 2
        Write-Host ([String]::Format("{0,12:F10}`t{1,13:F10}", $c, $c - [Math]::Sqrt(2)))

        $fc = f($c)
        if ([Math]::Abs($fc) -lt 0.0000000001)
        {
            break
        }

        if ($fc -lt 0)
        {
            # f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            $a = $c
        }
        else
        {
            # f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            $b = $c
        }
    }
    return $c
}

function f($x)
{
    return $x * $x - 2
}

$a = 1
$b = 2
Write-Host ([String]::Format("{0,12:F10}", (bisection $a $b)))
Z:\>powershell -file Z:\0901.ps1
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Perl

my $a = 1;
my $b = 2;
printf("%12.10f\n", bisection($a, $b));

sub bisection
{
    my ($a, $b) = @_;
    my $c;
    while (1)
    {
        # 区間 (a, b) の中点 c = (a + b) / 2
        $c = ($a + $b) / 2;
        printf("%12.10f\t%13.10f\n", $c, $c - sqrt(2));

        my $fc = f($c);
        if (abs($fc) < 0.0000000001)
        {
            last;
        }

        if ($fc < 0)
        {
            # f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            $a = $c;
        }
        else
        {
            # f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            $b = $c;
        }
    }
    $c;
}

sub f
{
    my ($x) = @_;
    $x * $x - 2;
}
Z:\>perl Z:\0901.pl
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

PHP

<?php
$a = 1;
$b = 2;
printf("%12.10f\n", bisection($a, $b));

function bisection($a, $b)
{
    while (TRUE)
    {
        # 区間 (a, b) の中点 c = (a + b) / 2
        $c = ($a + $b) / 2;
        printf("%12.10f\t%13.10f\n", $c, $c - sqrt(2));

        $fc = f($c);
        if (abs($fc) < 0.0000000001) break;

        if ($fc < 0)
        {
            # f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            $a = $c;
        }
        else
        {
            # f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            $b = $c;
        }
    }
    return $c;
}

function f($x)
{
    return $x * $x - 2;
}
?>
Z:\>php Z:\0901.php
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507812     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Python

# coding: Shift_JIS

import math

def f(x):
    return x * x - 2.0

def bisection(a, b):
    while True:
        # 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2
        print "%12.10f\t%13.10f" % (c, c - math.sqrt(2))

        fc = f(c)
        if (abs(fc) < 0.0000000001):
            break

        if (fc < 0):
            # f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c
        else:
            # f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c
    return c

a = 1.0
b = 2.0
print "%12.10f" % bisection(a, b)
Z:\>python Z:\0901.py
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507812     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Ruby

include Math

def f(x)
    x * x - 2.0
end

def bisection(a, b)
    while true
        # 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2
        printf("%12.10f\t%13.10f\n", c, c - sqrt(2))

        fc = f(c)
        if fc.abs < 0.0000000001
            break
        end

        if fc < 0
            # f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c
        else
            # f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c
        end
    end

    c
end

a = 1.0
b = 2.0
printf("%12.10f\n", bisection(a, b))
Z:\>ruby Z:\0901.rb
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507812     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Groovy

a = 1
b = 2
printf ("%12.10f\n" , bisection(a, b))

def bisection(a, b) {
    while (true) {
        // 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2
        printf("%12.10f\t%13.10f\n", c, c - Math.sqrt(2))

        fc = f(c)
        if (Math.abs(fc) < 0.0000000001) break

        if (fc < 0) {
            // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c
        } else {
            // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c
        }
    }
    c
}

def f(x) {
    x * x - 2
}
Z:\>groovy Groovy0901.groovy
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Pascal

program Pas0901(arg);
{$MODE delphi}

uses
    SysUtils, Math;

function f(x:Double):Double;
begin
    result := x * x - 2.0;
end;

function bisection(a:Double; b:Double):Double;
var
    c:  Double;
    fc: Double;
begin
    while true do
    begin
        { 区間 (a, b) の中点 c = (a + b) / 2 }
        c := (a + b) / 2;
        writeln(format('%12.10f'#9'%13.10f', [c, c - Sqrt(2)]));

        fc := f(c);
        if Abs(fc) < 0.0000000001 then break;

        if fc < 0 then
        begin
            { f(c) < 0 であれば, 解は区間 (c, b) の中に存在 }
            a := c;
        end
        else
        begin
            { f(c) > 0 であれば, 解は区間 (a, c) の中に存在 }
            b := c;
        end;
    end;

    result := c;
end;

var
    a:  Double = 1.0;
    b:  Double = 2.0;
begin
    writeln(format('%12.10f', [bisection(a, b)]));
end.
Z:\>fpc -v0 -l- Pas0901.pp

Z:\>Pas0901
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Ada

with TEXT_IO, Ada.Long_Float_Text_IO, Ada.Numerics.Long_Elementary_Functions;
use  TEXT_IO, Ada.Long_Float_Text_IO, Ada.Numerics.Long_Elementary_Functions;

procedure Ada0901 is

    function f(x:Long_Float) return Long_Float is
    begin
        return x * x - 2.0;
    end f;

    function bisection(x0:Long_Float; x1:Long_Float) return Long_Float is
        a:  Long_Float;
        b:  Long_Float;
        c:  Long_Float;
        fc: Long_Float;
    begin
        a := x0;
        b := x1;
        while true loop
            -- 区間 (a, b) の中点 c = (a + b) / 2
            c := (a + b) / 2.0;
            Put(c,             Fore=>2, Aft=>10, Exp=>0);
            Put(Ascii.HT);
            Put(c - Sqrt(2.0), Fore=>2, Aft=>10, Exp=>0);
            New_Line;

            fc := f(c);
            if Abs(fc) < 0.0000000001 then
                exit;
            end if;

            if fc < 0.0 then
                -- f(c) < 0 であれば, 解は区間 (c, b) の中に存在
                a := c;
            else
                -- f(c) > 0 であれば, 解は区間 (a, c) の中に存在
                b := c;
            end if;
        end loop;

        return c;
    end bisection;

    a: Long_Float := 1.0;
    b: Long_Float := 2.0;
begin

    Put(bisection(a, b), Fore=>2, Aft=>10, Exp=>0);
    New_Line;
end Ada0901;
xxxxxx@yyyyyy /Z
$ gnatmake Ada0901.adb

xxxxxx@yyyyyy /Z
$ Ada0901
 1.5000000000    0.0857864376
 1.2500000000   -0.1642135624
 1.3750000000   -0.0392135624
 1.4375000000    0.0232864376
 1.4062500000   -0.0079635624
 1.4218750000    0.0076614376
 1.4140625000   -0.0001510624
 1.4179687500    0.0037551876
 1.4160156250    0.0018020626
 1.4150390625    0.0008255001
 1.4145507813    0.0003372189
 1.4143066406    0.0000930783
 1.4141845703   -0.0000289921
 1.4142456055    0.0000320431
 1.4142150879    0.0000015255
 1.4141998291   -0.0000137333
 1.4142074585   -0.0000061039
 1.4142112732   -0.0000022892
 1.4142131805   -0.0000003818
 1.4142141342    0.0000005718
 1.4142136574    0.0000000950
 1.4142134190   -0.0000001434
 1.4142135382   -0.0000000242
 1.4142135978    0.0000000354
 1.4142135680    0.0000000056
 1.4142135531   -0.0000000093
 1.4142135605   -0.0000000019
 1.4142135642    0.0000000019
 1.4142135624    0.0000000000
 1.4142135624

VB.NET

Option Explicit

Module VB0901
    Public Sub Main()
        Dim a As Double = 1.0
        Dim b As Double = 2.0
        Console.WriteLine(String.Format("{0,12:F10}", bisection(a, b)))
    End Sub

    Private Function bisection(ByVal a As Double, ByVal b As Double)
        Dim c As Double
        Do While(True)
            '区間 (a, b) の中点 c = (a + b) / 2
            c = (a + b) / 2
            Console.WriteLine(String.Format("{0,12:F10}{2}{1,13:F10}", c, c - Math.Sqrt(2), vbTab))

            Dim fc As Double = f(c)
            If Math.Abs(fc) < 0.0000000001 Then Exit Do

            If fc < 0 Then
                'f(c) < 0 であれば, 解は区間 (c, b) の中に存在
                a = c
            Else
                'f(c) > 0 であれば, 解は区間 (a, c) の中に存在
                b = c
            End If
        Loop

        Return c
    End Function

    Private Function f(ByVal x As Double) As Double
        Return x * x - 2.0
    End Function
End Module
Z:\>vbc -nologo VB0901.vb

Z:\>VB0901
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

C#

using System;

public class CS0901
{
    public static void Main()
    {
        double a = 1;
        double b = 2;
        Console.WriteLine(string.Format("{0,12:F10}", bisection(a, b)));
    }

    private static double bisection(double a, double b)
    {
        double c;
        while (true)
        {
            // 区間 (a, b) の中点 c = (a + b) / 2
            c = (a + b) / 2;
            Console.WriteLine(string.Format("{0,12:F10}\t{1,13:F10}", c, c - Math.Sqrt(2)));

            double fc = f(c);
            if (Math.Abs(fc) < 0.0000000001) break;

            if (fc < 0)
            {
                // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
                a = c;
            }
            else
            {
                // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
                b = c;
            }
        }
        return c;
    }

    private static double f(double x)
    {
        return x * x - 2;
    }
}
Z:\>csc -nologo CS0901.cs

Z:\>CS0901
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Java

import static java.lang.System.out;

public class Java0901 {

    public static void main(String []args) {
        double a = 1;
        double b = 2;
        out.println(String.format("%12.10f", bisection(a, b)));
    }

    private static double bisection(double a, double b) {
        double c;
        while (true) {
            // 区間 (a, b) の中点 c = (a + b) / 2
            c = (a + b) / 2;
            out.println(String.format("%12.10f\t%13.10f", c, c - Math.sqrt(2)));

            double fc = f(c);
            if (Math.abs(fc) < 0.0000000001) break;

            if (fc < 0){
                // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
                a = c;
            } else {
                // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
                b = c;
            }
        }
        return c;
    }

    private static double f(double x) {
        return x * x - 2;
    }
}
Z:\>javac Java0901.java

Z:\>java Java0901
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

C++

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

double f(double x)
{
    return x * x - 2;
}

double bisection(double a, double b)
{
    double c;
    while (true)
    {
        // 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2;
        cout << setw(12) << fixed << setprecision(10) << c << "\t";
        cout << setw(13) << fixed << setprecision(10) << c - sqrt(2) << endl;

        double fc = f(c);
        if (fabs(fc) < 0.0000000001) break;

        if (fc < 0)
        {
            // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c;
        }
        else
        {
            // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c;
        }
    }
    return c;
}

int main()
{
    double a = 1;
    double b = 2;
    cout << setw(12) << fixed << setprecision(10) << bisection(a, b) << endl;
    return 0;
}
Z:\>bcc32 -q CP0901.cpp
cp0901.cpp:

Z:\>CP0901
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507812     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Objective-C

#import <Foundation/Foundation.h>
#import <math.h>

double f(double x)
{
    return x * x - 2;
}

double bisection(double a, double b)
{
    double c;
    while (YES)
    {
        // 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2;
        printf("%12.10f\t%13.10f\n", c, c - sqrt(2));

        double fc = f(c);
        if (fabs(fc) < 0.0000000001) break;

        if (fc < 0)
        {
            // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c;
        }
        else
        {
            // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c;
        }
    }
    return c;
}

int main()
{
    double a = 1;
    double b = 2;
    printf("%12.10f\n", bisection(a, b));
    return 0;
}
xxxxxx@yyyyyy /Z
$ gcc -o OC0901 OC0901.m -lobjc -lgnustep-base -I $INCLUDE -L $LIB $CFLAGS

xxxxxx@yyyyyy /Z
$ OC0901
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

D

import std.stdio;
import std.math;

void main(string[] args)
{
    double a = 1;
    double b = 2;
    writefln("%12.10f", bisection(a, b));
}

double bisection(double a, double b)
{
    double c;
    while (true)
    {
        // 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2;
        writefln("%12.10f\t%13.10f", c, c - sqrt(2.0));

        double fc = f(c);
        if (fabs(fc) < 0.0000000001) break;

        if (fc < 0)
        {
            // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c;
        }
        else
        {
            // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c;
        }
    }
    return c;
}

double f(double x)
{
    return x * x - 2;
}
Z:\>dmd D0901.d

Z:\>D0901
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135623
1.4375000000     0.0232864376
1.4062500000    -0.0079635623
1.4218750000     0.0076614376
1.4140625000    -0.0001510623
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372188
1.4143066406     0.0000930782
1.4141845703    -0.0000289920
1.4142456055     0.0000320430
1.4142150879     0.0000015255
1.4141998291    -0.0000137332
1.4142074585    -0.0000061038
1.4142112732    -0.0000022891
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000055
1.4142135531    -0.0000000093
1.4142135605    -0.0000000018
1.4142135642     0.0000000018
1.4142135624     0.0000000000
1.4142135624

Go

package main

import "fmt"
import "math"

func main() {
    var a float64 = 1
    var b float64 = 2
    fmt.Printf("%12.10f\n", bisection(a, b))
}

func bisection(a float64, b float64) float64 {
    var c float64
    for {
        // 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2
        fmt.Printf("%12.10f\t%13.10f\n", c, c - math.Sqrt(2))

        var fc float64 = f(c)
        if math.Fabs(fc) < 0.0000000001 {
            break
        }
        if fc < 0 {
            // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            a = c
        } else {
            // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            b = c
        }
    }
    return c
}

func f(x float64) float64 {
    return x * x - 2
}
Z:\>8g GO0901.go

Z:\>8l -o GO0901.exe GO0901.8

Z:\>GO0901
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507812     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Scala

object Scala0901 {

    def main(args: Array[String]) {
        val a = 1.0
        val b = 2.0
        println("%12.10f".format(bisection(a, b)))
    }

    def bisection(a:Double, b:Double):Double = {
        // 区間 (a, b) の中点 c = (a + b) / 2
        val c = (a + b) / 2
        println("%12.10f\t%13.10f".format(c, c - Math.sqrt(2)))

        val fc = f(c)
        if (Math.abs(fc) < 0.0000000001)
            c
        else {
            if (fc < 0) {
                // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
                bisection(c, b)
            } else {
                // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
                bisection(a, c)
            }
        }
    }

    def f(x:Double) = {
        x * x - 2
    }
}
Z:\>scala Scala0901.scala
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

F#

module Fs0901

open System

let f (x:double):double =
    x * x - 2.0

let rec bisection (a:double) (b:double):double =
    // 区間 (a, b) の中点 c = (a + b) / 2
    let c = (a + b) / 2.0
    printfn "%12.10f\t%13.10f" c (c - Math.Sqrt(2.0))

    let fc = f c
    if abs(fc) < 0.0000000001 then
        c
    else
        if fc < 0.0 then
            // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
            bisection c b
        else
            // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
            bisection a c

let a = 1.0
let b = 2.0
printfn "%12.10f" (bisection a b)

exit 0
Z:\>fsi  --nologo --quiet Fs0901.fs
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135624

Clojure

(defn f[x]
    (- (* x x) 2.0))

(defn bisection [a b]
    ; 区間 (a, b) の中点 c = (a + b) / 2
    (def c (/ (+ a b) 2.0))
    (println (format "%12.10f\t%13.10f" c (- c (Math/sqrt 2))))

    (def fc (f c))
    (if (< (. Math abs fc) 0.00000000001)
        c
        (if (< fc 0)
            (bisection c b)
            (bisection a c))))

(def a 1.0)
(def b 2.0)
(println (format "%12.10f" (bisection a b)))
Z:\>java -cp C:\ProgramFiles\clojure-1.5.1\clojure-1.5.1.jar clojure.main Clj0901.clj
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507813     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624     0.0000000000
1.4142135615    -0.0000000009
1.4142135619    -0.0000000005
1.4142135622    -0.0000000002
1.4142135623    -0.0000000001
1.4142135623    -0.0000000000
1.4142135624    -0.0000000000
1.4142135624    -0.0000000000
1.4142135624

Haskell

import Text.Printf
import Debug.Trace

f::Double->Double
f x = x * x - 2.0

bisection::Double->Double->Double
bisection a b =
    let
        -- 区間 (a, b) の中点 c = (a + b) / 2
        c = (a + b) / 2.0
        fc = (f c) :: Double
    in
        if abs(fc) < 0.0000000001 then
            c
        else
            if fc < 0.0
                then
                    -- f(c) < 0 であれば, 解は区間 (c, b) の中に存在
                    trace (printf "%12.10f\t%13.10f" c (c - ((sqrt 2.0)::Double)))
                    bisection c b
                else
                    -- f(c) > 0 であれば, 解は区間 (a, c) の中に存在
                    trace (printf "%12.10f\t%13.10f" c (c - ((sqrt 2.0)::Double)))
                    bisection a c

main = do
    let a = 1.0
    let b = 2.0
    printf "%12.10f\n" (bisection a b)
Z:\>runghc Hs0901.hs
1.5000000000     0.0857864376
1.2500000000    -0.1642135624
1.3750000000    -0.0392135624
1.4375000000     0.0232864376
1.4062500000    -0.0079635624
1.4218750000     0.0076614376
1.4140625000    -0.0001510624
1.4179687500     0.0037551876
1.4160156250     0.0018020626
1.4150390625     0.0008255001
1.4145507812     0.0003372189
1.4143066406     0.0000930783
1.4141845703    -0.0000289921
1.4142456055     0.0000320431
1.4142150879     0.0000015255
1.4141998291    -0.0000137333
1.4142074585    -0.0000061039
1.4142112732    -0.0000022892
1.4142131805    -0.0000003818
1.4142141342     0.0000005718
1.4142136574     0.0000000950
1.4142134190    -0.0000001434
1.4142135382    -0.0000000242
1.4142135978     0.0000000354
1.4142135680     0.0000000056
1.4142135531    -0.0000000093
1.4142135605    -0.0000000019
1.4142135642     0.0000000019
1.4142135624
inserted by FC2 system