Finding roots of f(x) = x^3 - 3x^2 + 2x using Secant method.
// Finding roots of f(x) = x^3 - 3x^2 + 2x using Secant method.
function f(x : Float) : Float; begin Result := x * x * x - 3.0 * x * x + 2.0 * x; end;
function Secant(xA, xB : Float) : Float;
var fA, fB, d : Float; i : Integer;
begin
fA := f(xA);
for i := 0 to 50 do begin
fB := f(xB);
if Abs(fB - fA) < 1e-15 then Break;
d := (xB - xA) / (fB - fA) * fB;
if Abs(d) < 1e-12 then Exit(xB);
xA := xB; fA := fB; xB -= d;
end;
Result := xB;
end;
PrintLn('Root found: ' + Secant(1.5, 2.5).ToString(3));
Root found: 2.000