Develop a program and algorithm to find the roots of the quadratic equation using bisection method.
#include<bits/stdc++.h>
using namespace std;
#define EP 0.01
double function(double m)
{
return m*m*m - m*m + 2;
}
void bisect(double x, double y)
{
if (function(x) * function(y) >= 0)
{
cout << "Assumptions for values of x and y not correct\n";
return;
}
double c = x;
while ((y-x) >= EP)
{
c = (x+y)/2;
if (function(c) == 0.0)
break;
else if (function(c)*function(x) < 0)
y = c;
else
x = c;
}
cout << "The value of root is : " << c;
}
int main()
{
double x,y;
cout<<"Enter the values of x and y: "<<endl;
cin>>x>>y;
bisect(x, y);
return 0;
}
Comments
Leave a comment