[cpp]
// bisect.cpp
// Program to find a root of an equation by the bisection method
// A J Norman, Leicester University Engineering Department, Dec 1993
// Pascal -> C++ Jul 1995
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include <string.h>
#include <time.h>
#define MIN_TOLERANCE 1.0e-10
int bisection (double, double, double, int, int &,
double &, double &, char []);
void get_conditions (double &, double &, double &, int &);
double user_function (double x);
int main (void)
{
clock_t start, finish;
double x1, x2, tolerance, root, value, time_taken;
int max_iterations, iterations;
char error, err_string[80];
get_conditions (x1, x2, tolerance, max_iterations);
start = clock();
// Do the calculation 50000 times so that the time taken is a few
// seconds, rather than a few milliseconds.
for (unsigned long i = 0 ; i < 50000; i++)
{
error = bisection(x1, x2, tolerance, max_iterations, iterations,
root, value, err_string);
}
finish = clock();
time_taken = (finish - start) / double(CLOCKS_PER_SEC);
// Print out the number of iterations, or an error message
if (error)
{
cout << "ERROR : " << err_string << endl;
}
else
{
cout << "Successful calculation, " << iterations
<< " iterations." << endl
<< "The root is " << root << endl
<< "The value of the function at this point is "
<< setiosflags(ios::fixed) << setprecision(2)
<< value << endl
<< "Time for 1000 bisection calculations was "
<< time_taken << " seconds." << endl;
}
return 0;
}
//======================================================================
void get_conditions (double & x1, double & x2, double & tolerance,
int & max_iterations)
{
cout << "Bisection method to find the root of an equation." << endl
<< "Input two values of x." << endl
<< "f(x1) must be positive and f(x2) negative, or vice versa."
<< endl << "X1 = ";
cin >> x1;
cout << "X2 = ";
cin >> x2;
cout << "Calculation will terminate when f(x) is less than the"
<< endl << "tolerance level input by the user." << endl;
do
{
cout << "Tolerance : ";
cin >> tolerance;
if (tolerance <= MIN_TOLERANCE)
{
cout << "ERROR : tolerance must be greater than "
<< MIN_TOLERANCE << endl;
}
}
while (tolerance <= MIN_TOLERANCE);
do
{
cout << "Maximum number of iterations (1 or more) : ";
cin >> max_iterations;
}
while (max_iterations < 1);
}
//======================================================================
// Function to find a root of an equation by the bisection method.
// x1, x2 : Range of x within which the root lies. The user
// must ensure that the values of the function at x1
// and x2 have opposing signs.
// tolerance : tolerance level of the answer
// max_iterations : maximum number of iterations to be performed
// root : final value of the root
// value : value of user_funct at the root
// iterations : number of iterations performed
// err_string : string containing an error message, if appropriate
//
// Function returns 1 (true) if an error has been encountered.
//======================================================================
int bisection (double x1, double x2, double tolerance,
int max_iterations, int & iterations,
double & root, double & value, char err_string[])
{
double y1, y2, y3, a1, a2, a3;
// Error checking, to make sure that the range x1 -> x2
// really does contain a root.
y1 = user_function(x1);
y2 = user_function(x2);
if (y1 < 0.0)
{
if (y2 < 0.0)
{
strcpy(err_string,
"The function is negative at both ends of the range");
return 1;
}
else
{
a1 = x1;
a2 = x2;
}
}
else
{
if (y2 > 0.0)
{
strcpy(err_string,
"The function is positive at both ends of the range");
return 1;
}
else
{
a1 = x2;
a2 = x1;
}
}
// Begin the iterations.
for (iterations = 1; iterations <= max_iterations; iterations++)
{
a3 = (a2 - a1) / 2.0 + a1;
y3 = user_function(a3);
if (y3 < 0.0) { a1 = a3; } else { a2 = a3; }
if (fabs(y3) <= tolerance)
{
root = a3;
value = y3;
return 0;
}
}
strcpy(err_string, "Maximum number of iterations was exceeded");
return 1;
}
//======================================================================
// This function describes the mathematical function whose root is
// to be found.
//======================================================================
double user_function (double x)
{
return x*x - 5.0*x + 6.0;
}
[/cpp]
I think is it clear to you now
