NewtonRaphson.cpp

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 // to compile:  g++ -I. -lm NewtonRaphson.cpp<br />
00009 
00010 /***************************************************************************
00011  *   Copyright (C) 2009 by Clark Sims                                      *
00012  *   http://AcumenSoftwareInc.com/WhoWeAre/Clark_Sims.html                 *
00013  *   ClarkSims@AcumenSoftwareInc.com                                       *
00014  *                                                                         *
00015  *   This program is free software; you can redistribute it and/or modify  *
00016  *   it under the terms of the GNU General Public License as published by  *
00017  *   the Free Software Foundation; either version 2 of the License, or     *
00018  *   (at your option) any later version.                                   *
00019  *                                                                         *
00020  *   This program is distributed in the hope that it will be useful,       *
00021  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00022  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00023  *   GNU General Public License for more details.                          *
00024  *                                                                         *
00025  *   You should have received a copy of the GNU General Public License     *
00026  *   along with this program; if not, write to the                         *
00027  *   Free Software Foundation, Inc.,                                       *
00028  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00029  ***************************************************************************/
00030 
00031 #include "NewtonRaphson.h"
00032 #include <math.h>
00033 
00035 class Newton_functor {
00036 public:
00038   double _offset;
00039   
00041   Newton_functor( double offset) : _offset(offset) { }
00042 
00044   double f( double x) {
00045     return exp( x) - _offset;
00046   }
00047   
00049   double df( double x) {
00050     return exp( x);
00051   }
00052 };
00053 
00054 int main() {
00055   Newton_functor functor( 5);
00056 
00057   {
00058     NewtonRaphsonSolve0< Newton_functor, double > 
00059       newton( 
00060              1.2,                  // X0
00061              1e-8,                 // Epsilon
00062              true,                 // Twice_df
00063              100,                  // max_iter
00064              functor,              // F
00065              &Newton_functor::f,   // f
00066              &Newton_functor::df); // df
00067 
00068     cout << "running iteration with f and df defined, over all real numbers" << endl;
00069     newton.do_iteration( &cout);
00070 
00071     cout << "running iteration with f, df and d2f defined, over all real numbers" << endl;
00072     newton.set_d2f( &Newton_functor::df);
00073     newton.do_iteration( &cout);
00074 
00075     cout << "running iteration with f, df and d2f defined, over the interval [1.6, infinity)" << endl;
00076     newton.set_check_boundary( true);
00077     newton.set_max_x( 1.6);
00078     newton.do_iteration( &cout);
00079   }
00080 
00081   cout << "running a numerically equivalent example, where functor._offset = 0, and newton._fsolve = 5.0" 
00082        << endl;
00083 
00084   functor._offset = 0;
00085   {
00086     NewtonRaphsonSolve0< Newton_functor, double > 
00087       newton( 
00088              1.2,                  // X0
00089              1e-8,                 // Epsilon
00090              true,                 // Twice_df
00091              100,                  // max_iter
00092              functor,              // F
00093              &Newton_functor::f,   // f
00094              &Newton_functor::df,  // df
00095              0,                    // d2f
00096              5.0);                 // Fsolve
00097 
00098     cout << "running iteration with f and df defined, over all real numbers" << endl;
00099     newton.do_iteration( &cout);
00100 
00101     cout << "running iteration with f, df and d2f defined, over all real numbers" << endl;
00102     newton.set_d2f( &Newton_functor::df);
00103     newton.do_iteration( &cout);
00104 
00105     cout << "running iteration with f, df and d2f defined, over the interval [1.6, infinity)" << endl;
00106     newton.set_check_boundary( true);
00107     newton.set_max_x( 1.6);
00108     newton.do_iteration( &cout);
00109   }
00110 
00111   return 0;
00112 }
00113 

Generated on Fri Jan 7 13:59:02 2011 for NewtonRaphson by  doxygen 1.5.1