function X = newton3(F, X0, ITERATIONS)
%NEWTON3(F, X0, ITERATIONS)
% Estimate zeros of F by Newton's method.
%
% F should be a column vector n high of
%  symbolic expressions with n variables.
%
% X0 is an initial guess, a real column n-vector.
%
% ITERATIONS is the number of iterations to compute.
%
% EXAMPLE:
%  syms x y; newton2([cos(x) - x; sin(y)], [0.1; 3.0], 3)

F = sym(F);         % in case user inputs a string
vars = symvar(F);   % vector of variables in alphabetical order

% assuming here that findsym and symvar return variables in same order:
disp(['Looking for ' findsym(F) ' that make the following function zero:'])
disp(F)

J = jacobian(F, vars);
X = X0;
for i = 1:ITERATIONS
    JJ = double(subs(J, vars, X.'));
    FF = double(subs(F, vars, X.'));
    X = X - inv(JJ) * FF;
    disp(['Iteration ' num2str(i) ':'])
    disp(X)
end
