root / Ludo / numgrad.inc
Historique | Voir | Annoter | Télécharger (916 octets)
1 | 3 | avalancogn | { ****************************************************************** |
---|---|---|---|
2 | Numerical gradient |
||
3 | ****************************************************************** } |
||
4 | |||
5 | procedure Gradient(X, G : TVector); |
||
6 | |||
7 | const |
||
8 | Eta = 1.0E-4; { Relative increment } |
||
9 | |||
10 | var |
||
11 | I : Integer; { Loop variable } |
||
12 | Temp : Float; { Temporary variable } |
||
13 | Delta : Float; { Increment } |
||
14 | Xm : Float; { X - Delta } |
||
15 | Xp : Float; { X + Delta } |
||
16 | Fm : Float; { F(X - Delta) } |
||
17 | Fp : Float; { F(X + Delta) } |
||
18 | |||
19 | begin |
||
20 | for I := 1 to Nvar do |
||
21 | begin |
||
22 | if X[I] <> 0.0 then |
||
23 | Delta := Eta * Abs(X[I]) |
||
24 | else |
||
25 | Delta := Eta; |
||
26 | |||
27 | Xp := X[I] + Delta; |
||
28 | Xm := X[I] - Delta; |
||
29 | |||
30 | Temp := X[I]; |
||
31 | |||
32 | X[I] := Xm; |
||
33 | Fm := Func(X); |
||
34 | |||
35 | X[I] := Xp; |
||
36 | Fp := Func(X); |
||
37 | |||
38 | G[I] := (Fp - Fm) / (2.0 * Delta); |
||
39 | |||
40 | X[I] := Temp |
||
41 | end; |
||
42 | end; |