[Math] Can’t find all roots to function with fixed point method

fixed-point-theoremsMATLABnumerical methods

I have a function $f(x) = x^2-8x-10\cos(2x)+15$ and I'm supposed to via Matlab find all roots for this function. I can see graphically that it has $4$ roots and I've written a matlab function to find a root. When the absolute difference between $x_n$ and $x_{n+1}$ is smaller than $10^{-10}$ the function returns $x_{n+1}$ as the root.

Since it has $4$ roots I call the function $4$ times, with different starting values each time, all of them being close to some root.

The problem is this: the 1st and 3rd root are found. And the 2nd and 4th roots aren't. Trying to find the 2nd root returns me the value of the 1st root, and trying to find the 4th root just loops telling me $x_n$ is NaN.

Even when the start value when I try to find the 2nd root is the root, it still returns the 1st root.

This is for a school lab and it has to be found with a fix-point Method I've been given:

x_n = (1/15)x^2+(7/15)x-(2/3)cos2x+1

My function for finding roots looks like this

function root = fixpoint(x, T)

while true
    x_n = 1/15*(x^2)+7/15*x-2/3*cos(2*x)+1;
    error = abs(x_n-x);
    disp(['Error: ', num2str(error),' X_n: ', num2str(x_n)])

    if error < T
        break
    end

    x = x_n;

end

root = x_n;

end

And I call it 4 times with:

errorMargin = 10^-10;
p1 = fixpoint(2, errorMargin);
p2 = fixpoint(3.977, errorMargin);
p3 = fixpoint(5, errorMargin);
p4 = fixpoint(7, errorMargin);  

And I get: p1= 2.429(correct), p2= 2.429(actual is 3.977), p3= 5.571(correct), p4 loops 'NaN'

What do I need to do to find all $4$ roots?

Best Answer

Your problem is that fixed point iteration only converges when the derivative of the right hand side is less than $1$ in absolute value. If you linearize the problem, the error is multiplied by the derivative of the RHS at each step. If that derivative is greater than $1$ in absolute value, you will get farther and farther from the root. The value of the derivative at $3.977$ is about $2.3$ You don't say what the fourth root is, but you started at $7$ and the derivative looks to be greater than $1$ there as well. A plot of the derivative from Alpha is below.
Plot from Alpha

Related Question