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)
Show older comments
Erik Ramirez
on 15 Sep 2019
Commented: Erik Ramirez
on 15 Sep 2019
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
on 15 Sep 2019
Edited: Dimitris Kalogiros
on 15 Sep 2019
What do you intend to achieve with this code?
What is the goal ? Solving an equation? which is the equation ?
Accepted Answer
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)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!