Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

findroot.cpp

Go to the documentation of this file.
00001 /*
00002     Sheep - A Rigid Body Dynamics Engine
00003     Copyright (C) 2001-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of Sheep.
00007 
00008     Sheep is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     Sheep is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with Sheep; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #include "findroot.h"   // include first
00024 
00025 #include <cassert>
00026 #include <cmath>    // std::fabs()
00027 
00028 using namespace sheep;
00029 
00030 Real sheep::FindRoot(const Function1 &f, Real left, Real right, Real tolerance) {
00031     assert(tolerance >= 0);
00032 
00033     if(left > right) {
00034         Real dummy = left;
00035         left = right;
00036         right = dummy;
00037     }
00038 
00039     Real fl = f.EvaluateAt(left);
00040     Real fr = f.EvaluateAt(right);
00041 
00042     if(fabs(fl) < tolerance)
00043         return fl;
00044     else if(fabs(fr) < tolerance)
00045         return fr;
00046 
00047     assert(fl * fr < 0);
00048 
00049     int sign = fr > 0 ? 1 : -1;
00050 
00051     while(right - left > tolerance) {
00052         Real mid = (left + right) / 2;
00053         Real fm = f.EvaluateAt(mid);
00054 
00055         if(sign * fm > 0)
00056             right = mid;
00057         else left = mid;
00058     }
00059 
00060     return (left + right) / 2;
00061 }
00062 
00063 //  A zero of the function f(x) is computed in the interval left, right.
00064 //
00065 //  Input.
00066 //
00067 //  left        left endpoint of initial interval
00068 //  right       right endpoint of initial interval
00069 //  f           function subprogram which evaluates f(x) for any x in
00070 //              the interval left, right
00071 //  tolerance   desired length of the interval of uncertainty of the
00072 //              final result (>= 0.0)
00073 //
00074 //  Output.
00075 //
00076 //  zeroin abcissa approximating a zero of f in the interval left, right.
00077 //
00078 //
00079 //  It is assumed  that   f(left)   and   f(right)   have  opposite  signs
00080 //  without  a  check.  zeroin  returns a zero x in the given interval
00081 //  left, right to within a tolerance 4 * macheps * abs(x) + tolerance,
00082 //  where macheps is the relative machine precision.
00083 //
00084 //  This function subprogram is a slightly  modified  translation of
00085 //  the algol 60 procedure  zero  given in  richard brent, algorithms for
00086 //  minimization without derivatives, prentice - hall, inc. (1973).
00087 
00088 /*
00089 Real FindRoot(const Function1 &f, Real left, Real right, Real tolerance) {
00090     assert(tolerance >= 0);
00091 
00092     // Compute eps, the relative machine precision.
00093     //!\todo Move that part out of this function.
00094 
00095     Real eps = 1;
00096     Real tol1;
00097 
00098     do {
00099         eps /= 2;
00100         tol1 = 1 + eps;
00101     } while(tol1 > 1);
00102 
00103     // Initialization.
00104 
00105     Real a = left;
00106     Real b = right;
00107     Real fa = f.EvaluateAt(a);
00108     Real fb = f.EvaluateAt(b);
00109 
00110     //!\todo Throw an exception instead.
00111     assert(fa * fb < 0);
00112 
00113     // Begin step.
00114 
00115     Real c = a;
00116     Real fc = fa;
00117     Real d = b - a;
00118     Real e = d;
00119 
00120     while(1) {
00121         if(fabs(fc) < fabs(fb)) {
00122             a = b;
00123             b = c;
00124             c = a;
00125 
00126             fa = fb;
00127             fb = fc;
00128             fc = fa;
00129         }
00130 
00131         // Convergence test.
00132 
00133         Real mid = 0.5 * (c - b);
00134         Real tol = 2 * eps * fabs(b) + 0.5 * tolerance;
00135 
00136         if(fabs(mid) <= tol)
00137             break;
00138 
00139         if(fb == 0)
00140             break;
00141 
00142         if(fabs(e) < tol || fabs(fa) <= fabs(fb)) { // is bisection necessary?
00143             // Bisection.
00144             e = d = mid;
00145         } else {
00146             Real p, q, r, s;
00147 
00148             if(a != c) {    // is quadratic interpolation possible?
00149                 // Inverse quadratic interpolation.
00150                 q = fa / fc;
00151                 r = fb / fc;
00152                 s = fb / fa;
00153                 p = s * (2 * mid * q * (q - r) - (b - a) * (r - 1));
00154                 q = (q - 1) * (r - 1) * (s - 1);
00155             } else {
00156                 // Linear interpolation.
00157                 s = fb / fa;
00158                 p = 2 * mid * s;
00159                 q = 1 - s;
00160             }
00161 
00162             // Adjust signs.
00163 
00164             if(p > 0)
00165                 q = -q;
00166 
00167             p = fabs(p);
00168 
00169             // Is interpolation acceptable?
00170 
00171             if(2 * p >= 3 * mid * q - fabs(tol * q) || p >= fabs(0.5 * e * q)) {
00172                 // Bisection.
00173                 e = d = mid;
00174             } else {
00175                 e = d;
00176                 d = p / q;
00177             }
00178         }
00179 
00180         // Complete step.
00181 
00182         a = b;
00183         fa = fb;
00184 
00185         if(fabs(d) > tol)
00186             b += d;
00187         else b += fabs(tol) * (mid >= 0 ? 1 : -1);
00188 
00189         fb = f.EvaluateAt(b);
00190 
00191         if(fb * (fc / fabs(fc)) > 0) {
00192             c = a;
00193             fc = fa;
00194             e = d = b - a;
00195         }
00196     }
00197 
00198     return b;
00199 }
00200 */

Generated on Tue May 11 01:31:49 2004 for toxic by doxygen 1.3.6