function B = curl(x,e,u)

% B = curl(x,e,u)
%          calculates the curl of the scalar potential u
%          defined at the nodes x of the triangulation e

%sprintf('%s','curl')

n = max(size(e));

B = zeros(2,n);

for i=1:n
    ei = e(i,:);
    
    x1 = x(:,ei(1));
    x2 = x(:,ei(2));
    x3 = x(:,ei(3));
    
    R = [x2-x1 x3-x1];
    invR = inv(R);
        
    u1 = u(ei(1));
    u2 = u(ei(2));
    u3 = u(ei(3));
    
    B(1,i) = (u2-u1)*invR(1,2) + (u3-u1)*invR(2,2);
    B(2,i) = - ( (u2-u1)*invR(1,1) + (u3-u1)*invR(2,1) );
end
