0

I am trying to write a C++ program that finds the root of the following function:

`x{(1 + [(k*n)/(1 + k*x)]} - L`

using newton's method and this is the code I have and I cannot figure out what is going wrong, no matter how many iterations I put in, it never seems to converge. someone please help me!! thanks!!

```
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
enum run {TERMINATE=0, LOOP, NoRoot, HighIterations};
double f(double x, double k, double n, double l) {
return (((x*(1 + (k*n))/((1 + (k*x))))) - l);
}
double derivative(double x, double k, double n) {
return 1 + ((k*n)/((1+x)*(1+x)));
}
run newton(int NIterations, double TOL, double& x, double k, double n, double l, int summation) {
for(int iter = 1; iter <= NIterations; iter++) {
double derivativex = 1 + ((k*n)/((1+x)*(1+x)));
if(derivativex == 0.0) {
return HighIterations;
}
double deriv = -f(x, k, n, l)/derivativex;
x = x + deriv;
if(fabs(deriv) < TOL) {
return TERMINATE;
}
cout << "Iteration " << iter << ": x = " << x << ", dx = " << deriv << endl;
}
return LOOP;
double sum = 0;
for (int i = 0; i < summation + 1; i++) { //perform the function
sum = sum + (x*(1 + (k*n)/(1 + (k*x))) - l);
}
}
int main() {
double zero;
double tolerance;
double k;
double n;
double l;
int iterations;
int summ;
cout << "please enter a value for M, the number of different binding molecules: ";
cin >> summ;
for (int q = 0; q < summ; q++) {
cout << "please enter a value for k, the equilibrium constant(s): ";
cin >> k;
}
for (int z = 0; z < summ; z++) {
cout << "please enter a value for n, the total concentration(s) of the various binding molecules: ";
cin >> n;
}
cout << "please enter value, l, for the total concentration of the ligand: ";
cin >> l;
cout << "please enter a value that you may guess to be the root: ";
cin >> zero;
cout << "please enter a value for the tolerance level: ";
cin >> tolerance;
if (tolerance <= 0) {
cout << "The tolerance level must be greater than zero. Please enter a new value: ";
cin >> tolerance;
}
cout << "please enter the numer of iterations you would like to perform: ";
cin >> iterations;
run w = newton(iterations, tolerance, zero, k, n, l, summ);
switch(w) {
case TERMINATE: {
cout << "The root is " << zero << endl;
return 0;
}
case LOOP:
cout << "FAIL: Did not converge in " << iterations << " iterations!" << endl;
}
}
```

*Edited 3 Years Ago by happygeek*: fixed formatting