1d_function_solver.cc
Go to the documentation of this file.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 #include "claraty/1d_function_solver.h"
00027 #include <iostream>
00028 #include <math.h>
00029
00030 using namespace std;
00031
00032 namespace claraty {
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 N_1D_Function_Solver::
00047 N_1D_Function_Solver(double (*function)(double x),
00048 double guess,
00049 int max_iterations,
00050 double tolerance,
00051 double perturbation,
00052 double step_size)
00053 :
00054 _function1(NULL)
00055 {
00056 set_function(function);
00057 set_initial_guess(guess);
00058 set_maximum_iterations(max_iterations);
00059 set_solution_tolerance(tolerance);
00060 set_perturbation(perturbation);
00061 set_step_size(step_size);
00062 }
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 N_1D_Function_Solver::
00076 N_1D_Function_Solver(Function<> * function,
00077 double guess,
00078 int max_iterations,
00079 double tolerance,
00080 double perturbation,
00081 double step_size)
00082 :
00083 _function2(NULL)
00084 {
00085 set_function(function);
00086 set_initial_guess(guess);
00087 set_maximum_iterations(max_iterations);
00088 set_solution_tolerance(tolerance);
00089 set_perturbation(perturbation);
00090 set_step_size(step_size);
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100 int N_1D_Function_Solver::
00101 find_root(double& result)
00102 {
00103 int iterations = 0;
00104 bool terminate = false;
00105 int return_code = CONTINUE_ITERATION;
00106 double current_value, next_value=0, delta_value;
00107 double derivative;
00108
00109 while (!terminate)
00110 {
00111
00112 current_value = _function(_guess);
00113
00114
00115
00116
00117 if (fabs(current_value) < _error_tolerance)
00118 {
00119 terminate = true;
00120 return_code = SOLVED;
00121 }
00122 else
00123 {
00124
00125
00126 next_value = _function(_guess+_perturbation);
00127 delta_value = next_value - current_value;
00128
00129
00130 derivative = delta_value/_perturbation;
00131
00132
00133 _guess -= current_value / derivative;
00134
00135
00136 iterations++;
00137
00138
00139 if (iterations > _max_iterations)
00140 {
00141 terminate = true;
00142 return_code = MAX_ITERATIONS_EXCEEDED;
00143 }
00144 }
00145 #ifdef DEBUG
00146 cout << "Iteration #: " << iterations
00147 << " Current guess = " << _guess
00148 << " Current value = " << current_value
00149 << " Next value = " << next_value;
00150 << endl;
00151 #endif // DEBUG
00152 }
00153 result = _guess;
00154 return return_code;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 void N_1D_Function_Solver::
00169 _bisect(double &x0, double &y0,
00170 double &x2, double &y2,
00171 double &x4, double &y4)
00172 {
00173
00174 double x1 = ( x0 + x2 )/2;
00175 double y1 = _function(x1);
00176
00177
00178 if(y1 < y2)
00179 {
00180 x4 = x2; y4 = y2; x2 = x1; y2 = y1;
00181 return;
00182 }
00183
00184
00185 if(y1 > y2)
00186 {
00187 x0 = x1; y0 = y1;
00188 }
00189
00190
00191 double x3 = ( x2 + x4 )/2;
00192 double y3 = _function(x3);
00193
00194
00195 if(y3 < y2)
00196 {
00197 x0 = x2; y0 = y2; x2 = x3; y2 = y3;
00198 return;
00199 }
00200
00201
00202 if(y3 > y2)
00203 {
00204 x4 = x3; y4 = y3;
00205 }
00206
00207
00208 if(y1 == y2)
00209 {
00210 x2 = x1; y2 = y1;
00211 }
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 int N_1D_Function_Solver::
00231 minimize(double &x0, double &y0,
00232 double &x1, double &y1,
00233 double &x2, double &y2,
00234 long n, double &result)
00235 {
00236 for(long i=0; i<n; i++) _bisect(x0, y0, x1, y1, x2, y2);
00237
00238 result = x1;
00239 return SOLVED;
00240 }
00241
00242 }