function I = objectiveFunc(xpl,xpt,x,e,ep,B,Breq)

% I = objectiveFunc(xpl,xpt,x,e,ep,B,Breq)
%        calculates the following cost functional:
%        I(B) = (1/meas(Omega_p)) * \int_{Omega_p} |B(x)-Breq|^2 dx

%sprintf('%s','objectiveFunc')

area = abs(xpl)*abs(xpt);
np = max(size(ep));
I = 0;

for i=1:np
    ei = e(ep(i),:);
    
    x1 = x(:,ei(1));
    x2 = x(:,ei(2));
    x3 = x(:,ei(3));
    
    R = [x2-x1 x3-x1];
    areai = abs(det(R))/2;
    
    I = I + power(norm(B(:,ep(i))-Breq),2)*areai;
end

I = I/(area*power(norm(Breq),2));
