/// \file NewtornRaphson.cpp
/// This is a demonstration of how to use NewtonRaphson.h
/// to compile: g++ -I. -lm NewtonRaphson.cpp
/// description: this test the algorithm in 3 situations:
/// 1) calculating df at bouth points, to find the average df, for each iteration, with no boundaries
/// 2) calculating d2f to get second order approximation at each iteration, with no boundaries
/// 3) calculating d2f to get second order approximation at each iteration, and a boundary solution
/***************************************************************************
* Copyright (C) 2009 by Clark Sims *
* http://AcumenSoftwareInc.com/WhoWeAre/Clark_Sims.html *
* ClarkSims@AcumenSoftwareInc.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "NewtonRaphson.h"
#include
/// \brief This is a functor which encapsulates the function: exp(x) - offset. The function variable is x. Offset is a hidden variable. This class is used to demonstrate the NewtonRaphsonSolve0< functor, real > template class, in the test file NewtonRaphson.cpp
class Newton_functor {
public:
/// the hidden variable for class Newton_functor, which makes this class a functor, as opposed to a group of function pointers
double _offset;
/// the creator for Newton_functor which assigns the hidden variable offset
Newton_functor( double offset) : _offset(offset) { }
/// the function to be solved
double f( double x) {
return exp( x) - _offset;
}
/// the derivative of the function to be solved
double f_prime( double x) {
return exp( x);
}
};
int main() {
Newton_functor sf( 5);
{
NewtonRaphsonSolve0< Newton_functor, double > foobar( 1.2, 1e-8, true, 100, sf, &Newton_functor::f, &Newton_functor::f_prime);
foobar.do_iteration( true);
foobar.set_d2f( &Newton_functor::f_prime);
foobar.do_iteration( true);
foobar.set_check_boundary( true);
foobar.set_max_x( 1.6);
foobar.do_iteration( true);
}
sf._offset = 0;
{
NewtonRaphsonSolve0< Newton_functor, double > foobar( 1.2, 1e-8, true, 100, sf, &Newton_functor::f, &Newton_functor::f_prime, 0, 5.0);
foobar.do_iteration( true);
}
return 0;
}