function SR(a,x0,Nmax,tol); % SR is a program that estimates the square root of a % Inputs: % a: The number of interest % x0: The initial guess % Nmax: Max number of allowable iterations. % tol: max |x1-x0| (tolerance) close all; %closes all previous figures format long; X=[x0]; for i=1:Nmax; x1=.5*(x0+a/x0); disp(['x0=',num2str(x1,16), '. Convergence reached after ', num2str(i), ' iterations for tol=',num2str(tol)]); if abs(x0-x1)<=tol; k=[0:i-1]; plot(k,X); title(['Square Root of ',num2str(a),', tol=',num2str(tol)]); ylabel('x_0'); xlabel('iteration'); return; else x0=x1; X=[X,x0]; end end disp('Max number of iteration reached, convergence not achieved')