Numerical integration and root finding in Pascal
last modified November 19, 2026 Free Pascal 3.2.2
Numerical methods replace an exact formula with a finite computation. This
article implements the trapezoid, midpoint, and Simpson integration rules one at
a time, compares their errors on a known integral, and then finds the root of a
cubic with bisection, Newton's method, and the secant method. The integration
routines receive the function as a procedural parameter, and the root finders use
Extended precision so that the tolerances are meaningful. The cubic
x³ - 2x - 5 has one real root, about 2.0945514815, which makes
the results easy to check.
The trapezoid rule
An integral is approximated by dividing the interval into N panels and
replacing the function on each panel with a simple shape. The trapezoid rule
joins the two endpoints of a panel with a straight line, so the area of the panel
is the average of the endpoint values times the width. The function below takes
the integrand as a TFunc parameter, the interval, and the number of
panels.
program trapezoid;
{$mode objfpc}{$H+}
uses SysUtils;
type
TFunc = function(X: Extended): Extended;
function Cubic(X: Extended): Extended;
begin
Result := X * X * X;
end;
function Trapezoid(F: TFunc; A, B: Extended; N: Integer): Extended;
var
H, Sum: Extended;
I: Integer;
begin
H := (B - A) / N;
Sum := (F(A) + F(B)) / 2;
for I := 1 to N - 1 do
Sum := Sum + F(A + I * H);
Result := Sum * H;
end;
procedure Report(const Name: string; Value: Extended);
begin
WriteLn(Name, ': ', FormatFloat('0.000000000000', Value),
' error: ', FormatFloat('0.00E+00', Abs(Value - 0.25)));
end;
begin
WriteLn('integral of x^3 from 0 to 1, exact 0.25');
Report('trapezoid n=10 ', Trapezoid(@Cubic, 0, 1, 10));
Report('trapezoid n=100', Trapezoid(@Cubic, 0, 1, 100));
end.
The exact integral of x³ from 0 to 1 is 1/4. With ten panels the
trapezoid rule reports 0.2525, an error of 2.50E-03. The error is proportional
to h², where h = 1/N, so ten times more panels reduce
it by a factor of 100: the hundred-panel result is 0.250025. The
@Cubic argument passes the address of the function, which is how a
procedural type receives a routine.
$ fpc -gl trapezoid.pas $ ./trapezoid integral of x^3 from 0 to 1, exact 0.25 trapezoid n=10 : 0.252500000000 error: 2.50E-03 trapezoid n=100: 0.250025000000 error: 2.50E-05
The midpoint rule
The midpoint rule evaluates the function at the centre of each panel and treats the panel as a rectangle. It needs one evaluation per panel instead of two, and for a smooth function its error has the same order as the trapezoid error, with the opposite sign on this example. The code differs only in the formula for the sample point.
program midpoint;
{$mode objfpc}{$H+}
uses SysUtils;
type
TFunc = function(X: Extended): Extended;
function Cubic(X: Extended): Extended;
begin
Result := X * X * X;
end;
function Midpoint(F: TFunc; A, B: Extended; N: Integer): Extended;
var
H, Sum: Extended;
I: Integer;
begin
H := (B - A) / N;
Sum := 0;
for I := 0 to N - 1 do
Sum := Sum + F(A + (I + 0.5) * H);
Result := Sum * H;
end;
procedure Report(const Name: string; Value: Extended);
begin
WriteLn(Name, ': ', FormatFloat('0.000000000000', Value),
' error: ', FormatFloat('0.00E+00', Abs(Value - 0.25)));
end;
begin
WriteLn('integral of x^3 from 0 to 1, exact 0.25');
Report('midpoint n=10 ', Midpoint(@Cubic, 0, 1, 10));
Report('midpoint n=100', Midpoint(@Cubic, 0, 1, 100));
end.
Ten panels give 0.24875, which is 1.25E-03 below the exact value; the trapezoid rule was 2.50E-03 above it. A hundred panels reduce the midpoint error to 1.25E-05. The two rules bracket the exact value in this case because the function is convex on the interval, but that is a property of the example, not a general guarantee.
$ fpc -gl midpoint.pas $ ./midpoint integral of x^3 from 0 to 1, exact 0.25 midpoint n=10 : 0.248750000000 error: 1.25E-03 midpoint n=100: 0.249987500000 error: 1.25E-05
Simpson's rule
Simpson's rule fits a parabola through three points: the two endpoints and the midpoint of a pair of panels. The weights are 1, 4, 2, 4, ..., 2, 4, 1, and the result is multiplied by h/3. The number of panels must be even, because the rule consumes two panels at a time.
program simpson;
{$mode objfpc}{$H+}
uses SysUtils;
type
TFunc = function(X: Extended): Extended;
function Cubic(X: Extended): Extended;
begin
Result := X * X * X;
end;
function Simpson(F: TFunc; A, B: Extended; N: Integer): Extended;
var
H, Sum: Extended;
I: Integer;
begin
H := (B - A) / N;
Sum := F(A) + F(B);
for I := 1 to N - 1 do
if Odd(I) then
Sum := Sum + 4 * F(A + I * H)
else
Sum := Sum + 2 * F(A + I * H);
Result := Sum * H / 3;
end;
procedure Report(const Name: string; Value: Extended);
begin
WriteLn(Name, ': ', FormatFloat('0.000000000000', Value),
' error: ', FormatFloat('0.00E+00', Abs(Value - 0.25)));
end;
begin
WriteLn('integral of x^3 from 0 to 1, exact 0.25');
Report('simpson n=10 ', Simpson(@Cubic, 0, 1, 10));
Report('simpson n=100', Simpson(@Cubic, 0, 1, 100));
end.
Simpson's rule is exact for cubics in exact arithmetic, so both panel counts print 0.250000000000. The tiny 4.07E-20 at 100 panels is floating-point rounding noise, not a truncation error. This accuracy is why Simpson's rule is the default choice when the integrand is smooth and the panel count can be made even.
$ fpc -gl simpson.pas $ ./simpson integral of x^3 from 0 to 1, exact 0.25 simpson n=10 : 0.250000000000 error: 0.00E+00 simpson n=100: 0.250000000000 error: 4.07E-20
Bisection
Bisection needs an interval in which the function changes sign. It evaluates the midpoint, keeps the half whose endpoints still have opposite signs, and repeats. The interval is halved at every step, so the error after k steps is at most the original width divided by 2k. The method is slow but robust: it cannot fail as long as the initial interval brackets a root.
program bisection;
{$mode objfpc}{$H+}
uses SysUtils;
function F(X: Extended): Extended;
begin
Result := X * X * X - 2 * X - 5;
end;
var
A, B, Mid, FA, FM: Extended;
Iter: Integer;
begin
A := 2;
B := 3;
FA := F(A);
Iter := 0;
while (B - A) > 1E-12 do
begin
Mid := (A + B) / 2;
FM := F(Mid);
if FM = 0 then
Break;
if (FA < 0) <> (FM < 0) then
B := Mid
else
begin
A := Mid;
FA := FM;
end;
Inc(Iter);
end;
Mid := (A + B) / 2;
WriteLn('root: ', FormatFloat('0.000000000000', Mid));
WriteLn('f(root): ', FormatFloat('0.00E+00', F(Mid)));
WriteLn('iterations: ', Iter);
end.
The interval [2, 3] brackets the root because F(2) = -1 and F(3) = 16. The
condition (FA < 0) <> (FM < 0) is true when the two
values have opposite signs. After 40 iterations the interval is narrower than
1E-12, and the reported root is accurate to about 12 decimal places. The value
of F at the root is not exactly zero because the root is irrational.
$ fpc -gl bisection.pas $ ./bisection root: 2.094551481543 f(root): 2.68E-12 iterations: 40
Newton's method
Newton's method uses the derivative to jump directly toward the root: xn+1 = xn - F(xn)/F'(xn). Near a simple root the number of correct digits roughly doubles at every step, which is why it needs only a handful of iterations. The price is that the derivative must be available and that the initial guess must be close enough to converge.
program newton;
{$mode objfpc}{$H+}
uses SysUtils;
function F(X: Extended): Extended;
begin
Result := X * X * X - 2 * X - 5;
end;
function DF(X: Extended): Extended;
begin
Result := 3 * X * X - 2;
end;
var
X, Next: Extended;
Iter: Integer;
begin
X := 2.0;
Iter := 0;
repeat
Next := X - F(X) / DF(X);
if Abs(Next - X) < 1E-12 then
Break;
X := Next;
Inc(Iter);
until Iter = 100;
WriteLn('root: ', FormatFloat('0.000000000000', Next));
WriteLn('f(root): ', FormatFloat('0.00E+00', F(Next)));
WriteLn('iterations: ', Iter);
end.
The derivative 3x² - 2 is about 10 at x = 2, so the first step lands near 2.1 and the following steps refine the last digits quickly. The loop stops when the step is smaller than 1E-12, and the counter is a safety net against a cycle or a division by a derivative close to zero. Four iterations produce the same root as 40 bisection steps, at the cost of evaluating two functions per step instead of one.
$ fpc -gl newton.pas $ ./newton root: 2.094551481542 f(root): -1.73E-18 iterations: 4
The secant method
The secant method replaces the derivative with a finite difference computed from the last two points: xn+1 = xn - F(xn)(xn - xn-1)/(F(xn) - F(xn-1)). It needs two starting values and no derivative, and it converges almost as fast as Newton's method. The first step can be poor when the two starting points are far apart.
program secant;
{$mode objfpc}{$H+}
uses SysUtils;
function F(X: Extended): Extended;
begin
Result := X * X * X - 2 * X - 5;
end;
var
X0, X1, X2: Extended;
Iter: Integer;
begin
X0 := 2.0;
X1 := 3.0;
Iter := 0;
repeat
X2 := X1 - F(X1) * (X1 - X0) / (F(X1) - F(X0));
if Abs(X2 - X1) < 1E-12 then
Break;
X0 := X1;
X1 := X2;
Inc(Iter);
until Iter = 100;
WriteLn('root: ', FormatFloat('0.000000000000', X2));
WriteLn('f(root): ', FormatFloat('0.00E+00', F(X2)));
WriteLn('iterations: ', Iter);
end.
Starting from 2 and 3, the secant line through the two function values points
close to the root, and six iterations reach the 12-digit accuracy. The
denominator is the difference of two function values; if the two points ever
produce the same value, the division by zero raises EZeroDivide,
so a production routine checks for it. The secant method is a good default
when the derivative is expensive or unavailable.
$ fpc -gl secant.pas $ ./secant root: 2.094551481542 f(root): 1.30E-18 iterations: 6
Next steps
The procedural types article explains the function parameters used by the integration rules, and the floating-point article covers the rounding and tolerance questions behind these comparisons. The linear algebra article applies similar numerical care to matrices, and the scientific simulations article integrates differential equations with Euler and Runge-Kutta steps.
Source
In this article we integrated a cubic with the trapezoid, midpoint, and Simpson rules, measured their errors, and found a root with bisection, Newton's method, and the secant method. The series continues with linear algebra in Pascal.
Author
List all Pascal tutorials.