@menu
* Functions and Variables for romberg::
@end menu

@node Functions and Variables for romberg, , Top, Top
@section Functions and Variables for romberg

@anchor{romberg_function}
@deffn {Function} romberg @
@fname{romberg} (@var{expr}, @var{x}, @var{a}, @var{b}) @
@fname{romberg} (@var{F}, @var{a}, @var{b})

Computes a numerical integration by Romberg's method.

@code{romberg(@var{expr}, @var{x}, @var{a}, @var{b})}
returns an estimate of the integral @code{integrate(@var{expr}, @var{x}, @var{a}, @var{b})}.
@var{expr} must be an expression which evaluates to a floating point value
when @var{x} is bound to a floating point value.

@code{romberg(@var{F}, @var{a}, @var{b})}
returns an estimate of the integral @code{integrate(@var{F}(x), x, @var{a}, @var{b})}
where @code{x} represents the unnamed, sole argument of @var{F};
the actual argument is not named @code{x}.
@var{F} must be a Maxima or Lisp function which returns a floating point value
when the argument is a floating point value.
@var{F} may name a translated or compiled Maxima function.

The accuracy of @code{romberg} is governed by the global variables
@code{rombergabs} and @code{rombergtol}.
@code{romberg} terminates successfully when
the absolute difference between successive approximations is less than @code{rombergabs},
or the relative difference in successive approximations is less than @code{rombergtol}.
Thus when @code{rombergabs} is 0.0 (the default)
only the relative error test has any effect on @code{romberg}.

@code{romberg} halves the stepsize at most @code{rombergit} times before it gives up;
the maximum number of function evaluations is therefore @code{2^rombergit}.
If the error criterion established by @code{rombergabs} and @code{rombergtol}
is not satisfied, @code{romberg} prints an error message.
@code{romberg} always makes at least @code{rombergmin} iterations;
this is a heuristic intended to prevent spurious termination when the integrand is oscillatory.

@code{romberg} repeatedly evaluates the integrand after binding the variable
of integration to a specific value (and not before).
This evaluation policy makes it possible to nest calls to @code{romberg},
to compute multidimensional integrals.
However, the error calculations do not take the errors of nested integrations
into account, so errors may be underestimated.
Also, methods devised especially for multidimensional problems may yield
the same accuracy with fewer function evaluations.

See also @mref{Introduction to QUADPACK}, a collection of numerical integration functions.

Examples:

A 1-dimensional integration.
@c ===beg===
@c f(x) := 1/((x - 1)^2 + 1/100) + 1/((x - 2)^2 + 1/1000) 
@c               + 1/((x - 3)^2 + 1/200);
@c rombergtol : 1e-6;
@c rombergit : 15;
@c estimate : romberg (f(x), x, -5, 5);
@c exact : integrate (f(x), x, -5, 5);
@c abs (estimate - exact) / exact, numer;
@c ===end===
@example
@group
(%i1) f(x) := 1/((x - 1)^2 + 1/100) + 1/((x - 2)^2 + 1/1000)
              + 1/((x - 3)^2 + 1/200);
                    1                 1                1
(%o1) f(x) := -------------- + --------------- + --------------
                     2    1           2    1            2    1
              (x - 1)  + ---   (x - 2)  + ----   (x - 3)  + ---
                         100              1000              200
@end group
@group
(%i2) rombergtol : 1e-6;
(%o2)                 9.999999999999999e-7
@end group
@group
(%i3) rombergit : 15;
(%o3)                          15
@end group
@group
(%i4) estimate : romberg (f(x), x, -5, 5);
(%o4)                   173.6730736617464
@end group
@group
(%i5) exact : integrate (f(x), x, -5, 5);
        3/2          3/2      3/2          3/2
(%o5) 10    atan(7 10   ) + 10    atan(3 10   )
      3/2         9/2       3/2         5/2
 + 5 2    atan(5 2   ) + 5 2    atan(5 2   ) + 10 atan(60)
 + 10 atan(40)
@end group
@group
(%i6) abs (estimate - exact) / exact, numer;
(%o6)                 7.552722451569877e-11
@end group
@end example

A 2-dimensional integration, implemented by nested calls to @code{romberg}.
@c ===beg===
@c g(x, y) := x*y / (x + y);
@c rombergtol : 1e-6;
@c estimate : romberg (romberg (g(x, y), y, 0, x/2), x, 1, 3);
@c assume (x > 0);
@c integrate (integrate (g(x, y), y, 0, x/2), x, 1, 3);
@c exact : radcan (%);
@c abs (estimate - exact) / exact, numer;
@c ===end===
@example
@group
(%i1) g(x, y) := x*y / (x + y);
                                    x y
(%o1)                   g(x, y) := -----
                                   x + y
@end group
@group
(%i2) rombergtol : 1e-6;
(%o2)                 9.999999999999999e-7
@end group
@group
(%i3) estimate : romberg (romberg (g(x, y), y, 0, x/2), x, 1, 3);
(%o3)                  0.8193023962835647
@end group
@group
(%i4) assume (x > 0);
(%o4)                        [x > 0]
@end group
@group
(%i5) integrate (integrate (g(x, y), y, 0, x/2), x, 1, 3);
                                           3
                                     2 log(-) - 1
                    9                      2        9
(%o5)      (- 9 log(-)) + 9 log(3) + ------------ + -
                    2                     6         2
@end group
@group
(%i6) exact : radcan (%);
                    26 log(3) - 26 log(2) - 13
(%o6)             - --------------------------
                                3
@end group
@group
(%i7) abs (estimate - exact) / exact, numer;
(%o7)                 1.371197987185102e-10
@end group
@end example

@opencatbox
@category{Package romberg}
@category{Numerical methods}
@closecatbox

@end deffn

@anchor{rombergabs}
@defvr {Option variable} rombergabs
Default value: 0.0

The accuracy of @code{romberg} is governed by the global variables
@code{rombergabs} and @code{rombergtol}.
@code{romberg} terminates successfully when
the absolute difference between successive approximations is less than @code{rombergabs},
or the relative difference in successive approximations is less than @code{rombergtol}.
Thus when @code{rombergabs} is 0.0 (the default)
only the relative error test has any effect on @code{romberg}.

See also @mref{rombergit} and @mref{rombergmin}.

@opencatbox
@category{Package romberg}
@closecatbox

@end defvr

@anchor{rombergit}
@defvr {Option variable} rombergit
Default value: 11

@code{romberg} halves the stepsize at most @code{rombergit} times before it gives up;
the maximum number of function evaluations is therefore @code{2^rombergit}.
@code{romberg} always makes at least @code{rombergmin} iterations;
this is a heuristic intended to prevent spurious termination when the integrand is oscillatory.

See also @mref{rombergabs} and @mref{rombergtol}.

@opencatbox
@category{Package romberg}
@closecatbox

@end defvr

@anchor{rombergmin}
@defvr {Option variable} rombergmin
Default value: 0

@code{romberg} always makes at least @code{rombergmin} iterations;
this is a heuristic intended to prevent spurious termination when the integrand is oscillatory.

See also @mref{rombergit}, @mref{rombergabs}, and @mref{rombergtol}.

@opencatbox
@category{Package romberg}
@closecatbox

@end defvr

@anchor{rombergtol}
@defvr {Option variable} rombergtol
Default value: 1e-4

The accuracy of @code{romberg} is governed by the global variables
@code{rombergabs} and @code{rombergtol}.
@code{romberg} terminates successfully when
the absolute difference between successive approximations is less than @code{rombergabs},
or the relative difference in successive approximations is less than @code{rombergtol}.
Thus when @code{rombergabs} is 0.0 (the default)
only the relative error test has any effect on @code{romberg}.

See also @mref{rombergit} and @mref{rombergmin}.

@opencatbox
@category{Package romberg}
@closecatbox

@end defvr

