Matlab while loop complicated function. Help me figure this out.
古いコメントを表示
I need to modify the statement in the while loop so that it allows me to check the termination of the loop when the conditional is greater than delta and it is less than maxit. Run your function to compute the square root of a prime number between 50 and 100 using a delta value of 5E‐3 and maxit of 10.
function Novikov(x,delta,maxit)
%The simple square root function with added functionality
%%Synopsis: Call the function NewtonSqrt and pass a numerical value 'a'
%whose square root we need to approximate. Pass also delta and maxit for
%convergence control
it = 0;
if nargin<2,delta=5E-6;end
if nargin<3, maxit=5; end
r=x/2;
rold=x;
%Use fprintf command as a place holder
fprintf('\n the estimate for the square root of x is \n')
%disp(['The approach to sqrt(a) for a=',num2str(a)]);
%i=0;
while abs((r-rold)/rold)>delta
%while i<6
it = it + 1;
it < maxit;
rold=r; %Save old value of r for next convergence
r=0.5*(rold+x/rold);
disp(r)
%i=i+1;
end
fprintf('%14.6f \n', r)
%disp('Matlab''s value: ')
fprintf('Matlab''s value is: ')
disp(sqrt(x))
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!