% HARDMIN  Use Newton to find minimum of f(x) = (tan x) / (x^2)
%          by solving f'(x) = 0.

% the original function
f  = @(x) x.^(-2) .* tan(x)

% draw a picture which explains starting point
x = 0:0.001:2;
figure(1)
plot(x,f(x))
axis([0 2 0 5]), grid on

% the function g(x) = f'(x); to solve g(x) = 0:
g  = @(x) x.^(-3) .* ( x .* sec(x).^2 - 2 * tan(x) )
% its derivative
dg = @(x) x.^(-4) .* ( 6 * tan(x) - 4 * x .* sec(x).^2 + ...
                         2 * x.^2 .* sec(x).^2 .* tan(x) )

% starting point
x = 0.9

% do Newton iteration
for j=1:10
  x = x - g(x) / dg(x)
end

% some numbers to check
f(x)
g(x)
