Question regarding while loop
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
xxtan1
am 16 Aug. 2020
Bearbeitet: Alan Stevens
am 16 Aug. 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 Kommentar
Akzeptierte Antwort
Alan Stevens
am 16 Aug. 2020
Bearbeitet: Alan Stevens
am 16 Aug. 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!