~ubuntu-branches/ubuntu/utopic/mricron/utopic

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
{ ******************************************************************
  Brackets a minimum of a function
  ****************************************************************** }

unit uminbrak;

interface

uses
  utypes, uminmax;

procedure MinBrack(Func : TFunc; var A, B, C, Fa, Fb, Fc : Float);

implementation

procedure MinBrack(Func : TFunc; var A, B, C, Fa, Fb, Fc : Float);
{ ------------------------------------------------------------------
  Given two points (A, B) this procedure finds a triplet (A, B, C)
  such that:

  1) A < B < C
  2) A, B, C are within the golden ratio
  3) Func(B) < Func(A) and Func(B) < Func(C).

  The corresponding function values are returned in Fa, Fb, Fc
  ------------------------------------------------------------------ }

  begin
    if A > B then
      FSwap(A, B);

    Fa := Func(A);
    Fb := Func(B);

    if Fb > Fa then
      begin
        FSwap(A, B);
        FSwap(Fa, Fb);
      end;

    C := B + GOLD * (B - A);
    Fc := Func(C);

    while Fc < Fb do
      begin
        A := B;
        B := C;
        Fa := Fb;
        Fb := Fc;
        C := B + GOLD * (B - A);
        Fc := Func(C);
      end;

    if A > C then
      begin
        FSwap(A, C);
        FSwap(Fa, Fc);
      end;
  end;

end.