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 日

0 投票

(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

1 件のコメント

xxtan1
xxtan1 2020 年 8 月 16 日
Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 8 月 16 日

編集済み:

2020 年 8 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by