function out=m164hw6(f1,f2,x0)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% to run this program first type 
% syms x y
% then enter input functions as symbolic functions and initial guess as a column matrix for example
% m164hw6(3*x^3-4*x*y^2-7*x*y+3,y^3+3*x^2*y-4*x^3,[1;2])
%
% Performs newtons method calculation for f=[f1(x,y);f2(x,y)] with initial
% guess x0 until the value of the function is within tol of zero or number of steps grows to
% MaxSteps
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tol=1e-6; %tolerance for how small f(x_k) should be before it stops 
MaxSteps=20; %Maximum number of steps before it quits and decides that it diverges
syms x y

Jacobian=transpose([diff(f1,'x'),diff(f2,'x');diff(f1,'y'),diff(f2,'y')]);
xk=x0;
steps=1;
fnum=[subs(subs(f1,y,xk(2),0),x,xk(1),0);subs(subs(f2,y,xk(2),0),x,xk(1),0)];
while steps<MaxSteps && not(all(abs(fnum) < tol))
    JacobianNum=subs(subs(Jacobian,y,xk(2),0),x,xk(1),0);
    fnum=[subs(subs(f1,y,xk(2),0),x,xk(1),0);subs(subs(f2,y,xk(2),0),x,xk(1),0)];
    if det(JacobianNum)==0
        out='Jacobian is singular, try different input value';
        return
    else
        p=-inv(JacobianNum)*fnum;
        xk=xk+p;
        steps=steps+1;
    end
end
if steps==MaxSteps
    out='To many steps taken. does not converge?';
else    
    out=xk;
end
end
