MATLAB: Fixed point (matematice)

fixed-pointfunctionnormtolwhilewhile loop

Hello.
I have attempt to code a fixed point in MATLAB. But I don't know if this correct, but I think it works.
function sol=fixpunkt(g,x0,tol)
xnew=x0;
xold=x0+2*tol;
while norm(xold-xnew)>tol
xnew = xnew
xnew=g(x0) ;
end
sol=xnew;
When I tried fixpunkt(1,1,5) in the command window, MATLAB works 'busy' for a long long time, I guess it is the while loop doing that,
I would be happy if someone can give a opinion about this.
Regards Cillian

Best Answer

xold never gets updated, so xnew gets set to the same value every iteration, and the loop never ends ...