UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils::AlgoHNewton< Real > Class Template Reference

Class for solving \( f(x)=0 \) without the usew of derivative. More...

#include <Utils_AlgoHNewton.hh>

Public Member Functions

 AlgoHNewton ()=default
 
 ~AlgoHNewton ()=default
 
Real eval (Real a, Real b, AlgoHNewton_base_fun< Real > const *fun)
 
Real eval (Real a, Real b, Real amin, Real bmax, AlgoHNewton_base_fun< Real > const *fun)
 
void set_max_iterations (Integer mit)
 
Integer used_iter () const
 
Integer num_fun_eval () const
 
Integer num_fun_D_eval () const
 
Real tolerance () const
 
bool converged () const
 
Real a () const
 
Real b () const
 
Real fa () const
 
Real fb () const
 

Detailed Description

template<typename Real>
class Utils::AlgoHNewton< Real >

Class for solving \( f(x)=0 \) without the usew of derivative.

Usage simple example:

To use this class, first wrap your function in a derived class. For instance, for the function \( f(x) = x^2 - 2 \), you can define:

class Fun1 : public AlgoHNewton_base_fun<double> {
public:
double eval(double x) const override { return x*x - 2; }
double D (double x) const override { return 2*x; }
};
Abstract base class for defining mathematical functions used in the zero search algorithm.
Definition Utils_AlgoHNewton.hh:81
virtual Real eval(Real x) const =0
virtual Real D(Real x) const =0

Next, instantiate the function and the solver. Then, call the desired method to find the root:

HNewton<real_type> solver;
Fun1 f;
real_type a=-1, b=2;
real_type x_solution = solver.eval2(a,b,f);
Real b() const
Definition Utils_AlgoHNewton.hh:313
Real a() const
Definition Utils_AlgoHNewton.hh:312

If the method converges, x_solution will contain the computed solution.

Usage Detailed Example:

To create a custom function, derive from this class and implement the required methods. Here is an example for the function \( f(x) = x^2 - 2 \):

Step 1: including headers

To use the HNewton class, include the necessary headers:

#include "Utils_HNewton.hh"
#include "Utils_fmt.hh" // For formatted output

defining your function

Define the function for which you want to find the root. The function must take a single argument (the variable for which you're solving) and return a value.

For example, to find the root of \( \sin(x) - \frac{x}{2} = 0 \):

class Fun1 : public AlgoHNewton_base_fun<double> {
public:
double eval(double x) const override { return sin(x) - x / 2; }
double D (double x) const override { return cos(x) - 0.5; }
};
static Fun1 myFunction;

Calling the solver

To find a root, call the eval method with the initial guesses for the root (interval [a, b]). Here's an example of how to set up a program to find a root:

int
main() {
// Create an instance of the solver
HNewton<real_type> solver;
// Define the interval [a, b]
real_type a = 0.0; // lower bound
real_type b = 2.0; // upper bound
// Solve for the root
real_type root = solver.eval(a, b, myFunction);
// Print the results
cout << "Root found: " << root << endl;
cout << "f(root) = " << myFunction.eval(root) << endl;
return 0;
}

Analyzing results

After calling the eval method, you can check the number of iterations, number of function evaluations, and whether the algorithm converged:

cout << "Iterations: " << solver.used_iter() << endl;
cout << "Function Evaluations: " << solver.num_fun_eval() << endl;
cout << "Function Derivative Evaluations: " << solver.num_fun_D_eval() << endl;
cout << "Converged: " << (solver.converged() ? "Yes" : "No") << endl;

Constructor & Destructor Documentation

◆ AlgoHNewton()

template<typename Real>
Utils::AlgoHNewton< Real >::AlgoHNewton ( )
default

◆ ~AlgoHNewton()

template<typename Real>
Utils::AlgoHNewton< Real >::~AlgoHNewton ( )
default

Member Function Documentation

◆ a()

template<typename Real>
Real Utils::AlgoHNewton< Real >::a ( ) const
inline

◆ b()

template<typename Real>
Real Utils::AlgoHNewton< Real >::b ( ) const
inline

◆ converged()

template<typename Real>
bool Utils::AlgoHNewton< Real >::converged ( ) const
inline
Returns
true if the last computation was successfull

◆ eval() [1/2]

template<typename Real>
Real Utils::AlgoHNewton< Real >::eval ( Real a,
Real b,
AlgoHNewton_base_fun< Real > const * fun )
inline

Find the solution for a function wrapped in the class AlgoHNewton_base_fun<Real> starting from guess interval [a,b]

Parameters
alower bound search interval
bupper bound search interval
funthe pointer to base class AlgoHNewton_base_fun<Real> wrapping the user function

◆ eval() [2/2]

template<typename Real>
Real Utils::AlgoHNewton< Real >::eval ( Real a,
Real b,
Real amin,
Real bmax,
AlgoHNewton_base_fun< Real > const * fun )
inline

Find the solution for a function wrapped in the class AlgoHNewton_base_fun<Real> starting from guess interval [a,b]

Parameters
aguess interval lower bound
bguess interval upper bound
aminlower bound search interval
bmaxupper bound search interval
funthe pointer to base class AlgoHNewton_base_fun<Real> wrapping the user function

◆ fa()

template<typename Real>
Real Utils::AlgoHNewton< Real >::fa ( ) const
inline

◆ fb()

template<typename Real>
Real Utils::AlgoHNewton< Real >::fb ( ) const
inline

◆ num_fun_D_eval()

template<typename Real>
Integer Utils::AlgoHNewton< Real >::num_fun_D_eval ( ) const
inline
Returns
the number of evaluation used in the last computation

◆ num_fun_eval()

template<typename Real>
Integer Utils::AlgoHNewton< Real >::num_fun_eval ( ) const
inline
Returns
the number of evaluation used in the last computation

◆ set_max_iterations()

template<typename Real>
void Utils::AlgoHNewton< Real >::set_max_iterations ( Integer mit)

Fix the maximum number of iteration.

Parameters
mitthe maximum number of iteration

◆ tolerance()

template<typename Real>
Real Utils::AlgoHNewton< Real >::tolerance ( ) const
inline
Returns
the tolerance set for computation

◆ used_iter()

template<typename Real>
Integer Utils::AlgoHNewton< Real >::used_iter ( ) const
inline
Returns
the number of iterations used in the last computation

The documentation for this class was generated from the following files: