%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [K,R,I]=assm_sys(edof,K,Ke,R,Re,I,Ie)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% assemble local element contributions to global stiffness and force
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% input:  edof  = [ elem X1 Y1 X2 Y2 ]   ... incidence matrix
%         Ke    = [ nedof x nedof ]      ... element tangent  Ke 
%         Re    = [ nedof x 1 ]          ... element residual Re
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% output: K     = [ ndof x ndof ]        ... global tangent   K
%         R     = [ ndof x 1 ]           ... global residual  R
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[nie,n]=size(edof);

I(edof(1),:)=Ie(:);

t=edof(:,2:n);
for i = 1:nie
  K(t(i,:),t(i,:)) = K(t(i,:),t(i,:))+Ke;
  R(t(i,:))         =R(t(i,:))       +Re;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
