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
|
{ ******************************************************************
Secant method for nonlinear equation
****************************************************************** }
unit usecant;
interface
uses
utypes;
procedure Secant (Func : TFunc;
var X, Y : Float;
MaxIter : Integer;
Tol : Float;
var F : Float);
implementation
procedure Secant (Func : TFunc;
var X, Y : Float;
MaxIter : Integer;
Tol : Float;
var F : Float);
var
Iter : Integer;
G, Z : Float;
begin
Iter := 0;
SetErrCode(OptOk);
repeat
F := Func(X);
if MaxIter < 1 then Exit;
G := Func(Y);
Iter := Iter + 1;
if (F = G) or (Iter > MaxIter) then
begin
SetErrCode(OptNonConv);
Exit;
end;
Z := (X * G - Y * F) / (G - F);
X := Y;
Y := Z;
until Abs(X - Y) < Tol * (Abs(X) + Abs(Y));
X := 0.5 * (X + Y);
F := Func(X);
end;
end.
|