% This MATLAB code performs Newton's method based on the user. % The user specifies either the number of iterations to perform % or the error tolerance. See Section 4.9 of Stewart for details. stop=input(' Enter number of iterations or error tolerance--> '); if stop>1, num_steps = stop; % Perform a specified number of iterations else tolerance = stop; % Iterate until error tolerance reached end x1 = input(' Enter the initial condition, x1--> '); x = [x1]; xn = x1; if stop>1, % Perform a specified number of iterations for i=1:num_steps, xnext = xn - (xn^3-xn^2-1)/(3*xn^2-2*xn); % #6 section 4.9 x = [x; xnext]; % keep history of the estimates error = xnext-xn; % calculate the difference between consec terms xn = xnext; end xnext % display the final estimate error % display the difference between the last two estimates else done = 0; % iterate until error tolerance met steps = 0; while done==0, xnext = xn - (xn^3-xn^2-1)/(3*xn^2-2*xn); % #6 section 4.9 x = [x; xnext]; % keep history of the estimates if (abs(xn-xnext)