math::calculus::romberg(3tcl)
β¨ math::calculus::romberg π
okay bestie, this is the manual page for math::calculus::romberg(3tcl) from the Tcl Math Library. Romberg integration? It's giving numerical integration but make it fashion. Let's go. π¦
πΈ NAME πΈ
math::calculus::romberg β Romberg integration
β¨ SYNOPSIS β¨
package require Tcl 8.5 9
package require math::calculus 0.6
::math::calculus::romberg f a b ?-option value...?
::math::calculus::romberg_infinity f a b ?-option value...?
::math::calculus::romberg_sqrtSingLower f a b ?-option value...?
::math::calculus::romberg_sqrtSingUpper f a b ?-option value...?
::math::calculus::romberg_powerLawLower gamma f a b ?-option value...?
::math::calculus::romberg_powerLawUpper gamma f a b ?-option value...?
::math::calculus::romberg_expLower f a b ?-option value...?
::math::calculus::romberg_expUpper f a b ?-option value...?
π DESCRIPTION π
The romberg procedures in the math::calculus package do numerical integration of a function of one variable. They're meant to be "production quality" β robust, precise, and reasonably efficient in terms of how many function evaluations they use. No cap. β¨
π PROCEDURES π
The following procedures are available for Romberg integration:
::math::calculus::romberg f a b ?-option value...?β Integrates an analytic function over a given interval.::math::calculus::romberg_infinity f a b ?-option value...?β Integrates an analytic function over a half-infinite interval.::math::calculus::romberg_sqrtSingLower f a b ?-option value...?β Integrates a function that is expected to be analytic over an interval except for the presence of an inverse square root singularity at the lower limit.::math::calculus::romberg_sqrtSingUpper f a b ?-option value...?β Integrates a function that is expected to be analytic over an interval except for the presence of an inverse square root singularity at the upper limit.::math::calculus::romberg_powerLawLower gamma f a b ?-option value...?β Integrates a function that is expected to be analytic over an interval except for the presence of a power law singularity at the lower limit.::math::calculus::romberg_powerLawUpper gamma f a b ?-option value...?β Integrates a function that is expected to be analytic over an interval except for the presence of a power law singularity at the upper limit.::math::calculus::romberg_expLower f a b ?-option value...?β Integrates an exponentially growing function; the lower limit of the region of integration may be arbitrarily large and negative.::math::calculus::romberg_expUpper f a b ?-option value...?β Integrates an exponentially decaying function; the upper limit of the region of integration may be arbitrarily large.
π PARAMETERS π
fβ Function to integrate. Must be expressed as a single Tcl command, to which will be appended a single argument, specifically, the abscissa at which the function is to be evaluated. The first word of the command will be processed withnamespace whichin the caller's scope prior to any evaluation. Given this processing, the command may be local to the calling namespace rather than needing to be global.aβ Lower limit of the region of integration.bβ Upper limit of the region of integration. For theromberg_sqrtSingLower,romberg_sqrtSingUpper,romberg_powerLawLower,romberg_powerLawUpper,romberg_expLower, andromberg_expUpperprocedures, the lower limit must be strictly less than the upper. For the other procedures, the limits may appear in either order.gammaβ Power to use for a power law singularity; see section IMPROPER INTEGRALS for details.
π OPTIONS π
-abserror epsilonβ Requests that the integration machinery proceed at most until the estimated absolute error of the integral is less thanepsilon. The error may be seriously over- or underestimated if the function (or any of its derivatives) contains singularities; see section IMPROPER INTEGRALS for details. Default is1.0e-08.-relerror epsilonβ Requests that the integration machinery proceed at most until the estimated relative error of the integral is less thanepsilon. The error may be seriously over- or underestimated if the function (or any of its derivatives) contains singularities; see section IMPROPER INTEGRALS for details. Default is1.0e-06.-maxiter mβ Requests that integration terminate after at mostntriplings of the number of evaluations performed. In other words, givennfor-maxiter, the integration machinery will make at most3**nevaluations of the function. Default is14, corresponding to a limit approximately4.8 millionevaluations. (Well-behaved functions will seldom require more than a few hundred evaluations.)-degree dβ Requests that an extrapolating polynomial of degreedbe used in Romberg integration; see section DESCRIPTION for details. Default is4. Can be at mostm-1.
π DESCRIPTION π (the sequel)
The romberg procedure performs Romberg integration using the modified midpoint rule. Romberg integration is an iterative process. At the first step, the function is evaluated at the midpoint of the region of integration, and the value is multiplied by the width of the interval for the coarsest possible estimate. At the second step, the interval is divided into three parts, and the function is evaluated at the midpoint of each part; the sum of the values is multiplied by three. At the third step, nine parts are used, at the fourth twenty-seven, and so on, tripling the number of subdivisions at each step.
Once the interval has been divided at least d times, a polynomial is fitted to the integrals estimated in the last d+1 divisions. The integrals are considered to be a function of the square of the width of the subintervals (any good numerical analysis text will discuss this process under "Romberg integration"). The polynomial is extrapolated to a step size of zero, computing a value for the integral and an estimate of the error.
This process will be well-behaved only if the function is analytic over the region of integration; there may be removable singularities at either end of the region provided that the limit of the function (and of all its derivatives) exists as the ends are approached. Thus, romberg may be used to integrate a function like f(x)=sin(x)/x over an interval beginning or ending at zero.
Note:rombergwill either fail to converge or else return incorrect error estimates if the function, or any of its derivatives, has a singularity anywhere in the region of integration (except for the case mentioned above). Care must be used, therefore, in integrating a function like1/(1-x**2)to avoid the places where the derivative is singular.
π¦ IMPROPER INTEGRALS π¦
Romberg integration is also useful for integrating functions over half-infinite intervals or functions that have singularities. The trick is to make a change of variable to eliminate the singularity, and to put the singularity at one end or the other of the region of integration. The math::calculus package supplies a number of romberg procedures to deal with the commoner cases.
romberg_infinity
Integrates a function over a half-infinite interval; either a or b may be infinite. a and b must be of the same sign; if you need to integrate across the axis, say, from a negative value to positive infinity, use romberg to integrate from the negative value to a small positive value, and then romberg_infinity to integrate from the positive value to positive infinity. The romberg_infinity procedure works by making the change of variable u=1/x, so that the integral from a to b of f(x) is evaluated as the integral from 1/a to 1/b of f(1/u)/u**2.
romberg_powerLawLower and romberg_powerLawUpper
Integrate a function that has an integrable power law singularity at either the lower or upper bound of the region of integration (or has a derivative with a power law singularity there). These procedures take a first parameter, gamma, which gives the power law. The function or its first derivative are presumed to diverge as (x-a)**(-gamma) or (b-x)**(-gamma). gamma must be greater than zero and less than 1.
These procedures are useful not only in integrating functions that go to infinity at one end of the region of integration, but also functions whose derivatives do not exist at the end of the region. For instance, integrating f(x)=pow(x,0.25) with the origin as one end of the region will result in the romberg procedure greatly underestimating the error in the integral. The problem can be fixed by observing that the first derivative of f(x), f'(x)=x**(-3/4)/4, goes to infinity at the origin. Integrating using romberg_powerLawLower with gamma set to 0.75 gives much more orderly convergence.
These procedures operate by making the change of variable u=(x-a)**(1-gamma) (romberg_powerLawLower) or u=(b-x)**(1-gamma) (romberg_powerLawUpper).
To summarize the meaning of gamma:
- If
f(x) ~ x**(-a)(0 < a < 1), usegamma = a - If
f'(x) ~ x**(-b)(0 < b < 1), usegamma = b
romberg_sqrtSingLower and romberg_sqrtSingUpper
These procedures behave identically to romberg_powerLawLower and romberg_powerLawUpper for the common case of gamma=0.5; that is, they integrate a function with an inverse square root singularity at one end of the interval. They have a simpler implementation involving square roots rather than arbitrary powers.
romberg_expLower and romberg_expUpper
These procedures are for integrating a function that grows or decreases exponentially over a half-infinite interval. romberg_expLower handles exponentially growing functions, and allows the lower limit of integration to be an arbitrarily large negative number. romberg_expUpper handles exponentially decaying functions and allows the upper limit of integration to be an arbitrary large positive number. The functions make the change of variable u=exp(-x) and u=exp(x) respectively.
π¦ OTHER CHANGES OF VARIABLE π¦
Okay bestie, so what if you need an improper integral that isn't on the little list we already covered? Plot twist: you can write a change of variable yourself in literally like five lines of Tcl. No cap. But the Tcl coding for it is lowkey arcane, so we're doing a full worked example together. Grab a snack. π
Say the function we're integrating is f(x)=exp(x)/sqrt(1-x*x) (not exactly a natural function, but it's a great example), and we want to integrate it over the interval (-1,1). Notice how the denominator falls to zero at both ends of that interval? Yeah. π
We wanna make a change of variable from x to u so that dx/sqrt(1-x**2) maps to du. Pick x=sin(u), and you can find that dx=cos(u)*du, and sqrt(1-x**2)=cos(u). The integral from a to b of f(x) is the integral from asin(a) to asin(b) of f(sin(u))*cos(u).
So we can make a function g that takes an arbitrary function f plus the parameter u, and computes this new integrand. Behold:
proc g { f u } {
set x [expr { sin($u) }]
set cmd $f; lappend cmd $x; set y [eval $cmd]
return [expr { $y / cos($u) }]
}
So now, integrating f from a to b is the exact same thing as integrating g from asin(a) to asin(b). β¨ The one tricky bit is getting f consistently evaluated in the caller's scope β and this next procedure handles that for you:
proc romberg_sine { f a b args } {
set f [lreplace $f 0 0 [uplevel 1 [list namespace which [lindex $f 0]]]]
set f [list g $f]
return [eval [linsert $args 0 romberg $f [expr { asin($a) }] [expr { asin($b) }]]]
}
This romberg_sine procedure will do any function with sqrt(1-x*x) in the denominator. Our sample function is f(x)=exp(x)/sqrt(1-x*x):
proc f { x } {
expr { exp($x) / sqrt( 1. - $x*$x ) }
}
And integrating it? Just apply romberg_sine exactly like you'd use any of the other romberg procedures. Easy. Iconic. π
foreach { value error } [romberg_sine f -1.0 1.0] break
puts [format "integral is %.6g +/- %.6g" $value $error]
integral is 3.97746 +/- 2.3557e-010
π«Ά BUGS, IDEAS, FEEDBACK π«Ά
Real talk: this document, and the package it describes, will undoubtedly contain bugs and other problems. That's just how it is, bestie. π Please report them in the category math :: calculus of the Tcllib Trackers at [http://core.tcl.tk/tcllib/reportlist](http://core.tcl.tk/tcllib/reportlist).
Also please report any ideas for enhancements you may have for either the package and/or the documentation. We want the ideas. Feed us. πΈ
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Also important: attachments are strongly preferred over inlined patches. You can make an attachment by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. Don't skip this part. π
π SEE ALSO π
math::calculusmath::interpolate
π CATEGORY π
Mathematics
π COPYRIGHT π
Copyright (c) 2004 Kevin B. Kenny <kennykb@acm.org>. All rights reserved. Redistribution permitted under the terms of the Open Publication License <http://www.opencontent.org/openpub/>
And that's a wrap on the whole page, bestie!! π This is math::calculus::romberg(3tcl) from tcllib, version 0.6, and yeah we really did read all of it. Go integrate something improper. Slay. β¨π¦π
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2004 Kevin B\&. Kenny <kennykb@acm\&.org>\&. All rights reserved\&. Redistribution permitted under the terms of the Open Publication License <http://www\&.opencontent\&.org/openpub/>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms