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;
}