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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops 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