can someone please help me?! I have been working on this C++ project forever and I can't seem to get it. I am trying to write a program that will find the root of the following equation using the bisection method:
x(1 + [summation from 1 to M of (Kj*Nj)/(1 + Kj*x)]) - L = 0
where the user inputs a value for M, the different values of Kj and Nj and one value for L.
i.e. I would input M=2, K1=1, N1=1, K2=2, N2=.5, L=3, etc. I would be soooo appreciative of any help! Thanks!
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main () {
double a, b, TOL, maxiterations;
double k[20], n[20], L, M, FA, FP;
cout << "enter M: " << endl;
cin >> M;
for (int g = 0; g < M; g++) {
cout << "enter k: " << endl;
cin >> k[g];
cout << "enter n: " << endl;
cin >> n[g];
}
cout << "enter L: " << endl;
cin >> L;
cout << "enter a: " << endl;
cin >> a;
cout << "enter b: " << endl;
cin >> b;
cout << "enter tolerance: " << endl;
cin >> TOL;
cout << "enter iterations: " << endl;
cin >> maxiterations;
double summationa = 0;
double summationp = 0;
for (int h = 0; h < M; h++) {
summationa += ((k[g] * n[g])/(1.0 + (k[g] * a)));
double p = a + ((b - a)/2);
summationp += ((k[g] * n[g])/(1.0 + (k[g] * p)));
for (int i = 1; i <= maxiterations; i++) {
//double p = a + ((b - a)/2);
FP = (p * (1 + summationp)) - L;
if (FP = 0 || (b - a)/2 < TOL) {
cout << "root is " << p << endl;
return 0;
}
i = i + 1;
FA = (a * (1 + summationa)) - L;
if ((FA * FP) > 0) {
a = p;
}
else {
b = p;
}
}
}
cout << "failed after " << maxiterations << " iterations." << endl;
return 0;
}