Abstract

This algorithm uses the bisection algorithm to solve the equation 3x² + 12x + 1 = 0.  The equation being solved can be changed by recoding the F function.

Source Code - ../mp/bisection.cpp

/**************************************************
 * File:        Bisection.c
 * Description: Simple implementation of the
 *              bisection algorithm in C
 **************************************************/

#include <math.h>
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
 
class Function {
  private:
    double * Xi;
    double * Yi;
    unsigned N;
  public:
    double Y (double X);
    bool   InputPoints ();
};

class Bisection {
  private:
    double X1;
    double X2;
    double Y1;
    double Y2;
    double X;
    double Y;
    double Tol;
    unsigned Limit;
    unsigned N;
    bool OK;
    Function F;
    bool Bisect (); 
    void Input (); 
    void Output (); 
    void Step (); 
  public:
         Bisection ();
    void Run ();  
};

double F (double);
void Input (double *, double *, double *, unsigned *); 
void Output (double, double, unsigned, int); 
void Step (double *, double *, double *, double *, double *, double *); 
 
int main () {
  Bisection Algorithm;

  cout << "Bisection algorithm - C++\n";
  Algorithm.Run ();
  
  return 0;
}


Bisection::Bisection () {
}

void Bisection::Run () {  
  Input ();
  OK = Bisect ();
  Output ();
}  

bool Bisection::Bisect () {
  
  Y1 = F.Y (X1);
  Y2 = F.Y (X2);
  OK = (Y1 > 0) != (Y2 > 0);
  cout << "    ";
  cout.width (12);
  cout << X1;
  cout.width (12);
  cout << Y1;
  cout << "\n";
  cout << "    ";
  cout.width (12);
  cout << X2;
  cout.width (12);
  cout << Y2;
  cout << "\n";
  if (OK) {
    N = 0;
    while ((fabs (X1 - X2) > Tol) && (N < Limit)) {
      Step ();
      ++N;
      cout << "    ";
      cout.width (12);
      cout << X;
      cout.width (12);
      cout << Y;
      cout << "\n";
    }
  }
  return OK;
} 

void Bisection::Input () {
  cout << "First X value?  ";
  cin >> X1;
  cout << "Second X value?  ";
  cin >> X2;
  cout << "Tolerance?  ";
  cin >> Tol;
  cout << "Maximum iterations?  ";
  cin >> Limit;
}
 
void Bisection::Output () {
  if (OK) {
    cout << "Algorithm succeeded after " << N << " steps\n";
  } else {
    cout << "Algorithm failed after " << N << " steps\n";
  }
  cout << "X = " << X << "\n";
  cout << "Y = " << Y << "\n";
}

void Bisection::Step () {
  X = (X1 + X2) / 2.0;
  Y = F.Y (X);
  if ((Y1 < 0) && (Y < 0)) {
    X1 = X;
    Y1 = Y;
  } else {
    X2 = X;
    Y2 = Y;
  }
}

double Function::Y (double X) {
  double Y;
  Y = 1.0 + 3.0 * X * (X + 4.0); 
  return Y;
}

bool Function::InputPoints () {
  unsigned i;
  cout << "Number of points?  ";
  cin >> N;
  Xi = new double [N];
  Ti = new double [N];
  i = 0;
  while (i < N) {
    cout << "X[" << i << }]\n";
    cin >> Xi[u];
    cout << "Y[" << i << }]\n";
    cin >> Yi[u];
    ++i;
  }
  return true;
}

Other Languages

C Now C Bisection Algorithm
C++ Now
C++ Bisection Algorithm
FORTRAN Now FORTRAN Bisection Algorithm
Java Now Java Bisection Algorithm