Statistiques
| Révision:

root / numhess.inc @ 3

Historique | Voir | Annoter | Télécharger (1,668 ko)

1
{ ******************************************************************
2
  Numerical hessian and gradient
3
  ****************************************************************** }
4

    
5
procedure HessGrad(X, G : TVector; H : TMatrix);
6

    
7
const
8
  Eta = 1.0E-6;     { Relative increment }
9

    
10
var
11
  Delta, Xminus, Xplus, Fminus, Fplus : TVector;
12
  Temp1, Temp2, F, F2plus             : Float;
13
  I, J                                : Integer;
14

    
15
begin
16
  DimVector(Delta, Nvar);   { Increments   }
17
  DimVector(Xminus, Nvar);  { X - Delta    }
18
  DimVector(Xplus, Nvar);   { X + Delta    }
19
  DimVector(Fminus, Nvar);  { F(X - Delta) }
20
  DimVector(Fplus, Nvar);   { F(X + Delta) }
21

    
22
  F := Func(X);
23

    
24
  for I := 1 to Nvar do
25
    begin
26
      if X[I] <> 0.0 then
27
        Delta[I] := Eta * Abs(X[I])
28
      else
29
        Delta[I] := Eta;
30
      Xplus[I] := X[I] + Delta[I];
31
      Xminus[I] := X[I] - Delta[I];
32
    end;
33

    
34
  for I := 1 to Nvar do
35
    begin
36
      Temp1 := X[I];
37
      X[I] := Xminus[I];
38
      Fminus[I] := Func(X);
39
      X[I] := Xplus[I];
40
      Fplus[I] := Func(X);
41
      X[I] := Temp1;
42
    end;
43

    
44
  for I := 1 to Nvar do
45
    begin
46
      G[I] := (Fplus[I] - Fminus[I]) / (2.0 * Delta[I]);
47
      H[I,I] := (Fplus[I] + Fminus[I] - 2.0 * F) / Sqr(Delta[I]);
48
    end;
49

    
50
  for I := 1 to Pred(Nvar) do
51
    begin
52
      Temp1 := X[I];
53
      X[I] := Xplus[I];
54
      for J := Succ(I) to Nvar do
55
        begin
56
          Temp2 := X[J];
57
          X[J] := Xplus[J];
58
          F2plus := Func(X);
59
          H[I,J] := (F2plus - Fplus[I] - Fplus[J] + F) / (Delta[I] * Delta[J]);
60
          H[J,I] := H[I,J];
61
          X[J] := Temp2;
62
        end;
63
      X[I] := Temp1;
64
    end;
65

    
66
end;
67