why is my Fixed point iteration method only giving me first iteration. (i included info at the bottom of code and the next few iterations answers)

Asked by Erik Ramirez

Erik Ramirez (view profile)

on 15 Sep 2019 at 5:22
Latest activity Commented on by Erik Ramirez

Erik Ramirez (view profile)

on 15 Sep 2019 at 17:18
Accepted Answer by John D'Errico

John D'Errico (view profile)

x= sym('x');
tol=.00001;
n_0=input('Plese input your number of iterations: ');
p_0=input('Plese input your initial approximation: ');
F = input('Plese input your equation: ');
%g = (10/(4+x))^(1/2);
N=0;
p=(subs(F,x,p_0));
while(N <= n_0 && not(abs(p-p_0) <= tol ))
N=N+1;
F=p;
p_0=p;
display(double(p_0))
end
% iterations= 30
% initial approxiation= 1.5
% equation= (10/(4+x))^(1/2)
% iterations: 1.5, 1.348399725, 1.367376372, 1.364957015, 1.365264748, ...

1 Comment

Dimitris Kalogiros

Dimitris Kalogiros (view profile)

on 15 Sep 2019 at 7:58
What do you intend to achieve with this code?
What is the goal ? Solving an equation? which is the equation ? Release

R2019b

Answer by John D'Errico

John D'Errico (view profile)

on 15 Sep 2019 at 8:23

Think about it. Does p EVER change? Why not? How would you make it change?
Inside the loop, we see only this:
N=N+1;
F=p;
p_0=p;
display(double(p_0))
p starts out as whatever number is it. Does it EVER change? (NO.)
When you do this:
F=p;
Here, I think you think this evaluates the FUNCTION every time. It does not. p is a number. It sets F to be the number p.
Therefore, p_0 = p. ALWAYS. Now, what does your test do? It terminates the loop when p0-p is a small enough number. Zero certainly satisfies the requirement. So the loop runs for one iteration, then quits.
So, change your code to re-evaluate the function inside F. Do not overwrite the variable F.

Erik Ramirez

Erik Ramirez (view profile)

on 15 Sep 2019 at 17:18
Thank you so much!!!