Follow this link to skip to the main content

1d_function_solver.h

Go to the documentation of this file.
00001 // -*-c++-*-
00002 //---------------------------< /-/ CLARAty /-/ >------------------------------
00003 /**
00004  * @file  1d_function_solver.h
00005  *
00006  * Provides algorithms to (a) find the roots for a single variable
00007  * math function (i.e. zero crossings), and (b) the local
00008  * minimum/maximum of a single variable function over a specified
00009  * interval. (a) roots is implemented using a Newton Raphson
00010  * minimization iterative technique.
00011  *
00012  * <br>@b Designer(s):  Hari Nayar
00013  * <br>@b Author(s):    Issa A.D. Nesnas
00014  * <br>@b Date:         May 15, 2001
00015  *
00016  * <b>Software License:</b><br>
00017  * <code>  http://claraty.jpl.nasa.gov/license/open_src/  or
00018  *         file: license/open_src.txt  </code>
00019  *
00020  * &copy; 2006, Jet Propulsion Laboratory, California Institute of Technology<br>
00021  *
00022  * $Revision: 1.5 $
00023  */
00024 //-----------------------------------------------------------------------------
00025 
00026 #ifndef  N_1D_FUNCTION_SOLVER_H
00027 #define  N_1D_FUNCTION_SOLVER_H
00028 
00029 #include "claraty/share.h"
00030 #include "claraty/function.h"
00031 
00032 namespace claraty {
00033 
00034 /**
00035  * @ingroup math_algorithms
00036  *
00037  */
00038 //@{
00039 //@}
00040 
00041 //template <double, double> class Function;
00042 
00043 class N_1D_Function_Solver {
00044 public:
00045   enum { SOLVED, 
00046          MAX_ITERATIONS_EXCEEDED,
00047          CONTINUE_ITERATION };
00048   
00049   N_1D_Function_Solver() {};
00050   N_1D_Function_Solver(double  (*function)(double x),
00051                         double  guess,
00052                         int     max_iterations,
00053                         double  tolerance,
00054                         double  perturbation,
00055                         double  step_size = 0.5);
00056   
00057   N_1D_Function_Solver(Function<> * function,
00058                        double       guess,
00059                        int          max_iterations,
00060                        double       tolerance,
00061                        double       perturbation,
00062                        double       step_size = 0.5);
00063   
00064   ~N_1D_Function_Solver() {};
00065 
00066   void set_function(double (*function)(double x)) {_function2 = function;}
00067   void set_function(Function<> * function)        {_function1 = function;}
00068   void set_initial_guess(double guess)            {_guess = guess;}
00069   void set_maximum_iterations(int max_iterations) {_max_iterations = max_iterations;}
00070   void set_solution_tolerance(double tolerance)   {_error_tolerance = tolerance;};
00071   void set_step_size(double step_size)            {_step_size = step_size;};
00072   void set_perturbation(double perturbation)      {_perturbation = perturbation;};
00073 
00074   int find_root(double& result);
00075   int minimize (double &x0, double &y0, 
00076                 double &x1, double &y1,
00077                 double &x2, double &y2, long n, double &result);
00078 
00079 private:
00080   Function<> * _function1;
00081   double    (* _function2)(double variable);
00082 
00083   int        _max_iterations;
00084   double     _error_tolerance;
00085   double     _step_size;
00086   double     _guess;
00087   double     _perturbation;
00088 
00089   double     _function(double x);
00090   void       _bisect  (double &x0, double &y0, 
00091                        double &x2, double &y2, 
00092                        double &x4, double &y4);
00093   
00094 
00095 };
00096 
00097 
00098 //-----------------------------------------------------------------------------
00099 
00100 inline
00101 double N_1D_Function_Solver::
00102 _function(double x) 
00103 {
00104   if (_function1)
00105     return (*_function1)(x);
00106   else
00107     return (*_function2)(x);
00108 }
00109 //-----------------------------------------------------------------------------
00110 
00111 } // namespace claraty
00112 
00113 #endif // N_1D_FUNCTION_SOLVER_H