diff --git a/computer_oriented_statistical_methods/bisection_method.cpp b/computer_oriented_statistical_methods/bisection_method.cpp index 089828c9a..c93c529d2 100644 --- a/computer_oriented_statistical_methods/bisection_method.cpp +++ b/computer_oriented_statistical_methods/bisection_method.cpp @@ -1,25 +1,60 @@ +/** + * \file + * \brief Solve the equation \f$f(x)=0\f$ using [bisection + * method](https://en.wikipedia.org/wiki/Bisection_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+b_i}{2} + * \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, false_position.cpp, secant_method.cpp + */ #include #include +#include -static float eq(float i) { +#define EPSILON \ + 1e-6 // std::numeric_limits::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); // original equation } -int main() { - float a, b, x, z; +/** get the sign of any given number */ +template +int sgn(T val) { + return (T(0) < val) - (val < T(0)); +} - for (int i = 0; i < 100; i++) { - z = eq(i); - if (z >= 0) { - b = i; - a = --i; +/** main function */ +int main() { + double a = -1, b = 1, x, z; + 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 break; } } std::cout << "\nFirst initial: " << a; std::cout << "\nSecond initial: " << b; - for (int i = 0; i < 100; i++) { + + // start iterations + for (i = 0; i < MAX_ITERATIONS; i++) { x = (a + b) / 2; z = eq(x); std::cout << "\n\nz: " << z << "\t[" << a << " , " << b @@ -31,10 +66,10 @@ int main() { b = x; } - if (z > 0 && z < 0.0009) // stoping criteria + if (std::abs(z) < EPSILON) // stoping criteria break; } - std::cout << "\n\nRoot: " << x; + std::cout << "\n\nRoot: " << x << "\t\tSteps: " << i << std::endl; return 0; } diff --git a/computer_oriented_statistical_methods/false_position.cpp b/computer_oriented_statistical_methods/false_position.cpp index c5a314508..aebd154bb 100644 --- a/computer_oriented_statistical_methods/false_position.cpp +++ b/computer_oriented_statistical_methods/false_position.cpp @@ -1,19 +1,53 @@ +/** + * \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 + */ #include #include #include +#include -static float eq(float i) { - return (pow(i, 3) - (4 * i) - 9); // origial equation +#define EPSILON \ + 1e-6 // std::numeric_limits::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 +int sgn(T val) { + return (T(0) < val) - (val < T(0)); +} + +/** main function */ int main() { - float a, b, z, c, m, n; - system("clear"); - for (int i = 0; i < 100; i++) { - z = eq(i); - if (z >= 0) { - b = i; - a = --i; + 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 break; } } @@ -21,18 +55,20 @@ int main() { std::cout << "\nFirst initial: " << a; std::cout << "\nSecond initial: " << b; - for (int i = 0; i < 100; i++) { - float h, d; + for (i = 0; i < MAX_ITERATIONS; i++) { m = eq(a); n = eq(b); + c = ((a * n) - (b * m)) / (n - m); + a = c; z = eq(c); - if (z > 0 && z < 0.09) { // stoping criteria + + if (std::abs(z) < EPSILON) { // stoping criteria break; } } - std::cout << "\n\nRoot: " << c; + std::cout << "\n\nRoot: " << c << "\t\tSteps: " << i << std::endl; return 0; } diff --git a/computer_oriented_statistical_methods/newton_raphson_method.cpp b/computer_oriented_statistical_methods/newton_raphson_method.cpp index 47d276490..318363a39 100644 --- a/computer_oriented_statistical_methods/newton_raphson_method.cpp +++ b/computer_oriented_statistical_methods/newton_raphson_method.cpp @@ -1,42 +1,57 @@ +/** + * \file + * \brief Solve the equation \f$f(x)=0\f$ using [Newton-Raphson + * method](https://en.wikipedia.org/wiki/Newton%27s_method) + * + * The \f$(i+1)^\text{th}\f$ approximation is given by: + * \f[ + * x_{i+1} = x_i - \frac{f(x_i)}{f'(x_i)} + * \f] + * + * \see bisection_method.cpp, false_position.cpp + */ #include +#include #include +#include -static float eq(float i) { +#define EPSILON \ + 1e-6 // std::numeric_limits::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); // original equation } -static float eq_der(float i) { +/** define the derivative function \f$f'(x)\f$ + */ +static double eq_der(double i) { return ((3 * std::pow(i, 2)) - 4); // derivative of equation } +/** Main function */ int main() { - float a, b, z, c, m, n; + std::srand(std::time(nullptr)); // initialize randomizer - for (int i = 0; i < 100; i++) { - z = eq(i); - if (z >= 0) { - b = i; - a = --i; - break; - } - } + double z, c = std::rand() % 100, m, n; + int i; - std::cout << "\nFirst initial: " << a; - std::cout << "\nSecond initial: " << b; - c = (a + b) / 2; + std::cout << "\nInitial approximation: " << c; - for (int i = 0; i < 100; i++) { - float h; + // start iterations + for (i = 0; i < MAX_ITERATIONS; i++) { m = eq(c); n = eq_der(c); z = c - (m / n); c = z; - if (m > 0 && m < 0.009) // stoping criteria + if (std::abs(m) < EPSILON) // stoping criteria break; } - std::cout << "\n\nRoot: " << z << std::endl; + std::cout << "\n\nRoot: " << z << "\t\tSteps: " << i << std::endl; return 0; }