Tcl Library Source Code

Bounty program for improvements to Tcl and certain Tcl packages.

[ Main Table Of Contents | Table Of Contents | Keyword Index | Categories | Modules | Applications ]


math::calculus - Integration and ordinary differential equations

Table Of Contents


package require Tcl 8.4
package require math::calculus 0.8.2

::math::calculus::integral begin end nosteps func
::math::calculus::integralExpr begin end nosteps expression
::math::calculus::integral2D xinterval yinterval func
::math::calculus::integral2D_accurate xinterval yinterval func
::math::calculus::integral3D xinterval yinterval zinterval func
::math::calculus::integral3D_accurate xinterval yinterval zinterval func
::math::calculus::qk15 xstart xend func nosteps
::math::calculus::qk15_detailed xstart xend func nosteps
::math::calculus::eulerStep t tstep xvec func
::math::calculus::heunStep t tstep xvec func
::math::calculus::rungeKuttaStep t tstep xvec func
::math::calculus::boundaryValueSecondOrder coeff_func force_func leftbnd rightbnd nostep
::math::calculus::solveTriDiagonal acoeff bcoeff ccoeff dvalue
::math::calculus::newtonRaphson func deriv initval
::math::calculus::newtonRaphsonParameters maxiter tolerance
::math::calculus::regula_falsi f xb xe eps


This package implements several simple mathematical algorithms:

The package is fully implemented in Tcl. No particular attention has been paid to the accuracy of the calculations. Instead, well-known algorithms have been used in a straightforward manner.

This document describes the procedures and explains their usage.


This package defines the following public procedures:


Several of the above procedures take the names of procedures as arguments. To avoid problems with the visibility of these procedures, the fully-qualified name of these procedures is determined inside the calculus routines. For the user this has only one consequence: the named procedure must be visible in the calling procedure. For instance:

namespace eval ::mySpace {
   namespace export calcfunc
   proc calcfunc { x } { return $x }
# Use a fully-qualified name
namespace eval ::myCalc {
   proc detIntegral { begin end } {
      return [integral $begin $end 100 ::mySpace::calcfunc]
# Import the name
namespace eval ::myCalc {
   namespace import ::mySpace::calcfunc
   proc detIntegral { begin end } {
      return [integral $begin $end 100 calcfunc]

Enhancements for the second-order boundary value problem:


Let us take a few simple examples:

Integrate x over the interval [0,100] (20 steps):

proc linear_func { x } { return $x }
puts "Integral: [::math::calculus::integral 0 100 20 linear_func]"

For simple functions, the alternative could be:

puts "Integral: [::math::calculus::integralExpr 0 100 20 {$x}]"

Do not forget the braces!

The differential equation for a dampened oscillator:

x'' + rx' + wx = 0

can be split into a system of first-order equations:

x' = y
y' = -ry - wx

Then this system can be solved with code like this:

proc dampened_oscillator { t xvec } {
   set x  [lindex $xvec 0]
   set x1 [lindex $xvec 1]
   return [list $x1 [expr {-$x1-$x}]]

set xvec   { 1.0 0.0 }
set t      0.0
set tstep  0.1
for { set i 0 } { $i < 20 } { incr i } {
   set result [::math::calculus::eulerStep $t $tstep $xvec dampened_oscillator]
   puts "Result ($t): $result"
   set t      [expr {$t+$tstep}]
   set xvec   $result

Suppose we have the boundary value problem:

Dy'' + ky = 0
x = 0: y = 1
x = L: y = 0

This boundary value problem could originate from the diffusion of a decaying substance.

It can be solved with the following fragment:

proc coeffs { x } { return [list $::Diff 0.0 $::decay] }
proc force  { x } { return 0.0 }

set Diff   1.0e-2
set decay  0.0001
set length 100.0

set y [::math::calculus::boundaryValueSecondOrder \
   coeffs force {0.0 1.0} [list $length 0.0] 100]

Bugs, Ideas, Feedback

This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category math :: calculus of the Tcllib Trackers. Please also report any ideas for enhancements you may have for either package and/or documentation.

When proposing code changes, please provide unified diffs, i.e the output of diff -u.

Note further that attachments are strongly preferred over inlined patches. Attachments can be made 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.




calculus, differential equations, integration, math, roots




Copyright © 2002,2003,2004 Arjen Markus