I not able to understand what type error I am making in the while loop
    5 ビュー (過去 30 日間)
  
       古いコメントを表示
    
I want to use babyloniansqrt to work if Error value should be grater than threshold and this will update the x and E value, using xnext = (x+S./x)/2,  E = abs(S-xnext.^2) formula. Please check my code and let me know what I am doing wrong
function out = babyloniansqrt(S,threshold)
x = 1;
if ~exist('threshold','var')
	threshold = 0.00001;
end
E = abs(S-x.^2);
while ~(E<threshold)
	xnext = (x+S./x)/2;
	E = abs(S-xnext.^2);
end
0 件のコメント
回答 (1 件)
  Rik
      
      
 2021 年 11 月 4 日
        
      編集済み: Rik
      
      
 2021 年 11 月 6 日
  
      You aren't updating the value itself, so the calculation is the exact same on every iteration.
function out = babyloniansqrt(S,threshold)
%Write lead line here
%
% write usage instructions and explanation here
x = 1;
if nargin<2
    %set default threshold if not provided
	threshold = 10^-5;
end
E = inf; %set initial value to enter the loop
while E>threshold
	x = (x+S./x)/2;
	E = abs(S-x.^2);
end
out=x;
end
3 件のコメント
参考
カテゴリ
				Help Center および File Exchange で Performance and Memory についてさらに検索
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


