Skip to content

Implementation of Bisection Method in C++ #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

| | Matlab | Python | C++ |
| --- | --- | --- | --- |
| Bisection method| :octocat: | | |
| Bisection method| :octocat: | |:octocat: |
| Newton-Raphson method | :octocat: | | |
| Secant method | :octocat: | | |

Expand Down
18 changes: 18 additions & 0 deletions cpp/BisectionMethod/README.md
Original file line number Diff line number Diff line change
@@ -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)


Binary file added cpp/BisectionMethod/SampleOutput.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions cpp/BisectionMethod/src/Makefile
Original file line number Diff line number Diff line change
@@ -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
125 changes: 125 additions & 0 deletions cpp/BisectionMethod/src/bisectionMethod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* @author: Sevval MEHDER
*
* Bisection Method Class soruce codes
*
*/

#include "bisectionMethod.h"
#include <cmath> // for abs()
#include <iomanip> // 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;

}
72 changes: 72 additions & 0 deletions cpp/BisectionMethod/src/bisectionMethod.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* @author: Sevval MEHDER
*
* Bisection Method Class Header File
*
*/

#include <iostream>
#include <string>

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);

};
39 changes: 39 additions & 0 deletions cpp/BisectionMethod/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* @author: Sevval MEHDER
*/

#include <iostream>
#include <math.h> // for exp()
#include <stdlib.h> // 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;

}