NewtonRaphson.cpp

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

Generated on Sat Sep 12 20:10:46 2009 for NewtonRaphson by  doxygen 1.5.1