diff --git a/numerical_methods/false_position.cpp b/numerical_methods/false_position.cpp index aebd154bb..f4554aa2d 100644 --- a/numerical_methods/false_position.cpp +++ b/numerical_methods/false_position.cpp @@ -4,7 +4,12 @@ * 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 + * \details + * First, multiple intervals are selected with the interval gap provided. + * Separate recursive function called for every root. + * Roots are printed Separatelt. + * + * For an interval [a,b] \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] @@ -13,62 +18,112 @@ * continued till a close enough approximation is achieved. * * \see newton_raphson_method.cpp, bisection_method.cpp + * + * \author Unknown author + * \author [Samruddha Patil](https://github.com/sampatil578) */ -#include -#include -#include -#include +#include /// for math operations +#include /// for io operations -#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 +/** + * @namespace numerical_methods + * @brief Numerical methods */ -static double eq(double i) { - return (std::pow(i, 3) - (4 * i) - 9); // origial equation +namespace numerical_methods { +/** + * @namespace false_position + * @brief Functions for [False Position] + * (https://en.wikipedia.org/wiki/Regula_falsi) method. + */ +namespace false_position { +/** +* @brief This function gives the value of f(x) for given x. +* @param x value for which we have to find value of f(x). +* @return value of f(x) for given x. +*/ +static float eq(float x) { + return (x*x-x); // original equation } -/** get the sign of any given number */ -template -int sgn(T val) { - return (T(0) < val) - (val < T(0)); +/** +* @brief This function finds root of the equation in given interval i.e. (x1,x2). +* @param x1,x2 values for an interval in which root is present. + @param y1,y2 values of function at x1, x2 espectively. +* @return root of the equation in the given interval. +*/ +static float regula_falsi(float x1,float x2,float y1,float y2){ + float diff = x1-x2; + if(diff<0){ + diff= (-1)*diff; + } + if(diff<0.00001){ + if (y1<0) { + y1=-y1; + } + if (y2<0) { + y2=-y2; + } + if (y1