I'm trying to write a C++ program to implement a fixed point iteration algorithm for the equation f(x) = 1 + 5x - 6x^3 - e^2x. The problem is, I don't really know what I'm doing. I have looked around on different sites and have found this code:
#include <iostream>
#include <cmath>
usingnamespace std;
constdouble tolerance = 0.001;
double compute(double x) {
return (6*pow(x, 3.0) + exp(2*x) - 1) / 5;
}
int main() {
double x = 1.0, root = 1.5, iter = 5.0, i = 1.0;
while (true) {
x = compute(x);
if ((fabs(x - root)) < tolerance) {
break;
}
cout << "Current value: " << x << endl;
i++;
root = x;
if (i > iter) {
cout << "Method failed\nFunction diverged\n";
break;
}
}
return 0;
}
Regardless of what I change, the method will always fail. There is apparently three roots for this function. Does anyone know enough about FPI to help me understand why I'm not able to find roots with this code? I would appreciate any help.
You may have to write the equation down on some paper to see how I manipulated it to that form. Apparently, for FPI, you have to get the equation in the form of x = something. Basically, I added x to both sides, then did a little more algebra to get it into that form. I also used the pow() function from cmath lib to raise the variable to a power instead of writing x*x*x*x ...
I'm pretty sure the equation is correct, I just don't know when to stop the loop.