% Comparison of three root finding methods
% ========================================
%   
% 1. Compute the 20 iterates for each method

x(1) = 3;
y(1) = 3;
z(1) = 3;

for n = 1:19
  x(n+1) = newton_root ("f", "df", x(1), 0, n); 
  y(n+1) = secant_root ("f", 2, y(1), 0, n);
  z(n+1) = bisection_root ("f", 0, z(1), n);
end

% 2. Plot sequence index vs. the log of the error
%
% First, compute the "true" limit with a large number of iterations 

xi = newton_root ("f", "df", x(1), 0, 30); 

x_error = abs(x - xi*ones(20,1));
y_error = abs(y - xi*ones(20,1));
z_error = abs(z - xi*ones(20,1));

nn = [1:20]; % Vector with the "x" coordinates for plotting 

semilogy (nn, x_error, nn, y_error, nn, z_error);


