fixed point iteration ,help needed

Hi i m trying to write a script file in matlab,which takes as input a function f(x) and an initial estimate of the root x0 and outputs the true root of the function f(x). This is th Fixed Point iteration method in matlab.

After i have written my script,i have to test it with a function g(x)=1+1/x at x0

I have already created a script file but i m not sure where i am going wrong.

My script File is:

function x = fixed(g,x0,tol)

x0=2;

n=1;

x(1)=x0

x(2)=feval(g,x0)

while abs(x(n-1)-x(n))>tol

k=n-1

x(n)=feval(g,k);

n=n+1;

end

return

can anyone tell me,wht is wrong with this script file. or anyone provide with with an alternate matlab code for fixed point iteration method.

any sort of help will be appreciated . thnks.