function [e] = errorI(M,data)

e = 0;
N = size(data,1);

Vd = data(:,1);
Id = data(:,2);
Vg = data(:,3);

KP = M(1);
VT0 = M(2);
LAMBDA = exp(M(3));
RS = exp(M(4));
RD = exp(M(5));

Vds = Vd - Id.*(RS+RD);
Vgs = Vg - Id.*RS;

for i = 1:N
        if (Vds(i)) < (Vgs(i) - VT0)    %Linear region
            I = KP*(Vgs(i) - VT0 - (Vds(i)/2))*Vds(i)*(1 + LAMBDA*Vds(i));
            abs = (I - Id(i));
            e = e + (abs^2);
        else                            %Saturation region
            I = (KP/2)*((Vgs(i) - VT0)^2)*(1 + LAMBDA*Vds(i));
            abs = (I - Id(i));
            e = e + (abs^2);
        end
end

