Question regarding while loop

1 次查看(过去 30 天)
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 个评论
KSSV
KSSV 2020-8-16
编辑:KSSV 2020-8-16
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.

请先登录,再进行评论。

采纳的回答

Alan Stevens
Alan Stevens 2020-8-16
编辑:Alan Stevens 2020-8-16
(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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by