matlab index out of bounds because numel

Can you help me with this code:
function [r, niter] = fpiter(g,x0,maxiter)
i = 1;
x(1,:) = x0;
tol = 1e-05;
while i <= maxiter
x(i+1,:) = g(x(i,:));
if abs(x(i+1,:)-x(i,:)) < tol %stopping criterion
disp('The procedure was successful after k iterations')
niter = i
disp('The root to the equation is')
r = x(i+1)
return
end
i = i+1;
end
if abs(x(i)-x(i-1)) > tol | i > N
disp('The procedure was unsuccessful')
disp('Condition |p(i+1)-p(i)| < tol was not sastified')
tol
disp('Please, examine the sequence of iterates')
x = x'
disp('In case you observe convergence, then increase the maximum number of iterations')
disp('In case of divergence, try another initial approximation p0 or rewrite g(x)')
disp('in such a way that |g''(x)|< 1 near the root')
end
Here's the g function:
function y = g(x)
y = x.^2 - 2.*x + 1;
I got this error when I'm running it with the code: fpiter('g',1, 5)
Attempted to access g(103); index out of bounds because numel(g)=1.
Error in fpiter (line 6)
x(i+1) = g(x(i));
Thank you :)

Answers (1)

Run the code as
fpiter(@g,1, 5)

5 Comments

It worked but I don't know why it took the code to finish all the iterations and then it was unsuccesful.
Here's the result: The procedure was unsuccessful Condition p(i+1)-p(i) < tol was not sastified
tol =
1.0000e-05
Please, examine the sequence of iterates
x =
1
0
1
0
1
0
In case you observe convergence, then increase the maximum number of iterations
In case of divergence, try another initial approximation p0 or rewrite g(x)
in such a way that |g'(x)|< 1 near the root
x = g(g(x)) when x = 0, 1, 3/2-(1/2)*5^(1/2), 3/2+(1/2)*5^(1/2)
If you start with x0 = 1/2 then you will get to your tolerance eventually, but it will take more than 5 steps, and g() of that will be close to 1 -- it will bounce around getting closer to 0 then to 1 as you go. And if your x0 is 3 then every step will increase in value and no root will be found.
Elvin
Elvin on 2 Mar 2013
Edited: Elvin on 2 Mar 2013
May I know what would be the "fix" that I could do to this could? By the way, here's what I'm trying to do: http://i.minus.com/inFVYTNmwZZh9.PNG
Your routine does do fixed-point iteration. However, it happens that for every quadratic that does not happen to have a certain specific relationship of coefficients, that there are two fixed point values, x = g(x), and two more fixed point values, x = g(g(x)), and four more fixed point values x = g(g(g(x))) . Some of these roots are likely to be imaginary.
In any case, as you are looking for roots of the polynomial, you should be adding the check g(x) == 0, because no matter whether the difference between x and g(x) is less than the tolerance, if g(x) is 0 then x is a root.
I've tried to change my answer to this:
function [r,niter,x] = fpiter(g,x0,maxiter)
x(1) = x0;
tol = 0.50;
for niter=1:maxiter
x(niter+1) = g(x(niter));
eval=feval(g,x(niter));
if (abs(x(niter+1)-x(niter))<=tol)|(eval==0)
break
end
end
x;
r=x(niter);
but I can't solve a quadratic equation. Can you help me change my code? Thanks

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Asked:

on 1 Mar 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!