function K = truss2d(A,E,P1,P2)
%   TRUSS2D
%
%   This command generates the 4 x 4 stiffness matrix for a planar
%   truss element in global coordinates. The syntax is:
%              K = truss2d(A,E,P1,P2)
%   where: A is the cross-sectional area;
%          E is the Young's modulus;
%          P1 and P2 are vectors of the {x,y} coordinates of the
%              endpoints.

l = norm(P2-P1);
alpha = atan2(P2(2)-P1(2),P2(1)-P1(1));
lamb = [cos(alpha) sin(alpha) -cos(alpha) -sin(alpha)]';
K = A*E/l*lamb*lamb';
