Created
January 22, 2019 21:56
-
-
Save prodhan/5688d3248db2fba160ff6d17492e9f7d to your computer and use it in GitHub Desktop.
Bisection method in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Created By Ariful Islam | |
Batch E-64 (DIU) | |
Roll: 34 | |
*/ | |
#include<iostream> | |
#include <math.h> | |
using namespace std; | |
double func(double x) | |
{ | |
return x*x*x - x -1; | |
} | |
void bisection(double a,double b, double e) | |
{ | |
double xi; | |
e=1/pow(10,e); | |
if(func(a) * func(b) >= 0) | |
{ | |
cout<<"Incorrect a and b"; | |
return; | |
} | |
else { | |
while ((b - a) >= e) { | |
xi = (a + b) / 2; | |
if (func(xi) == 0.0) { | |
cout << "Root = " << xi << endl; | |
break; | |
} else if (func(xi) * func(a) < 0) { | |
cout << "Root = " << xi << endl; | |
b = xi; | |
} else { | |
cout << "Root = " << xi << endl; | |
a = xi; | |
} | |
} | |
cout << "\nAccurate Root calculated is = " << xi; | |
} | |
} | |
int main() | |
{ | |
double a,b,e; | |
cout<<"Enter the real value for constant 'a': "; | |
cin>>a; | |
cout<<"Enter the real value for constant 'b': "; | |
cin>>b; | |
cout<<"Enter How Much Digit You Want to Match At Decimal Place: "; | |
cin>>e; | |
printf("The function used is x^3-x-1\n"); | |
cout<<"a = "<<a<<endl; | |
cout<<"b = "<<b<<endl; | |
bisection(a,b,e); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment