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 41 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 46 of file Root1D.h.

47 : f_(f), a_(a), b_(b), fa_(f(a)), fb_(f(b))
48 {
49 static_assert(std::is_same_v<double, decltype(f(0.0))>,
50 "Using this class for functions that do not return double"
51 " involves a lot of casts. Hence it is disabled.");
52
54 {
55 b_ = a_;
56 }
57 else if (detail::almost_zero(fb_))
58 {
59 a_ = b_;
60 }
61 else if (detail::same_sign(fa_, fb_))
62 {
63 OGS_FATAL("Regula falsi cannot be done, because the function values"
64 " at the interval ends have the same sign.");
65 }
66 }
#define OGS_FATAL(...)
Definition Error.h:19

References a_, MathLib::Nonlinear::detail::almost_zero(), b_, f_, fa_, 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 116 of file Root1D.h.

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

References a_, and 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 102 of file Root1D.h.

103 {
104 if (a_ == b_)
105 {
106 return a_;
107 }
108
109 const double s = (fb_ - fa_) / (b_ - a_);
110 const double c = a_ - fa_ / s;
111
112 return c;
113 }

References a_, b_, fa_, 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 69 of file Root1D.h.

70 {
71 for (unsigned i=0; i<num_steps; ++i)
72 {
73 if (a_ == b_)
74 {
75 return;
76 }
77
78 const double s = (fb_ - fa_) / (b_ - a_);
79 const double c = a_ - fa_ / s;
80 const double fc = f_(c);
81
83 a_ = b_ = c;
84 return;
85 }
87 {
88 a_ = b_;
89 fa_ = fb_;
90 b_ = c;
91 fb_ = fc;
92 } else {
93 const double m = SubType::get_m(fa_, fb_, fc);
94 fa_ *= m;
95 b_ = c;
96 fb_ = fc;
97 }
98 }
99 }

References a_, MathLib::Nonlinear::detail::almost_zero(), b_, f_, fa_, fb_, MathLib::s, and MathLib::Nonlinear::detail::same_sign().

Member Data Documentation

◆ a_

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

Definition at line 120 of file Root1D.h.

Referenced by RegulaFalsi(), getRange(), getResult(), and step().

◆ b_

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

Definition at line 120 of file Root1D.h.

Referenced by RegulaFalsi(), getRange(), getResult(), and step().

◆ f_

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

Definition at line 119 of file Root1D.h.

Referenced by RegulaFalsi(), and step().

◆ fa_

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

Definition at line 120 of file Root1D.h.

Referenced by RegulaFalsi(), getResult(), and step().

◆ fb_

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

Definition at line 120 of file Root1D.h.

Referenced by RegulaFalsi(), getResult(), and step().


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