Pascal Code
function
F(X:real):real;
begin
F
:= X*X*X - 3*X + 2;
end;
procedure
Secant(P0,P1,Delta,Epsilon:real;
var
P2,Y2,Dp,Ptwo,Pthree:real;
var
Cond,K:integer);
const Small
= 1E-20;
var Df,Y0,Y1,RelErr:real;
begin
K := 0;
Cond := 0;
Y0 := F(P0);
Y1 := F(P1);
while (K<Max) and
(Cond=0) do
begin
Df
:= (Y1 - Y0)/(P1 - P0);
if
Df = 0 then
begin
Cond
:= 1;
Dp
:= P1 - P0;
P2
:= P1;
end
else
begin
Dp
:= Y1/Df;
P2
:= P1 - Dp;
end;
Y2
:= F(P2);
RelErr
:= ABS(Dp)/(ABS(P2)+Small);
if
(RelErr <= Delta) then Cond := 2;
if
(ABS(Y2) < Epsilon) then Cond := 3;
if
(RelErr <= Delta) and (ABS(Y2) < Epsilon) then
Cond
:= 4;
P0
:= P1;
P1
:= P2;
Y0
:= Y1;
Y1
:= Y2;
K
:= K+1;
if
K = 1 then Ptwo := P1;
if
K = 2 then Pthree := P1;
end;
K := K+1;
end;
(c) John H. Mathews 2004