MATLAB Answers

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)

1 view (last 30 days)
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

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 15 Sep 2019
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.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019b