function [ I ] = NR( I_pv, I_0, V, Rs, Vt, a, Rp )
%NR Implements newton raphson method to solve the VI characteristic
%   Calculates the I corresponding to an input V

I = 0.0; %Our initial guess
PROPER_TOL = 1e-6;
%tol = 1e-3;
tol = PROPER_TOL;

aa = (V + Rs*I);
%display (aa);
b = aa/(Vt*a);
%display(b)
c = exp(b);
%display(c);
d=I_0*c;
%display(d);
e=aa/Rp;
%display (e);



IV = I - (I_pv - I_0*(exp((V+Rs*I)/(Vt*a)) -1) - (V+Rs*I)/Rp);
%display(IV);
k = 1;
while(abs(IV) > tol)
   dIV_dI = 1 + (I_0*Rs/(a*Vt))*exp((V+Rs*I)/(Vt*a)) + Rs/Rp;
   delta_I = -1/(dIV_dI)*IV; %The derivative wrt I times the value f(x) from the previous iteration
   I = I + delta_I;
   
  % I = I + 0.1;
   IV = I - (I_pv - I_0*(exp((V+Rs*I)/(Vt*a)) -1) - (V+Rs*I)/Rp);
   k=k+1;
end

%display(I);

end

