Skip to content

Instantly share code, notes, and snippets.

@prodhan
Created March 8, 2019 18:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prodhan/f437f30bb920721a3644f74ae88cef78 to your computer and use it in GitHub Desktop.
Save prodhan/f437f30bb920721a3644f74ae88cef78 to your computer and use it in GitHub Desktop.
Problem No 22 using Bisection Method
/*
Created By Ariful Islam
Batch E-64 (DIU)
Roll: 34
problem no 22
EQN: x=cubic root (48)
Method: Bisection
Note: Root lies between 3 and 4
*/
#include<iostream>
#include <math.h>
using namespace std;
double func(double x)
{
return x-cbrt(48);
}
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;
cout<<"The function used is x=cubic root (48)\n"<<endl;
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