NewtonRaphsonSolve0< functor, real > Class Template Reference

#include <NewtonRaphson.h>

Collaboration diagram for NewtonRaphsonSolve0< functor, real >:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<class functor, class real>
class NewtonRaphsonSolve0< functor, real >

This template class is an encapsulation of the one dimensional NewtonRaphson iteration algorithm. It allows for calculating df twice at each iteration, or d2f, to increase the spead of convergance. It also can be used in situations with boundaries. The orignal source cand be viewed or downloaded here.
.

An object of class NewtonRaphsonSolve0 is created with a call to the constructor, NewtonRaphsonSolve0::NewtonRaphsonSolve0, which takes arguments for the starting point, X0, the convergance stopping point, Epsilon, and a functor f, and a functor df, which is the derivative of f.
The function do_iteration, attempts to find the value of x, _x_converge, which solves the equation, (_F.*_f(_x_converge) - _f_solve) < _epsilon.
The second derivative d2f can optionally be supplied. If check_boundary is set to true, then boundary solutions can be solved also. The member function do_iteration, starts the iterative process.
The file NewtonRaphson.cpp contains 3 examples of how this template class can be used. The source code for NewtonRaphson.cpp can be downloaded here.

Definition at line 41 of file NewtonRaphson.h.

Public Member Functions

bool get_boundary_solution () const
 returns _boundary_solution
void set_d2f (real(functor::*d2f)(real))
 used to set the second order differential
void set_check_boundary (bool check_boundary)
 used to turn boundary checking on or off
void set_min_x (real min_x)
 used to set the minimum of the solution set
void set_max_x (real max_x)
 used to set the maximum of the solution set
real get_x_converge () const
 used to retrieve the convergance value
real get_f_converge () const
 used to retrieve the final value of f
functor & get_functor ()
 this returns a reference to the parent object with the hidden variables
 NewtonRaphsonSolve0 (real X0, real Epsilon, bool Twice_df, unsigned int max_iter, functor &F, real(functor::*f)(real), real(functor::*df)(real), real(functor::*d2f)(real)=NULL, real fsolve=0, bool check_boundary=false, real min_x=0, real max_x=0)
void do_iteration (ostream *output)
 This function does the calculations.

Protected Member Functions

int check_boundary (real &x, real df_dx)
 this returns the number of times the bounaries have been hit. If boundaries are hit more than twice, then the loop is stoped.

Protected Attributes

int _max_iter
 The maximum number of iterations of the routine. See the documentation for do_iteration, to see how this value affects the algorithm.
real _x0
 This is the starting point of the iteration. If _check_boundary is true, then _x0 must be between _min_x and _max_x.
real _epsilon
 the convergence parameter. The calculations stop when F(x) < epsilon.
real _fsolve
 the value of f to be solved. This is usually 0 in the academic literature.
bool _twice_df
 if true then the value of the derivative is calculated twice, to calculate the average value of df, over the interval dx. This is useful, if df is easier to calculate than f. For example dprice/dsigam, can be aproximated ver
real(functor::* _f )(real)
 this is the function whose 0 is being solved
real(functor::* _df )(real)
 this is the derivative
real(functor::* _d2f )(real)
 this is the second derivative. If set to null, then it isn't used
functor & _F
 this is the object used to calculate the functions
bool _check_boundary
 if true, then the search for the zero, has boundaries
real _min_x
 this is the minimum value of the acceptible range
real _max_x
 this is the maximum value of the acceptible range
real _x_converge
 the value of x such that f(x) = 0 +/- epsilon
real _f_converge
 the last value of f
vector< real > _x
 the sequence of x's
vector< real > _F_x
 the sequence of f_x's
vector< real > _abs_F_x
 the sequence of abs(f_x)'s
vector< real > _dF_x
 the sequence of df_x's
bool _converged
 a boolean which is true, if convergance occurs
bool _diverged
 a boolean which is true if the sequence diverged
bool _boundary_solution
 true if the final x is on the bounday
int _number_boundary_hits
 the number of times a boundary has been hit
bool _lower_boundary_hit
 the number of times the lower boundary has been hit
bool _upper_boundary_hit
 the number of times the uper boundary has been hit
bool _boundary_hit
 true if the last iteration hit the boundary


Constructor & Destructor Documentation

template<class functor, class real>
NewtonRaphsonSolve0< functor, real >::NewtonRaphsonSolve0 ( real  X0,
real  Epsilon,
bool  Twice_df,
unsigned int  max_iter,
functor &  F,
real(functor::*)(real)  f,
real(functor::*)(real)  df,
real(functor::*)(real)  d2f = NULL,
real  fsolve = 0,
bool  check_boundary = false,
real  min_x = 0,
real  max_x = 0 
) [inline]

This constructor sets everything needed by the algorithm.

Parameters:
[in] X0 This is the starting point of the search. It is stored as the data member, _x0.
[in] Epsilon The is used to define the end point of the iterative procedure, do_iteration. The loop will terminate when the abs(F(xi) - fsolve)) is less than Epsilon. It is stored as the data member, _epsilon.
[in] Twice_df If Twice_df then the derivative of F, at point i, is approximated as (F(x[i])+f(x[i-1])*.5 which approximates the derivative of F over the interval x[i] to x[i-1]. It is stored as the data member, _F.
[in] max_iter The maximum number of iterations before do_iteration, will terminate. It is stored as the data member _max_iter.
[in] F This reference to a functor object serves as the object for calling the member function pointers f, df and d2f. It is stored as the data member _F.
[in] f This pointer to a member function is used to calculate the function to solved. It is stored as the data member _f.
[in] df This pointer to a member function is used to calculate the derivative of the function to solved. It is stored as the data member _df.
[in] d2f This pointer to a member function is used to calculate the second derivative of the function to solved. It is stored as the data member _d2f.
[in] fsolve This is the value of f to be solved. It's default value is 0. It is stored as the data member _fsolve.
[in] check_boundary If true then the algorithm will check constrain the search so that it only stays within min_x and max_x. If the search goes outside of the range then the solution is set to the boundary value. It is stored as the data member _check_boundary.

Definition at line 185 of file NewtonRaphson.h.

00198     :  _x0( X0), _epsilon( Epsilon), _twice_df( Twice_df), _max_iter(max_iter), _F(F), _f(f), _df(df), _d2f(d2f), _fsolve(fsolve), _check_boundary(check_boundary), _min_x(min_x), _max_x(max_x), 
00199     _x_converge(0), _f_converge(0), 
00200     _x(max_iter), _F_x(max_iter), _abs_F_x(max_iter), _dF_x(max_iter), _converged(false), _diverged(false), 
00201     _lower_boundary_hit(false), _upper_boundary_hit(false), _boundary_hit(false)
00202   {
00203 
00204   }


The documentation for this class was generated from the following file:
Generated on Sat Sep 12 20:10:46 2009 for NewtonRaphson by  doxygen 1.5.1