Code-Eli  0.3.6
secant_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_secant_method_hpp
14 #define eli_mutil_nls_secant_method_hpp
15 
16 #include "eli/code_eli.hpp"
17 
19 
20 namespace eli
21 {
22  namespace mutil
23  {
24  namespace nls
25  {
26  template<typename data__>
27  class secant_method : public iterative_root_base<data__>
28  {
29  private:
30  data__ x1, x2;
31 
32  public:
33  secant_method() : iterative_root_base<data__>(), x1(0), x2(1)
34  {
35  }
36 
38  : iterative_root_base<data__>(sm), x1(sm.x1), x2(sm.x2)
39  {
40  }
41 
43  {
44  }
45 
46  void set_initial_guesses(const data__ &xg1, const data__ &xg2)
47  {
48  x1=xg1;
49  x2=xg2;
50  }
51 
52  void get_initial_guesses(data__ &xg1, data__ &xg2) const
53  {
54  xg1=x1;
55  xg2=x2;
56  }
57 
58  template<typename f__>
59  int find_root(data__ &root, const f__ &fun, const data__ &f0) const
60  {
61  data__ x(x1), fx(fun(x1)), xm1(x2), fxm1(x2), eval, eval_abs, tmp;
63 
64  // calculate the function evaluated at the initial location
65  eval=fx-f0;
66  eval_abs=std::abs(eval);
67  if (this->test_converged(0, eval_abs/f0, eval_abs))
68  {
69  root=x;
70  return this->converged;
71  }
72 
73  count=0;
74  while (!this->test_converged(count, eval_abs/f0, eval_abs))
75  {
76  if (fx==fxm1)
77  return this->no_root_found;
78 
79  tmp=x;
80  x-=eval*(x-xm1)/(fx-fxm1);
81  xm1=tmp;
82  fxm1=fx;
83  fx=fun(x);
84  eval=fx-f0;
85  eval_abs=std::abs(eval);
86 
87  ++count;
88  }
89 
90  root=x;
91  if (this->max_iteration_reached(count))
92  return this->max_iteration; // could not converge
93 
94  return this->converged;
95  }
96  };
97  }
98  }
99 }
100 #endif
Definition: math.hpp:20
bool max_iteration_reached(const iteration_type &it) const
Definition: iterative_root_base.hpp:261
Definition: secant_method.hpp:27
int find_root(data__ &root, const f__ &fun, const data__ &f0) const
Definition: secant_method.hpp:59
data__ x2
Definition: secant_method.hpp:30
bool test_converged(const iteration_type &it, const tolerance_type &relv, const tolerance_type &absv) const
Definition: iterative_root_base.hpp:256
void get_initial_guesses(data__ &xg1, data__ &xg2) const
Definition: secant_method.hpp:52
~secant_method()
Definition: secant_method.hpp:42
static const int no_root_found
Definition: iterative_root_base.hpp:156
Definition: iterative_root_base.hpp:151
void set_initial_guesses(const data__ &xg1, const data__ &xg2)
Definition: secant_method.hpp:46
data__ x1
Definition: secant_method.hpp:30
static const int max_iteration
Definition: iterative_root_base.hpp:155
secant_method()
Definition: secant_method.hpp:33
max_iteration_type::data_type iteration_type
Definition: iterative_root_base.hpp:161
static const int converged
Definition: iterative_root_base.hpp:154
secant_method(const secant_method< data__ > &sm)
Definition: secant_method.hpp:37