function [ x,it ] = newton2d(x0,tol)
%UNTITLED2 Summary of this function goes here
%   Detailed explanation goes here
x = x0;    
error = norm(f(x));

xp=linspace(-1,1);yp=linspace(-1,1);
plot(xp,xp.^2,yp.^2,yp,xp,1./4./xp);axis([-1 1 -1 1])
hold on;

it = 0;
xprev = x;
while norm(error) > tol
    fx = f(x);
    dfx = df (x);
   x,fx,dfx,
    s = dfx\fx,
    x = x - s; 
    it = it + 1;
    error = norm(f(x));
    %% print current information
    format long;[it,error], format;
    plot([x(1),xprev(1)],[x(2),xprev(2)],'-.');
    pause;
    
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function v = f(x)
  v = [x(1).^2-x(2);-x(1)+x(2)^2];
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function v = df (x)
  v = [2*x(1),-1;-1,2*x(2)];
end


