% M-file square2 to help turn column vectors into square matrices
% and clears unneeded column vectors.
% First turn the entering column vectors into matrices,
% where the number of rows is the number of observation points (NUM2)
% and the number of columns equals the number of elements.
% The column vectors are cleared once they are done with.
Uxs = reshape(Uxsv,NUM2,NUM);
clear Uxsv;
Uxn = reshape(Uxnv,NUM2,NUM);
clear Uxnv;
Uys = reshape(Uysv,NUM2,NUM);
clear Uysv;
Uyn = reshape(Uynv,NUM2,NUM);
clear Uynv;
Sxxs = reshape(Sxxsv,NUM2,NUM);
clear Sxxsv;
Sxxn = reshape(Sxxnv,NUM2,NUM);
clear Sxxnv;
Syys = reshape(Syysv,NUM2,NUM);
clear Syysv;
Syyn = reshape(Syynv,NUM2,NUM);
clear Syynv;
Sxys = reshape(Sxysv,NUM2,NUM);
clear Sxysv;
Sxyn = reshape(Sxynv,NUM2,NUM);
clear Sxynv;
% Next, sum the entries in each row to superpose
% the contributions of all the elements.
% This step will yield column vectors
% See p. 488 of the Matlab manual for this
Uxs = sum(Uxs')';
Uxn = sum(Uxn')';
Uys = sum(Uys')';
Uyn = sum(Uyn')';
Sxxs = sum(Sxxs')';
Sxxn = sum(Sxxn')';
Syys = sum(Syys')';
Syyn = sum(Syyn')';
Sxys = sum(Sxys')';
Sxyn = sum(Sxyn')';
% Now, reshape the column vectors into matrices
% that have the dimensions of the observation grid,
% adding the contributions from the shear and
% normal displacement discontinuities
Ux = reshape(Uxs,dimx,dimy) + reshape(Uxn,dimx,dimy);
Uy = reshape(Uys,dimx,dimy) + reshape(Uyn,dimx,dimy);
Sxx = reshape(Sxxs,dimx,dimy) + reshape(Sxxn,dimx,dimy);
Syy = reshape(Syys,dimx,dimy) + reshape(Syyn,dimx,dimy);
Sxy = reshape(Sxys,dimx,dimy) + reshape(Sxyn,dimx,dimy);