I've been working for over a month trying to figure out how to write a program that finds the root of an equation using the bisection method. someone please help me! the equation that I need to find the root for is a little complicated, let me explain:

let summation = sum from j = 0, 1, ... , M of (KjNj)/(1 + KjX)
so the equations is as follows:

(x * (1 + summation)) - L = 0

and the program has to take values for K, N, and L and find the root, X. for example, I did M = 1, K1 = 1, N1 = 1, and L = 3 and the root of that equation is 2.303 but my program is giving me 2.98 and I can't figure out where it's going wrong. If someone could please help me I would appreciate it so much! I've been working on this since the beginning of February, no joke. THANKS! here is the program I have so far:

#include <iostream>
#include <math.h>
using namespace std;

enum state {SUCCESS=0, WONT_STOP, BAD_DATA, BAD_ITERATE};

double f(double x, double k[20], double n[20], double L) {
  return ((x * (1 + ((k[20]*n[20])/(1 + (k[20]*x))))) - L);
}

double sgn(double x) {
  if(x>0) {
    return 1;
  } else if(x<0) {
    return -1;
  } else if(x==0) {
    return 0;
  } else {
    return x; // x is not a number
  }
}

state bisection(double a, double b, double k[20], double n[20], double L, double& x, double tolerance, int maxIteration, int debug) {
  double fa = f(a, k, n, L), fb = f(b, k, n, L);

  // Make sure there is a root between a and b
  if(sgn(fa)*sgn(fb) > 0.0) return BAD_DATA;

  // Swap a and b if necessary so a < b
  if(a > b) {
    double c = a, fc = fa;
    a = b; fa = fb;
    b = c; fb = fc;
  }

  // Bisection iteration loop
  double fx, dx = b-a;
  for(int iteration = 0; iteration < maxIteration; iteration++) {
    dx /= 2;
    x = a + dx;
    fx = f(x, k, n, L);

    if(debug) {
      cout << "Iter " << iteration << ": x = " << x << ", dx = " << dx 
    << ", a = " << a << ", b = " << b << ", f(x) = " << fx << endl;
    }

    // Check error tolerance
    if(dx <= tolerance) return SUCCESS;

    if(sgn(fa)*sgn(fx) > 0.0) {
      a = x; fa = fx;
    } else {
      b = x; fb = fx;
    }
  }

  return WONT_STOP;
}

int main() {
  double a,b,root,tol, k[20], n[20], L, m;
  int maxIter;
  int debug;

  // Input

  cout << "enter m: " << endl;
  cin >> m;
  for (int q = 0; q < m; q++) {
      cout << "enter a value for k: " << endl;
      cin >> k[q];
      cout << "enter a value for n: " << endl;
      cin >> n[q];
  }
  cout << "enter a value for L: " << endl;
  cin >> L;
  cout << "enter an endpoint a: " << endl;
  cin >> a;
  cout << "enter an endpoint b: " << endl;
  cin >> b;
  cout << "Enter the level of tolerance: " << endl;
  cin >> tol;
  cout << "enter the maximum number of iterations: " << endl;
  cin >> maxIter;
  cout << "Monitor iterations? (1/0): " << flush;
  cin >> debug;

  // Solve for a root

  state s = bisection(a, b, k, n, L, root, tol, maxIter, debug);

  // Report results

  switch(s) {
  case SUCCESS: {
    int prec = (int) (log10(1.0/tol) + log10(fabs(root))) + 1;
    cout.precision(prec);
    cout << "The root is " << root << endl;
    cout << "  f(" << root << ") = " << f(root, k, n, L) << endl;
    return 0;
  }
  case WONT_STOP:
    cout << "ERROR: Failed to converge in " << maxIter << " iterations!"
         << endl;
    return 1;
  case BAD_DATA:
    cout << "ERROR: Unsuitable interval!" << endl;
    return 1;
  default:
    cout << "ERROR: Coding error!" << endl;
  }
  return 1;
}
enter m:
1
enter a value for k:
1
enter a value for n:
1
enter a value for L:
3
enter an endpoint a:
0
enter an endpoint b:
5
Enter the level of tolerance:
.001
enter the maximum number of iterations:
25
Monitor iterations? (1/0): 1
Iter 0: x = 2.5, dx = 2.5, a = 0, b = 5, f(x) = -0.5
Iter 1: x = 3.75, dx = 1.25, a = 2.5, b = 5, f(x) = 0.75
Iter 2: x = 3.125, dx = 0.625, a = 2.5, b = 3.75, f(x) = 0.125
Iter 3: x = 2.8125, dx = 0.3125, a = 2.5, b = 3.125, f(x) = -0.1875
Iter 4: x = 2.96875, dx = 0.15625, a = 2.8125, b = 3.125, f(x) = -0.03125
Iter 5: x = 3.04688, dx = 0.078125, a = 2.96875, b = 3.125, f(x) = 0.046875
Iter 6: x = 3.00781, dx = 0.0390625, a = 2.96875, b = 3.04688, f(x) = 0.0078125
Iter 7: x = 2.98828, dx = 0.0195313, a = 2.96875, b = 3.00781, f(x) = -0.0117188
Iter 8: x = 2.99805, dx = 0.00976563, a = 2.98828, b = 3.00781, f(x) = -0.00195313
Iter 9: x = 3.00293, dx = 0.00488281, a = 2.99805, b = 3.00781, f(x) = 0.00292969
Iter 10: x = 3.00049, dx = 0.00244141, a = 2.99805, b = 3.00293, f(x) = 0.000488281
Iter 11: x = 2.99927, dx = 0.0012207, a = 2.99805, b = 3.00049, f(x) = -0.000732422
Iter 12: x = 2.99988, dx = 0.000610352, a = 2.99927, b = 3.00049, f(x) = -0.00012207
The root is 3
 f(3) = -0.0001221

Those are the values I get when I run for the above values of m, k, l ,n. At k = 1, and n = 1 and L = 3, the function reduces to f(x) = x( 1 + 1/(x + 1)) - 3 These are the values for the function f(x) = x( 1 + 1/(1 + x) )- 3 for the same values of x.

x           f(x)
2.50000	 0.21429
3.75000	 1.53947
3.12500	 0.88258
2.81250	 0.55020
2.96875	 0.71678
3.04688	 0.79978
3.00781	 0.75830
2.98828	 0.73755
2.99805	 0.74793
3.00293	 0.75311
3.00049	 0.75052
2.99927	 0.74922
2.99988	 0.74987

Clearly the values of f(x) differ in your implementation. Check the code where you calculate f(x) in your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.