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 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Erik Ramirez
le 15 Sep 2019
Commenté : Erik Ramirez
le 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 commentaire
Dimitris Kalogiros
le 15 Sep 2019
Modifié(e) : Dimitris Kalogiros
le 15 Sep 2019
What do you intend to achieve with this code?
What is the goal ? Solving an equation? which is the equation ?
Réponse acceptée
John D'Errico
le 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!