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 visualización (últimos 30 días)
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 comentario
Dimitris Kalogiros
Dimitris Kalogiros el 15 de Sept. de 2019
Editada: Dimitris Kalogiros el 15 de Sept. de 2019
What do you intend to achieve with this code?
What is the goal ? Solving an equation? which is the equation ?
eq.png

Iniciar sesión para comentar.

Respuesta aceptada

John D'Errico
John D'Errico el 15 de Sept. de 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.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by