% WARNING!  This is for f(x) = (x+1)(x-1)^2.  If you're going to
% change the function, and its derivative in _two_ places!
%
% [root,value,steps,A] = secant(x_now,tol) reads in a first guess
% x_now and a desired tolerance tol.  it
% takes steps until it gets |f(root)| < tol.  It then returns the root,
% the value of the function at the root, and the number of steps it
% took to find it

% A is a diagnostic matrix.  At the moment, it's 100x3 and contains
% A(i,1) = i, A(i,2) = x_i, A(i,3) = f(x_i)

function [root,value,steps,A] = secant0(x_now,tol)

steps = 0;

A = zeros(100,3);


% you'll have to change f and f_x here!!!
    f_now = (x_now+1)*(x_now-1)^2;
    fx_now = (x_now-1)^2 + 2*(x_now+1)*(x_now-1);
    x_prev = x_now ;
    f_prev = f_now ;
% now take a Newton step:
    x_now = x_now - f_now/fx_now;		
    steps = steps + 1;
    steps, f_now

while ( abs(f_now) > tol) & (steps < 100) 

% evaluate f at the present point (really it's x_i, but we
% call it x_now
% this is where you'll have to change f!!!
    f_now = (x_now+1)*(x_now-1)^2;

% now use this to define the next point (really it's x_(i+1), but we
% call it x_now
    x_temp = x_now - f_now*(x_prev-x_now)/(f_prev-f_now);

    steps = steps + 1;
    x_prev = x_now;
    f_prev = f_now;
    x_now = x_temp

    A(steps,1) = steps;
    A(steps,2) = x_now;
    A(steps,3) = f_now;	  

% print to the screen the step number and the function size
    steps, f_now

end

root = x_now;
value = f_now;	
