// Question 1

function z=J(x,y)
    z=(x-1)^2+(x^2-y)^2+3
endfunction

// Question 2

function G=Grad(x,y)
    G=[2*(x-1)+4*x*(x^2-y) ; -2*(x^2-y)]
endfunction

// Question 3

function D=Hess(x,y)
    D=[2+12*x^2-4*y , -4*x ; -4*x , 2]
endfunction

// Question 4

function R=Newton(x0,y0,eps)
    X=[x0]
    Y=[y0]
    Z=[x0;y0]
    
    G=Grad(Z(1),Z(2))
    
    while G(1)^2+G(2)^2 > eps
        Z=Z-Hess(Z(1),Z(2))^(-1)*Grad(Z(1),Z(2))
        X=[X,Z(1)]
        Y=[Y,Z(2)]
        G=Grad(Z(1),Z(2))
    end
    R=[X;Y]
endfunction
