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 #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,
00060 1e-8,
00061 true,
00062 100,
00063 functor,
00064 &Newton_functor::f,
00065 &Newton_functor::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,
00088 1e-8,
00089 true,
00090 100,
00091 functor,
00092 &Newton_functor::f,
00093 &Newton_functor::df,
00094 0,
00095 5.0);
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