2020-05-29 11:04:35 +08:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief Solve the equation \f$f(x)=0\f$ using [false position
|
|
|
|
* method](https://en.wikipedia.org/wiki/Regula_falsi), also known as the Secant
|
|
|
|
* method
|
|
|
|
*
|
|
|
|
* Given two points \f$a\f$ and \f$b\f$ such that \f$f(a)<0\f$ and
|
|
|
|
* \f$f(b)>0\f$, then the \f$(i+1)^\text{th}\f$ approximation is given by: \f[
|
|
|
|
* x_{i+1} = \frac{a_i\cdot f(b_i) - b_i\cdot f(a_i)}{f(b_i) - f(a_i)}
|
|
|
|
* \f]
|
|
|
|
* For the next iteration, the interval is selected
|
|
|
|
* as: \f$[a,x]\f$ if \f$x>0\f$ or \f$[x,b]\f$ if \f$x<0\f$. The Process is
|
|
|
|
* continued till a close enough approximation is achieved.
|
|
|
|
*
|
|
|
|
* \see newton_raphson_method.cpp, bisection_method.cpp
|
|
|
|
*/
|
2020-05-26 11:32:31 +08:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstdlib>
|
2019-12-22 22:50:33 +08:00
|
|
|
#include <iostream>
|
2020-05-29 11:04:35 +08:00
|
|
|
#include <limits>
|
2020-05-26 11:32:31 +08:00
|
|
|
|
2020-05-29 11:04:35 +08:00
|
|
|
#define EPSILON \
|
|
|
|
1e-6 // std::numeric_limits<double>::epsilon() ///< system accuracy limit
|
|
|
|
#define MAX_ITERATIONS 50000 ///< Maximum number of iterations to check
|
|
|
|
|
|
|
|
/** define \f$f(x)\f$ to find root for
|
|
|
|
*/
|
|
|
|
static double eq(double i) {
|
|
|
|
return (std::pow(i, 3) - (4 * i) - 9); // origial equation
|
|
|
|
}
|
|
|
|
|
|
|
|
/** get the sign of any given number */
|
|
|
|
template <typename T>
|
|
|
|
int sgn(T val) {
|
|
|
|
return (T(0) < val) - (val < T(0));
|
2019-12-22 22:50:33 +08:00
|
|
|
}
|
2020-05-26 11:32:31 +08:00
|
|
|
|
2020-05-29 11:04:35 +08:00
|
|
|
/** main function */
|
2019-12-22 22:50:33 +08:00
|
|
|
int main() {
|
2020-05-29 11:04:35 +08:00
|
|
|
double a = -1, b = 1, x, z, m, n, c;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// loop to find initial intervals a, b
|
|
|
|
for (int i = 0; i < MAX_ITERATIONS; i++) {
|
|
|
|
z = eq(a);
|
|
|
|
x = eq(b);
|
|
|
|
if (sgn(z) == sgn(x)) { // same signs, increase interval
|
|
|
|
b++;
|
|
|
|
a--;
|
|
|
|
} else { // if opposite signs, we got our interval
|
2020-05-26 11:32:31 +08:00
|
|
|
break;
|
2019-12-22 22:50:33 +08:00
|
|
|
}
|
2020-05-26 11:32:31 +08:00
|
|
|
}
|
|
|
|
|
2019-12-22 22:50:33 +08:00
|
|
|
std::cout << "\nFirst initial: " << a;
|
|
|
|
std::cout << "\nSecond initial: " << b;
|
2020-05-26 11:32:31 +08:00
|
|
|
|
2020-05-29 11:04:35 +08:00
|
|
|
for (i = 0; i < MAX_ITERATIONS; i++) {
|
2019-12-22 22:50:33 +08:00
|
|
|
m = eq(a);
|
|
|
|
n = eq(b);
|
2020-05-29 11:04:35 +08:00
|
|
|
|
2019-12-22 22:50:33 +08:00
|
|
|
c = ((a * n) - (b * m)) / (n - m);
|
2020-05-29 11:04:35 +08:00
|
|
|
|
2019-12-22 22:50:33 +08:00
|
|
|
a = c;
|
|
|
|
z = eq(c);
|
2020-05-29 11:04:35 +08:00
|
|
|
|
|
|
|
if (std::abs(z) < EPSILON) { // stoping criteria
|
2020-05-26 11:32:31 +08:00
|
|
|
break;
|
2019-12-22 22:50:33 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-26 11:32:31 +08:00
|
|
|
|
2020-05-29 11:04:35 +08:00
|
|
|
std::cout << "\n\nRoot: " << c << "\t\tSteps: " << i << std::endl;
|
2020-05-26 11:32:31 +08:00
|
|
|
return 0;
|
2019-12-22 22:50:33 +08:00
|
|
|
}
|