diff --git a/README.md b/README.md index fb460cc..a71a2da 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ | | Matlab | Python | C++ | | --- | --- | --- | --- | -| Bisection method| :octocat: | | | +| Bisection method| :octocat: | |:octocat: | | Newton-Raphson method | :octocat: | | | | Secant method | :octocat: | | | diff --git a/cpp/BisectionMethod/README.md b/cpp/BisectionMethod/README.md new file mode 100644 index 0000000..ba1825c --- /dev/null +++ b/cpp/BisectionMethod/README.md @@ -0,0 +1,18 @@ +Implementation of Bisection Method. +All .cpp files, .h file and Makefile in src folder. + +**Input format** +Usage: ./bisection start_point finish_point stoppingCriterion epsilon + +**Output format** + +Iteration: #numOfIteration, the absolute error: #valueOfAbsoluteError, the relative error: #valueOfRelativeError + +**Sample input** +Function: x + 1 − 2sin(πx) = 0 for 0.5 ≤ x ≤ 1 +Main arguments: 0.5 1 DISTANCE_TO_ROOT 0.00001 + +**Sample output** +![Sample Output](https://github.com/sevvalmehder/numerical-methods/blob/master/cpp/BisectionMethod/SampleOutput.jpg) + + diff --git a/cpp/BisectionMethod/SampleOutput.jpg b/cpp/BisectionMethod/SampleOutput.jpg new file mode 100644 index 0000000..4833585 Binary files /dev/null and b/cpp/BisectionMethod/SampleOutput.jpg differ diff --git a/cpp/BisectionMethod/src/Makefile b/cpp/BisectionMethod/src/Makefile new file mode 100644 index 0000000..2a32a29 --- /dev/null +++ b/cpp/BisectionMethod/src/Makefile @@ -0,0 +1,14 @@ +all: compile + +compile: bisectionMethod.o main.o + g++ bisectionMethod.o main.o -o bisection + +main.o: main.cpp + g++ -c main.cpp + +bisectionMethod.o: bisectionMethod.cpp + g++ -c bisectionMethod.cpp + +clean: + rm *.o bisection + clear \ No newline at end of file diff --git a/cpp/BisectionMethod/src/bisectionMethod.cpp b/cpp/BisectionMethod/src/bisectionMethod.cpp new file mode 100644 index 0000000..4c6d6fc --- /dev/null +++ b/cpp/BisectionMethod/src/bisectionMethod.cpp @@ -0,0 +1,125 @@ +/* + * @author: Sevval MEHDER + * + * Bisection Method Class soruce codes + * + */ + +#include "bisectionMethod.h" +#include // for abs() +#include // for setw + +Bisection::Bisection(double (*function)(double)){ + + // Set the coming function as a test function + setTestFunction(function); + +} + +bool Bisection::bisectionMethod(double start_a, double finish_b, std::string stoppingCriterion, + double epsilon){ + + // Current Iteration Value + int curIterVal = 1; + double root, rootNext; + double a = start_a, b = finish_b; + + // Find the root + root = (start_a + finish_b) / 2; + + // Show the values + std::cout << "Iteration: " << curIterVal + << "\t the absolute error: " << calculateAbsoluteError(start_a, root) + << "\t the relative error: " << calculateRelativeError(start_a ,root) << std::endl; + + do{ + // If they have opposite sign + if( (*test_function)(start_a) * (*test_function)(root) < 0) + b = root; + else + a = root; + + // Change the root + rootNext = (a + b) / 2; + + //Before 100 iteration, search the given stopping criterion + if( ErrorChecks(stoppingCriterion, root, rootNext, epsilon) ){ + + ++curIterVal; + + // Show the values + std::cout << "Iteration: " << curIterVal + << "\t the absolute error: " << std::setw(10) << calculateAbsoluteError(root, rootNext) + << "\t the relative error: " << std::setw(10) << calculateRelativeError(root, rootNext) << std::endl; + + // Show the result + std::cout << "After " << curIterVal << " iterations, approximate root is found " + << rootNext << "\nTheoretically required " << curIterVal << "iterations" << std::endl; + return true; + + } + // Increase the iteration + ++curIterVal; + + // Show the values + std::cout << "Iteration: " << curIterVal + << "\t the absolute error: " << std::setw(10) << calculateAbsoluteError(root, rootNext) + << "\t the relative error: " << std::setw(10) << calculateRelativeError(root, rootNext) << std::endl; + + root = rootNext; + } + while(curIterVal <= 100); + // After 100 iteration give an error message + + std::cout << "There is no root on this " << std::endl; + + return false; +} + +bool Bisection::ErrorChecks( std::string errorType, double rootF, double rootS, double epsilonValue){ + + if(errorType.compare("DISTANCE_TO_ROOT") == 0){ + + if(std::abs((*test_function)(rootS)) < epsilonValue) + return true; + else + return false; + } + else if(errorType.compare("ABSOLUTE_ERROR") == 0){ + + if(std::abs( rootS - rootF ) < epsilonValue) + return true; + else + return false; + + } + else if(errorType.compare("RELATIVE_ERROR") == 0){ + + if(std::abs( rootS - rootF ) / std::abs(rootS) < epsilonValue) + return true; + else + return false; + + } + else + return false; +} + + +double Bisection::calculateAbsoluteError(double root, double rootNext){ + + return std::abs(rootNext - root); + +} + +double Bisection::calculateRelativeError(double root, double rootNext){ + + return std::abs(rootNext - root) / std::abs(rootNext); + +} + +void Bisection::setTestFunction(double (*function)(double)){ + + test_function = function; + +} \ No newline at end of file diff --git a/cpp/BisectionMethod/src/bisectionMethod.h b/cpp/BisectionMethod/src/bisectionMethod.h new file mode 100644 index 0000000..22f3441 --- /dev/null +++ b/cpp/BisectionMethod/src/bisectionMethod.h @@ -0,0 +1,72 @@ +/* + * @author: Sevval MEHDER + * + * Bisection Method Class Header File + * + */ + +#include +#include + +class Bisection{ + + +public: + + /* + * Constructor + */ + Bisection(double (*function)(double)); + + /* This function implements Bisection method + * Params: + * start_a the start of the root search interval as a real value + * finish_b the end of the root search interval as a real value + * stoppingCriterion the type of stopping criterion as a String + * epsilon ϵ as a real value + * boolean, if we find a root before 100 iterations return true, otherwise false + */ + bool bisectionMethod(double start_a, double finish_b, std::string stoppingCriterion, + double epsilon); + + /* + * This function checks the error + * Params: + * errorType the type of error + * rootF first root (P(n-1)) + * rootS second root (P(n)) + * epsilonValue the epsilon value + */ + bool ErrorChecks(std::string errorType, double rootF, double rootS, double epsilonValue ); + + /* + * This function calculates the absolute error + * Params: + * root is the first root (P(n-1)) + * rootNext is the second root (P(n)) + * Return: + * the value of absolute error as double + */ + double calculateAbsoluteError(double root, double rootNext); + + /* + * This function calculates the relative error + * Params: + * root is the first root (P(n-1)) + * rootNext is the second root (P(n)) + * Return: + * the value of relative error as double + */ + double calculateRelativeError(double root, double rootNext); + + /* + * Setter for test function pointer + */ + void setTestFunction(double (*function)(double)); + +private: + + // The test function pointer + double (*test_function)(double); + +}; \ No newline at end of file diff --git a/cpp/BisectionMethod/src/main.cpp b/cpp/BisectionMethod/src/main.cpp new file mode 100644 index 0000000..ced3787 --- /dev/null +++ b/cpp/BisectionMethod/src/main.cpp @@ -0,0 +1,39 @@ +/* + * @author: Sevval MEHDER + */ + +#include +#include // for exp() +#include // for atof() +#include "bisectionMethod.h" + + +double F(double x); + +int main(int argc, char *argv[]){ + + if(argc != 5){ + std::cout << "Usage: ./bisection start_point finish_point stoppingCriterion epsilon" << std::endl; + return 0; + } + + // Create a Bisection object + Bisection bMethod(&F); + + // Call the method function + bMethod.bisectionMethod(atof(argv[1]), atof(argv[2]), argv[3], atof(argv[4])); + + return 1; +} + +// F is a function as like y = x + 2 +double F(double x){ + + double result; + + //std::cout << "Function: x + 1 − 2sin(πx) = 0 for 0.5 ≤ x ≤ 1" << std::endl; + result = (x + 1 - (2*sin(x*M_PI))); + + return result; + +} \ No newline at end of file