This program was working fine yesterday, and then all of a sudden today it isn't working and I haven't changed anything?! It complies OK, but for for easy functions that I enter and know the roots of, it says there is no root.
Can anyone please explain what is going on?
Thanks in advance!
#include <iostream>
#include <cmath>
using namespace std;
void bisection(double a, double b, double epsilon, int max, double A, double B, double C);
double f(double, double, double, double);
int main()
{
int max = 0; // maximum number of iterations
double a = 0.0; // left endpoint of original interval
double b = 0.0; // right endpoint of the original interval
double epsilon = 0.0; // convergence criterion
double A = 0.0; // A coefficient of inputed quadratic equation
double B = 0.0; // B coefficient of inputed quadratic equation
double C = 0.0; // C coefficient of inputed quadratic equation
cout << "Enter coefficents of a quadratic equation\n"
<< "in the following order: Ax^2 + Bx + C = 0" << endl;
cout << "\nEnter A: ";
cin >> A;
cout << "Enter B: ";
cin >> B;
cout << "Enter C: ";
cin >> C;
cout << "\nEnter the left endpoint of the original search interval (a): ";
cin >> a;
cout << "Enter the right endpoint of the original search interval (b): ";
cin >> b;
cout << "Enter the convergence criteria: ";
cin >> epsilon;
cout << "Enter the maximum number of iterations allowed: ";
cin >> max;
bisection(a, b, epsilon, max, A, B, C);
return 0;
}// end of main
void bisection(double a, double b, double epsilon, int max, double A, double B, double C)
{
int i = 0; // current iteration loop variable
double x1 = 0.0; // left endpoint of current interval
double x2 = 0.0; // right endpoint of current interval
double xmid = 0.0; // computed midpoint of current interval
double f1 = 0.0; // function evaluated at left endpoint of current interval
double f2 = 0.0; // function evaluated at right endpoint of current interval
double fmid = 0.0;; // function evaluated at computed midpoint of current interval
double width = 0.0; // width of original interval (b - a)
double curwidth = 0.0; // width of current interval (xmid - x1)
x1 = a;
xmid = b;
f1 = f(x1, A, B, C);
fmid = f(xmid, A, B, C);
width = (b - a);
// verify there is a root in the interval
if (f1 * fmid > 0.0)
{
cout << "\nNo root in the original interval exists" << endl;
}
else
{
for (i = 1; i <= max; i++)
{
x2 = (x1 + xmid) / 2.0;
cout << "The next midpoint, approximation is " << x2 << endl;
f2 = f(x2, A, B, C);
if (f1 * f2 <= 0.0) // root is in left half interval
{
curwidth = (x2 - x1) / 2.0;
fmid = f2;
xmid = x2;
}
else // root is in right half interval
{
curwidth = (xmid - x2) / 2.0;
f1 = f2;
x1 = x2;
}
if (curwidth < epsilon)
{
cout << "\nA root at x = " << x2 << " was found "
<< "in " << i << " iterations" << endl;
cout << "The value of the function is " << f2 << endl;
return;
}
}
}
cout << "***** BISECTION METHOD *****"
<< "\nAfter " << max << " iterations, no root was found "
<< "within the convergence criterion\n"
<< "The search for a root has failed due to excessive iterations\n"
<< "after the maximum number of " << max << " iterations" << endl;
return;
}// end of bisection
double f(double x, double A, double B, double C) // function to evaluate f(x)
{
return A * pow(x, 2) + B * x + C;
}// end of f