OGS
MathLib::Nonlinear::RegulaFalsi< SubType, Function > Class Template Reference

Detailed Description

template<typename SubType, typename Function>
class MathLib::Nonlinear::RegulaFalsi< SubType, Function >

Use the regula falsi method to find the root of some scalar function of one variable.

Definition at line 48 of file Root1D.h.

#include <Root1D.h>

Public Member Functions

 RegulaFalsi (Function &&f, double a, double b)
 
void step (const unsigned num_steps)
 Do num_steps iteration of regula falsi.
 
double getResult () const
 Returns the current estimate of the root.
 
double getRange () const
 Returns the size of the current search interval.
 

Private Attributes

Function f_
 
double a_
 
double b_
 
double fa_
 
double fb_
 

Constructor & Destructor Documentation

◆ RegulaFalsi()

template<typename SubType , typename Function >
MathLib::Nonlinear::RegulaFalsi< SubType, Function >::RegulaFalsi ( Function && f,
double a,
double b )
inline

Initializes finding a root of the Function f in the interval [a, b].

Definition at line 53 of file Root1D.h.

54 : f_(f), a_(a), b_(b), fa_(f(a)), fb_(f(b))
55 {
56 static_assert(std::is_same_v<double, decltype(f(0.0))>,
57 "Using this class for functions that do not return double"
58 " involves a lot of casts. Hence it is disabled.");
59
61 {
62 b_ = a_;
63 }
64 else if (detail::almost_zero(fb_))
65 {
66 a_ = b_;
67 }
68 else if (detail::same_sign(fa_, fb_))
69 {
70 OGS_FATAL("Regula falsi cannot be done, because the function values"
71 " at the interval ends have the same sign.");
72 }
73 }
#define OGS_FATAL(...)
Definition Error.h:26
bool almost_zero(double a)
Definition Root1D.h:38
bool same_sign(double a, double b)
Tells if a and b have the same sign.
Definition Root1D.h:30

References MathLib::Nonlinear::RegulaFalsi< SubType, Function >::a_, MathLib::Nonlinear::detail::almost_zero(), MathLib::Nonlinear::RegulaFalsi< SubType, Function >::b_, MathLib::Nonlinear::RegulaFalsi< SubType, Function >::fa_, MathLib::Nonlinear::RegulaFalsi< SubType, Function >::fb_, OGS_FATAL, and MathLib::Nonlinear::detail::same_sign().

Member Function Documentation

◆ getRange()

template<typename SubType , typename Function >
double MathLib::Nonlinear::RegulaFalsi< SubType, Function >::getRange ( ) const
inline

Returns the size of the current search interval.

Definition at line 123 of file Root1D.h.

123{ return std::abs(a_ - b_); }

References MathLib::Nonlinear::RegulaFalsi< SubType, Function >::a_, and MathLib::Nonlinear::RegulaFalsi< SubType, Function >::b_.

◆ getResult()

template<typename SubType , typename Function >
double MathLib::Nonlinear::RegulaFalsi< SubType, Function >::getResult ( ) const
inline

Returns the current estimate of the root.

Definition at line 109 of file Root1D.h.

110 {
111 if (a_ == b_)
112 {
113 return a_;
114 }
115
116 const double s = (fb_ - fa_) / (b_ - a_);
117 const double c = a_ - fa_ / s;
118
119 return c;
120 }
static const double s

References MathLib::Nonlinear::RegulaFalsi< SubType, Function >::a_, MathLib::Nonlinear::RegulaFalsi< SubType, Function >::b_, MathLib::Nonlinear::RegulaFalsi< SubType, Function >::fa_, MathLib::Nonlinear::RegulaFalsi< SubType, Function >::fb_, and MathLib::s.

◆ step()

template<typename SubType , typename Function >
void MathLib::Nonlinear::RegulaFalsi< SubType, Function >::step ( const unsigned num_steps)
inline

Do num_steps iteration of regula falsi.

Definition at line 76 of file Root1D.h.

77 {
78 for (unsigned i=0; i<num_steps; ++i)
79 {
80 if (a_ == b_)
81 {
82 return;
83 }
84
85 const double s = (fb_ - fa_) / (b_ - a_);
86 const double c = a_ - fa_ / s;
87 const double fc = f_(c);
88
89 if (detail::almost_zero(fc)) {
90 a_ = b_ = c;
91 return;
92 }
93 if (!detail::same_sign(fc, fb_))
94 {
95 a_ = b_;
96 fa_ = fb_;
97 b_ = c;
98 fb_ = fc;
99 } else {
100 const double m = SubType::get_m(fa_, fb_, fc);
101 fa_ *= m;
102 b_ = c;
103 fb_ = fc;
104 }
105 }
106 }

References MathLib::Nonlinear::RegulaFalsi< SubType, Function >::a_, MathLib::Nonlinear::detail::almost_zero(), MathLib::Nonlinear::RegulaFalsi< SubType, Function >::b_, MathLib::Nonlinear::RegulaFalsi< SubType, Function >::f_, MathLib::Nonlinear::RegulaFalsi< SubType, Function >::fa_, MathLib::Nonlinear::RegulaFalsi< SubType, Function >::fb_, MathLib::s, and MathLib::Nonlinear::detail::same_sign().

Member Data Documentation

◆ a_

◆ b_

◆ f_

template<typename SubType , typename Function >
Function MathLib::Nonlinear::RegulaFalsi< SubType, Function >::f_
private

◆ fa_

◆ fb_


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