%***********************************************************************
%  function ElemStiff2DTwoNodeBar
%
%  calculates the stiffness matrix of a 2D bar element
%***********************************************************************

function Ke = ElemStiff2DTwoNodeBar(xi, yi, xj, yj, Em, A)
% determine some element properties
dx = xj - xi;
dy = yj - yi;
L = sqrt( dx^2 + dy^2 );
c = dx/L;  s = dy/L;
cc = c^2; sc = s*c; ss = s^2;

Ke = (Em*A/L) * [ cc  sc -cc -sc;
                  sc  ss -sc -ss;
                 -cc -sc  cc  sc;
                 -sc -ss  sc  ss ];
                  
% Test code and output:
%
% Ke = ElemStiff2DTwoNodeBar(0, 0, 10, 10, 100, 2*sqrt(2))
%
% Ke =
%
%   10.0000   10.0000  -10.0000  -10.0000
%   10.0000   10.0000  -10.0000  -10.0000
%  -10.0000  -10.0000   10.0000   10.0000
%  -10.0000  -10.0000   10.0000   10.0000             
