% !! all vectors are assumed to be rows !!
% solves f(x)=b via Newton's method
% with initial guess x0
% df is a function handle that returns the differential of f
function [x, val, n]=newton(x0, delta, f, b, df, N)
% N is the maximum number of iterations
% make sure all vectors "stand up"
if(size(x0,1)==1)
    x0=x0';
end
if(size(b,1)==1)
    b=b';
end

x=x0;
for i=1:N
    % compute differential
    A=df(x);
    ff=f(x);
    if(size(ff,1)==1)
        ff=ff';
    end
    r=(ff-b);
    x=x - A\r;
    val = norm(r);
    if val< delta
        break;
    end
end
n=i;
