Question regarding while loop

1 visualización (últimos 30 días)
xxtan1
xxtan1 el 16 de Ag. de 2020
Editada: Alan Stevens el 16 de Ag. de 2020
function y = approx_sqrt(x)
y = x;
while abs(y^2 - x) > 1e-7*x
y = (x/y + y)/2
This is part of the code in my lecture class. I wonder what is the purpose of setting y = x at the begining?
Can anyone explain to me? Thank you so much.
  1 comentario
KSSV
KSSV el 16 de Ag. de 2020
Editada: KSSV el 16 de Ag. de 2020
It is nothing but saving x into other vairable y. x is like an intital value; it is saved into y, there after value of y changes and gets updated.

Iniciar sesión para comentar.

Respuesta aceptada

Alan Stevens
Alan Stevens el 16 de Ag. de 2020
Editada: Alan Stevens el 16 de Ag. de 2020
(1) Because you need a value in the logic test: abs(y^2 - x) > ...
(2) Because you need an initial guess on the right-hand side of the Newton-Raphson expresssion: (x/y + y)/2

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by