# fixed point iteration ,help needed

• Sep 27th 2008, 04:52 PM
mithilesh
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.
• Sep 28th 2008, 12:14 AM
CaptainBlack
Quote:

Originally Posted by mithilesh
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.

Try:

Code:

```function x = fixed(g,x0,tol)   xx=x0; err=1e6;   while err>tol   x=feval(g,xx);   err=abs(xx-x);   xx=x; end```
RonL
• Sep 28th 2008, 07:44 AM
mithilesh
i get it now,thhknks for ur help buddy. appreciate it.