%This code implements the Newton-Raphson method to solve a vector valued
%system of equations that satisfy the condition f(x1,x2,...,xk)=0. Written
%by Philip Shaw, Fordham University, 2022.
function[xstar]=NR(fx,x0)
Jx=CDJac(fx,x0); %calculate the Jacobian based upon the initial guess for the solution.
dif=1; %initialize difference for eps1
eps1=10^(-5); %chosen tolerance for function value
it=1; %define count variable
while (dif>eps1)
    x1=x0'-inv(Jx)*fx(x0); %based on x0 compute x1
    x0=x1'; %update the new value for x0
    dif=max(abs(fx(x0))); %calculate the maximum absolute value for function value
    
    
    difstore(it,1)=dif;
    %pause(.1)
    %plot(difstore)
    it=it+1; %update counter
    Jx=CDJac(fx,x0); %calculate the Jacobian based upon the new value for x0.
end
xstar=x1; %return the optimal value

    