Use the function file (provided) as a starting template to accomplish the follow
ID: 642755 • Letter: U
Question
Use the function file (provided) as a starting template to accomplish the following criteria:
b) insert the long format command into the function.
c) Introduce a variable called 'it' outside the loop and initialize to 0.
d) within the loop update 'it' by 1.
e) modify the statement in the while loop so that it allows you to check the termination of the loop when the conditional is greater than delta and 'it' is less than maxit.
f) run the function to compute the square root of a prime number between 120 and 175 using a delta value of 5E-3 and maxit of 10.
g) repeat step 6 with a delta value of 5E-9 and maxit of 12.
Explanation / Answer
Hi
function NewtonSqrtCS3(x, delta, maxit)
if nargin<2, delta=5E-6; end
if nargin<3, maxit=5; end
r=x/2;
rold=x;
fprintf(' the estimate for square root of x is ')
%disp('the approach to sqrt(a) for a=', num2str(a)]);
%i=0;
%it=0;
while (abs((r-rold)/rold)>delta))&& (it<maxit))
%while i<6
rold=r; %Save old value of r for next convergence
%it=it+1;
r=0.5*(rold+x/rold);
disp(r)
%i=i+1;
end
fprintf('%14.6f ' ,r)
%disp('Matlab' 's valu')
fprintf('Matlab' 's value is: ')
disp(sqrt(x))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.