src/main/cpp/numerics4cpp/root/falseposition.h

00001 /*
00002  * Copyright (c) 2005, DoodleProject
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 
00009  * Redistributions of source code must retain the above copyright
00010  * notice, this list of conditions and the following disclaimer.
00011  * 
00012  * Redistributions in binary form must reproduce the above copyright
00013  * notice, this list of conditions and the following disclaimer in
00014  * the documentation and/or other materials provided with the
00015  * distribution.
00016  * 
00017  * Neither the name of DoodleProject nor the names of its contributors
00018  * may be used to endorse or promote products derived from this
00019  * software without specific prior written permission.
00020  * 
00021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
00022  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
00023  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00024  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
00026  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00028  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00030  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00031  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00032  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00033  * SUCH DAMAGE.
00034  */
00035 
00036 #ifndef _NUMERICS4CPP_ROOT_FALSEPOSITION_H_
00037 #define _NUMERICS4CPP_ROOT_FALSEPOSITION_H_
00038 
00039 #include <cmath>
00040 #include <limits>
00041 #include "../iterativemethod.h"
00042 #include "../math.h"
00043 
00044 NUM_NAMESPACE_BEGIN
00045 
00072 template<class Function>
00073 class false_position_root_finder : public iterative_method {
00074 
00075 public:
00082         false_position_root_finder(Function& f, unsigned int iterations = 100,
00083         double relative_error = 1.0e-15)
00084         : iterative_method(iterations, relative_error), _function(f)
00085     {
00086         }
00087         
00096         double find_root(double min, double max) {
00097         double x0 = min;
00098         double x1 = max;
00099         double f0 = _function(x0);
00100         double f1 = _function(x1);
00101         double f;
00102         unsigned int n = 0;
00103         double x;
00104         double delta;
00105         double error;
00106 
00107         if (f0 > 0.0) {
00108             x = x0;
00109             x0 = x1;
00110             x1 = x;
00111             
00112             f = f0;
00113             f0 = f1;
00114             f1 = f;
00115         }
00116         
00117         do {
00118             delta = f1 * (x1 - x0) / (f1 - f0);
00119             x = x1 - delta;
00120             f = _function(x);
00121             error = std::max(std::fabs(f), std::fabs(delta / x1));
00122             if (f < 0.0) {
00123                 x0 = x;
00124                 f0 = f;
00125             } else {
00126                 x1 = x;
00127                 f1 = f;
00128             }
00129             ++n;
00130         } while (n < maximum_iterations() && error > maximum_relative_error());
00131             
00132         if (n >= maximum_iterations()) {
00133             throw convergence_exception(
00134                 "False position method failed to converge.");
00135         }
00136 
00137         return x;
00138         }
00139                         
00140 private:
00142         Function& _function;
00143 };
00144 
00145 NUM_NAMESPACE_END
00146 
00147 #endif

Generated on Wed Nov 21 22:22:15 2007 for numerics4c++ by  doxygen 1.5.3