### Fixed point iteration help

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:

 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include using namespace std; const double 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.
`return (6*pow(x, 3.0) + exp(2*x) - 1) / 5;` this is not anywhere near
 1 + 5x - 6x3 - e2x

Are you sure you don't want something like:
`1 + 5*x - 6*x*x*x - std::exp(2*x);`
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.
Integer to an Integer power pow is not as accurate or as fast.

Anyways what I was saying is you did:

(6x3 + e2x - 1)/5 which I have no idea how you got that out of 1 + 5x - 6x3 - e2x

Lets compare yours

x = 2
 ``` 6*23 + e2*2 - 1 --------------- 5```
 ```6*23 + e4 - 1 ------------- 5```
 ```6*8 + 54.5982 - 1 ----------------- 5```
 ``` 48 + 54.5982 - 1 ---------------- 5```
 ```101.5982 -------- 5```
 `20.3196`

with the original
x = 2

 `1 + 5*2 - 6*23 - e2*2`
 `1 + 5*2 - 6*23 - e4`
 `1 + 5*2 - 6*8 - 54.5982`
 `1 + 10 - 48 - 54.5982`
 `-91.5982`

Anyways you are missing your x it was 5x when you divide by 5 you can't simply remove x.

Oh I see, You did x = .. sorry I missed the
 x = something

Basically what I would do is:

 ```0 = 1 + 5x - 6x3 - e2x -5x = 1 - 6x3 - e2x x = (1 - 6x3 - e2x)/-5 x = (-1 + 6x3 + e2x)/5 xn+1 = (-1 + 6xn3 + e2xn)/5```

so with you example x = 1 and there are 5 iterations you would do

 ``` x1 = (-1 + 6*13 + e2*1)/5 ~ 2.4778 x2 = (-1 + 6x13 + e2x1)/5 ~ 46.4485 X3 = (-1 + 6x23 + e2x2)/5 ~ 4.4226694457891073431360201775296e+39 x4 = ... x5 = ...```

The code would look something like:

 ``12345678`` ``````double xn = 1; //inital const int iterations = 5; for(int x = 0; x < iterations; ++x) { xn = (-1 + 6*xn*xn*xn + std::exp(2*xn)) / 5.0; } std::cout << "result: " << xn << std::endl;``````

Though I would iterate until it gets close to the convergence and not a set amount of iterations.

http://www.youtube.com/watch?v=OLqdJMjzib8
Last edited on
Topic archived. No new replies allowed.