00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
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,
00061 1e-8,
00062 true,
00063 100,
00064 functor,
00065 &Newton_functor::f,
00066 &Newton_functor::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,
00089 1e-8,
00090 true,
00091 100,
00092 functor,
00093 &Newton_functor::f,
00094 &Newton_functor::df,
00095 0,
00096 5.0);
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