% HW 3 Problem 2

% SAMPLE CODE WITH A DIFFERENT LIMIT AND ITERATION
%
% Assumes that the following have been implemented:
%
%  fixedpoint.m - fixed point iteration
%  steffensen.m - fixed point with Steffensen's acceleration

% TO BE MODIFIED

% square root of 2 can be approximated by the fixed point iteration
% x_{n+1} = g(x_n), with g(x) as follows
g = @(x) (10*x+2/x)/11;

x0    = 1;   % initial guess
maxit = 100;  % max number of iterations
tol   = 1e-4;% tolerance

xs1 = fixedpoint(g,x0,maxit,tol);
xs2 = steffensen(g,x0,maxit,tol);
fprintf('%10s %13s %13s\n', ' ','fixed point','Steffensens');
fprintf('%10s %13.8f %13.8f\n','root',xs1(end),xs2(end));
fprintf('%10s %13d %13d\n','iter',length(xs1),length(xs2));
% for Steffensen's 2 out of 3 iterations are function evaluations.
fprintf('%10s %13d %13d\n','g evals',length(xs1)-1,2*(length(xs2)-1)/3);
fprintf('true value: sqrt(2) = %13.8f\n',sqrt(2));

% Sample output:
%
%   >> prob2_sample
%                fixed point   Steffensens
%         root    1.41384351    1.41421356
%         iter            35            13
%      g evals            34             8
%   true value: sqrt(2) =    1.41421356

