% % Written by: % -- % John L. Weatherwax 2009-04-21 % % email: wax@alum.mit.edu % % Please send comments and especially bug reports to the % above email address. % % Problem on EPage 62; Solutions on EPage 161 % %----- close all; clc; clear all; addpath('../../BookCode'); a = 100.112; tol = 1.e-6; % First iteration method: % xk = 1000; xkp1 = (xk + a/xk)/2; it = 1; x_iters = [xk,xkp1]; d = abs(xkp1 - xk); while d > tol , xk = xkp1; xkp1 = (xk + a/xk)/2; d = abs(xkp1 - xk); x_iters = [ x_iters, xkp1 ]; end ph1 = plot( x_iters, '.-r' ); grid('on'); hold('on'); ph_truth = plot( sqrt(a)*ones( 1, length(x_iters) ), '-g' ); % Second iteration method: % xk = 1000; xkp1 = (xk + a/xk)/2 - (xk - a/xk)^2 / (8*xk); it = 1; x_iters = [xk,xkp1]; d = abs(xkp1 - xk); while d > tol , xk = xkp1; xkp1 = (xk + a/xk)/2 - (xk - a/xk)^2 / (8*xk); d = abs(xkp1 - xk); x_iters = [ x_iters, xkp1 ]; end ph2 = plot( x_iters, '.-b' ); legend( [ph1,ph2,ph_truth], {'iterates method 1', 'iterates method 2','truth'} ); %saveas( gcf, '../../WriteUp/Graphics/Chapter3/c_3_p_17_plot.eps', 'epsc' );