Code-Eli  0.3.6
newton_raphson_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_newton_raphson_method_hpp
14 #define eli_mutil_nls_newton_raphson_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__>
28  {
29  public:
30  typedef data__ data_type;
31 
32  static const int hit_constraint = 101;
33 
34  public:
36  {
37  }
38 
40  : iterative_root_base<data_type>(nrm), x0(nrm.x0)
41  {
42  }
43 
45  {
46  }
47 
48  void set_initial_guess(const data_type &xg)
49  {
50  x0=xg;
51  }
52 
53  const data_type & get_initial_guess() const
54  {
55  return x0;
56  }
57 
58  template<typename f__, typename g__>
59  int find_root(data_type &root, const f__ &fun, const g__ &fprime, const data_type &f0) const
60  {
61  data_type x(x0), fx(fun(x0)), fpx(fprime(x0)), eval, eval_abs, dx(1);
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;
71  }
72 
73  count=0;
74  while (!this->test_converged(count, eval_abs/f0, eval_abs) && (std::abs(dx)>0))
75  {
76  if (fpx==0)
78 
79  dx=calculate_delta_factor(x, -eval/fpx);
80  x+=dx;
81  fx=fun(x);
82  fpx=fprime(x);
83  eval=fx-f0;
84  eval_abs=std::abs(eval);
85 
86  ++count;
87  }
88 
89  root=x;
90  if (this->max_iteration_reached(count))
91  return this->max_iteration; // could not converge
92  if (dx==0)
93  return this->hit_constraint; // constraints limited convergence
94 
95  return this->converged;
96  }
97 
98  private:
99  virtual data_type calculate_delta_factor(const data_type &, const data_type &dx) const {return dx;}
100 
101  private:
102  data_type x0;
103  };
104  }
105  }
106 }
107 #endif
Definition: math.hpp:20
bool max_iteration_reached(const iteration_type &it) const
Definition: iterative_root_base.hpp:261
Definition: newton_raphson_method.hpp:27
data_type x0
Definition: newton_raphson_method.hpp:102
newton_raphson_method(const newton_raphson_method< data_type > &nrm)
Definition: newton_raphson_method.hpp:39
~newton_raphson_method()
Definition: newton_raphson_method.hpp:44
bool test_converged(const iteration_type &it, const tolerance_type &relv, const tolerance_type &absv) const
Definition: iterative_root_base.hpp:256
newton_raphson_method()
Definition: newton_raphson_method.hpp:35
Definition: iterative_root_base.hpp:151
const data_type & get_initial_guess() const
Definition: newton_raphson_method.hpp:53
data__ data_type
Definition: newton_raphson_method.hpp:30
virtual data_type calculate_delta_factor(const data_type &, const data_type &dx) const
Definition: newton_raphson_method.hpp:99
static const int hit_constraint
Definition: newton_raphson_method.hpp:32
int find_root(data_type &root, const f__ &fun, const g__ &fprime, const data_type &f0) const
Definition: newton_raphson_method.hpp:59
static const int max_iteration
Definition: iterative_root_base.hpp:155
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_initial_guess(const data_type &xg)
Definition: newton_raphson_method.hpp:48