% EE362G Smart Grids
% Script for State Estimation on 2-bus network with SCADA Measurements

% Measurement vector
z=[1.00; 0.99; -1.56; 0.01];

% Desired tolerance
tol=1e-3;

% Initialization
x=[1; 0.99; 0];

% Maximum number of iterations 
maxIter=50;

% Initilization of iteration index
j=1;

% Main loop
while j<=maxIter
    xprev=x;
    
    J=Jac(x);
    
    x=x+(J'*J)\(J'*(z-hfn(x)));
    %keyboard % Uncomment to go step-by-step (use "dbcont" in command line to continue)
    if  norm(x-xprev)<=tol
        break;
    end
    j=j+1;
end
% Upon exit, x=system state