bracketingRootFinder.hpp
Go to the documentation of this file.
1 #ifndef JULIAN_BRACKETINGROOTFIDNER_HPP
2 #define JULIAN_BRACKETINGROOTFIDNER_HPP
3 
4 #include <gsl/gsl_errno.h>
5 #include <gsl/gsl_math.h>
6 #include <gsl/gsl_roots.h>
8 
9 namespace julian {
10 
41  FALSEPOS,
43  };
44 
63  template<typename T>
64  double bracketingRootFinder(T f, double x_lo, double x_hi, BracketingRootFinder type, double precision = 1e-12, int max_iter = 100) {
66  gsl_function *gsl_f = static_cast<gsl_function*>(&pF);
67  double root {0.0};
68 
69  int status;
70  int iter = 0;
71 
72  const gsl_root_fsolver_type *type_of_solver;
73  gsl_root_fsolver *s;
74 
75  switch (type){
76  case BracketingRootFinder::BISECTION: type_of_solver = gsl_root_fsolver_bisection; break;
77  case BracketingRootFinder::FALSEPOS: type_of_solver = gsl_root_fsolver_falsepos; break;
78  case BracketingRootFinder::BRENT_DEKKER: type_of_solver = gsl_root_fsolver_brent; break;
79  }
80 
81  s = gsl_root_fsolver_alloc (type_of_solver);
82  gsl_root_fsolver_set (s, gsl_f, x_lo, x_hi);
83 
84  do {
85  iter++;
86  status = gsl_root_fsolver_iterate (s);
87  root = gsl_root_fsolver_root (s);
88  x_lo = gsl_root_fsolver_x_lower (s);
89  x_hi = gsl_root_fsolver_x_upper (s);
90  status = gsl_root_test_interval (x_lo, x_hi, 0, precision);
91  } while (status == GSL_CONTINUE && iter < max_iter);
92 
93  gsl_root_fsolver_free (s);
94  return root;
95  }
96 } // namespace julian
97 #endif
98 
File contains adapter of GSL Function.
Definition: cadHoliday.cpp:3
Class implements adapter for gsl_function.
Definition: GslFunctionAdapter.hpp:25
BracketingRootFinder
Types of bracketing algorithms.
Definition: bracketingRootFinder.hpp:40
double bracketingRootFinder(T f, double x_lo, double x_hi, BracketingRootFinder type, double precision=1e-12, int max_iter=100)
Function finds roots using bracketing algorithms.
Definition: bracketingRootFinder.hpp:64
Method of False Position.