Code-Eli  0.3.6
bisection_method.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef eli_mutil_nls_bisection_method_hpp
14 #define eli_mutil_nls_bisection_method_hpp
15 
16 #include <cmath>
17 
18 #include "eli/code_eli.hpp"
19 
21 
22 namespace eli
23 {
24  namespace mutil
25  {
26  namespace nls
27  {
28  template<typename data__>
29  class bisection_method : public iterative_root_base<data__>
30  {
31  private:
32  data__ xmin, xmax;
33 
34  public:
35  bisection_method() : iterative_root_base<data__>(), xmin(0), xmax(1)
36  {
37  }
38 
40  : iterative_root_base<data__>(bm), xmin(bm.xmin), xmax(bm.xmax)
41  {
42  }
43 
45  {
46  }
47 
48  void set_low_bound(const data__ &xl)
49  {
50  xmin=xl;
51  }
52 
53  const data__ & get_low_bound() const
54  {
55  return xmin;
56  }
57 
58  void set_high_bound(const data__ &xh)
59  {
60  xmax=xh;
61  }
62 
63  const data__ & get_high_bound() const
64  {
65  return xmax;
66  }
67 
68  void set_bounds(const data__ &xl, const data__ &xh)
69  {
70  if (xh < xl)
71  {
72  assert(false);
73  return;
74  }
75 
76  set_low_bound(xl);
77  set_high_bound(xh);
78  }
79 
80  template<typename f__>
81  int find_root(data__ &root, const f__ &fun, const data__ &f0) const
82  {
83  data__ xmn(xmin), xmx(xmax), two(2);
84  data__ fmin, fmid, fmax, xmid, data_abs, data_abs2;
86 
87  // calculate the function evaluated at the minimum location
88  fmin=fun(xmn)-f0;
89  data_abs=std::abs(fmin);
90  if (this->test_converged(0, data_abs/f0, data_abs))
91  {
92  root=xmn;
93  return this->converged;
94  }
95 
96  // calculate the function evaluated at the maximum location
97  fmax=fun(xmx)-f0;
98  data_abs=std::abs(fmax);
99  if (this->test_converged(0, data_abs/f0, data_abs))
100  {
101  root=xmx;
102  return this->converged;
103  }
104 
105  count=0;
106  xmid=(xmx+xmn)/two;
107  fmid=fun(xmid)-f0;
108  data_abs=std::abs(xmx-xmn);
109  data_abs2=std::abs(fmid);
110  // this tests how close the min and max roots are to each other and tests how close the function evaluations are to zero
111  while (!this->test_converged(count, data_abs/xmid, data_abs) && !this->test_converged(count, data_abs2/f0, data_abs2))
112  {
113  // if middle point is opposite side of root as maximum point
114  if (fmid*fmax<0)
115  {
116  fmin=fmid;
117  xmn=xmid;
118  }
119  else if (fmid*fmin<0)
120  {
121  fmax=fmid;
122  xmx=xmid;
123  }
124  else
125  {
126  root=(xmn+xmx)/2;
127  return this->no_root_found;
128  }
129 
130  xmid=(xmx+xmn)/two;
131  fmid=fun(xmid)-f0;
132  data_abs=std::abs(xmx-xmn);
133  data_abs2=std::abs(fmid);
134  ++count;
135  }
136 
137  root=xmid;
138  if (this->max_iteration_reached(count))
139  {
140  return this->max_iteration; // could not converge
141  }
142 
143  return this->converged;
144  }
145  };
146  }
147  }
148 }
149 #endif
void set_bounds(const data__ &xl, const data__ &xh)
Definition: bisection_method.hpp:68
bisection_method()
Definition: bisection_method.hpp:35
Definition: math.hpp:20
data__ xmin
Definition: bisection_method.hpp:32
bool max_iteration_reached(const iteration_type &it) const
Definition: iterative_root_base.hpp:261
int find_root(data__ &root, const f__ &fun, const data__ &f0) const
Definition: bisection_method.hpp:81
bool test_converged(const iteration_type &it, const tolerance_type &relv, const tolerance_type &absv) const
Definition: iterative_root_base.hpp:256
static const int no_root_found
Definition: iterative_root_base.hpp:156
const data__ & get_high_bound() const
Definition: bisection_method.hpp:63
Definition: iterative_root_base.hpp:151
~bisection_method()
Definition: bisection_method.hpp:44
const data__ & get_low_bound() const
Definition: bisection_method.hpp:53
Definition: bisection_method.hpp:29
void set_high_bound(const data__ &xh)
Definition: bisection_method.hpp:58
static const int max_iteration
Definition: iterative_root_base.hpp:155
data__ xmax
Definition: bisection_method.hpp:32
max_iteration_type::data_type iteration_type
Definition: iterative_root_base.hpp:161
static const int converged
Definition: iterative_root_base.hpp:154
void set_low_bound(const data__ &xl)
Definition: bisection_method.hpp:48
bisection_method(const bisection_method< data__ > &bm)
Definition: bisection_method.hpp:39