% Finding the square root of a number using an iterative formula % EXERCISE#1 % Using fixed number of iterations R=4900; xold=-6.0; nmax=120 for i=1:1:nmax xnew=0.5*(xold+R/xold); xold=xnew; end % EXERCISE#2 % Checking for tolerance using the for loop! R=4900; xold=-6.0; nmax=120 tol=0.00005; for i=1:1:namx xnew=0.5*(xold+R/xold); epsa=abs((xnew-xold)/xnew)*100 if epsa<=tol break; end xold=xnew; end i xnew epsa % EXERCISE#3 % Checking for tolerance using the while loop! R=4900; xold=-6.0; tol=0.00005; nmax=120; % initializing the loop variable i=0; % initializing epsa to be more than the tolerance so that the loop works % the first time epsa=2*tol %comparing for meeting the tolerance and the maximum number of iterations while epsa>=tol & i<=nmax xnew=0.5*(xold+R/xold); epsa=abs((xnew-xold)/xnew)*100; xold=xnew; i=i+1; end i xnew epsa % EXERCISE#4 % Do it now!