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

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

#include <Utils_AlgoBracket.hh>

Public Member Functions

 AlgoBracket ()=default
 
 ~AlgoBracket ()=default
 
 AlgoBracket (Real tol_x, Real tol_f)
 
Real eval (Real a, Real b, Bracket_base_fun< Real > *fun)
 
Real eval (Real a, Real b, Real amin, Real bmax, Bracket_base_fun< Real > *fun)
 
template<typename PFUN>
Real eval2 (Real a, Real b, PFUN pfun)
 
template<typename PFUN>
Real eval2 (Real a, Real b, Real amin, Real bmax, PFUN pfun)
 
template<typename PFUN>
Real eval3 (Real a, Real b, Real fa, Real fb, PFUN pfun)
 
void set_max_iterations (Integer mit)
 
void set_max_fun_evaluation (Integer mfev)
 
Integer used_iter () const
 
Integer num_fun_eval () const
 
Real tolerance_x () const
 
Real tolerance_f () const
 
bool converged () const
 
Real a () const
 
Real b () const
 
Real fa () const
 
Real fb () const
 
string algo () const
 
void select (unsigned i_algo)
 

Detailed Description

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

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

Note
The used algorithm is described in: I.F.D. Oliveira, R.H.C. Takahashi, An Enhancement of the Bisection Method Average Performance Preserving Minmax Optimality, ACM Transactions on Mathematical Software, vol 47, N.1, 2020

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 Bracket_base_fun<double> {
public:
double eval(double x) const override { return x*x - 2; }
};
Abstract base class for defining mathematical functions used in the Bracket search algorithm.
Definition Utils_AlgoBracket.hh:83
virtual Real eval(Real x) const =0

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

Fun1 f;
real_type a=-1,b=2;
real_type x_solution = solver.eval2(a,b,f);
Real eval2(Real a, Real b, PFUN pfun)
Definition Utils_AlgoBracket.hh:358
Real a() const
Definition Utils_AlgoBracket.hh:443
Real b() const
Definition Utils_AlgoBracket.hh:444
AlgoBracket()=default

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 Bracket class, include the necessary headers:

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

Step 2: 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 \):

static real_type myFunction(real_type x) {
return sin(x) - x / 2;
}

Step 3: Creating solver instance

Create an instance of the Bracket class to solve your function. The class provides methods to evaluate functions and find roots.

Step 4: 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
// 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.eval2(a, b, myFunction);
// Print the results
cout << "Root found: " << root << endl;
cout << "f(root) = " << myFunction(root) << endl;
return 0;
}

Step 5: Using lambda functions

You can also use lambda functions to define your function inline, which can simplify your code. Here's how to modify the previous example:

int
main() {
// Create an instance of the solver
// Define the interval [a, b]
real_type a = 0.0; // lower bound
real_type b = 2.0; // upper bound
// Solve for the root using a lambda function
real_type root = solver.eval2(a, b, [](real_type x) { return sin(x) - x / 2; });
// Print the results
cout << "Root found: " << root << endl;
cout << "f(root) = " << sin(root) - root / 2 << endl;
return 0;
}

Step 6: Advanced usage with function parameters

If your function requires additional parameters, you can wrap it in a lambda or use std::bind. Here's an example using std::bind:

#include <functional>
static real_type myParameterizedFunction(real_type x, real_type a) {
return a * x * exp(-x);
}
int
main() {
// Create an instance of the solver
// Define the interval [a, b]
real_type a = 0.0;
real_type b = 5.0;
real_type parameter = -1.0;
// Solve for the root using std::bind
real_type root = solver.eval2(a, b, std::bind(myParameterizedFunction, std::placeholders::_1, parameter));
// Print the results
cout << "Root found: " << root << endl;
cout << "f(root) = " << myParameterizedFunction(root, parameter) << endl;
return 0;
}
Class for solving without the usew of derivative.
Definition Utils_AlgoBracket.hh:274

Step 7: Analyzing results

After calling the eval2 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 << "Converged: " << (solver.converged() ? "Yes" : "No") << endl;
Integer num_fun_eval() const
Definition Utils_AlgoBracket.hh:426
bool converged() const
Definition Utils_AlgoBracket.hh:441
Integer used_iter() const
Definition Utils_AlgoBracket.hh:421

Constructor & Destructor Documentation

◆ AlgoBracket() [1/2]

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

◆ ~AlgoBracket()

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

◆ AlgoBracket() [2/2]

template<typename Real>
Utils::AlgoBracket< Real >::AlgoBracket ( Real tol_x,
Real tol_f )
inlineexplicit

Member Function Documentation

◆ a()

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

◆ algo()

template<typename Real>
string Utils::AlgoBracket< Real >::algo ( ) const
inline

◆ b()

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

◆ converged()

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

◆ eval() [1/2]

template<typename Real>
Real Utils::AlgoBracket< Real >::eval ( Real a,
Real b,
Bracket_base_fun< Real > * fun )
inline

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

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

◆ eval() [2/2]

template<typename Real>
Real Utils::AlgoBracket< Real >::eval ( Real a,
Real b,
Real amin,
Real bmax,
Bracket_base_fun< Real > * fun )
inline

Find the solution for a function wrapped in the class Bracket_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 Bracket_base_fun<Real> wrapping the user function

◆ eval2() [1/2]

template<typename Real>
template<typename PFUN>
Real Utils::AlgoBracket< Real >::eval2 ( Real a,
Real b,
PFUN pfun )
inline

Find the solution for a function stored in pfun starting from guess interval [a,b]

Parameters
alower bound search interval
bupper bound search interval
pfunobject storing the function

◆ eval2() [2/2]

template<typename Real>
template<typename PFUN>
Real Utils::AlgoBracket< Real >::eval2 ( Real a,
Real b,
Real amin,
Real bmax,
PFUN pfun )
inline

Find the solution for a function stored in pfun starting from guess interval [a,b]

Parameters
aguess interval lower bound
bguess interval upper bound
aminlower bound search interval
bmaxupper bound search interval
pfunobject storing the function

◆ eval3()

template<typename Real>
template<typename PFUN>
Real Utils::AlgoBracket< Real >::eval3 ( Real a,
Real b,
Real fa,
Real fb,
PFUN pfun )
inline

Find the solution for a function wrapped into pfun starting from guess interval [a,b]

Parameters
alower bound search interval
bupper bound search interval
fathe value \( f(a) \)
fbthe value \( f(b) \)
pfunobject storing the function

◆ fa()

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

◆ fb()

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

◆ num_fun_eval()

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

◆ select()

template<typename Real>
void Utils::AlgoBracket< Real >::select ( unsigned i_algo)
inline

◆ set_max_fun_evaluation()

template<typename Real>
void Utils::AlgoBracket< Real >::set_max_fun_evaluation ( Integer mfev)

Fix the maximum number of evaluation.

Parameters
mfevthe maximum number of evaluation of \( f(x) \)

◆ set_max_iterations()

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

Fix the maximum number of iteration.

Parameters
mitthe maximum number of iteration

◆ tolerance_f()

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

◆ tolerance_x()

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

◆ used_iter()

template<typename Real>
Integer Utils::AlgoBracket< 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: