Professional Documents
Culture Documents
- REPORT (TRUSS2D) -
0. RUNNER FILE
The runner file Runner is a MATLAB m-file which evaluates the input file and
initiates the main MATLAB m-file. In this section input file must be specified by
name.
1. MAIN FILE
The main file Truss2D is a MATLAB m-file which assembles the global stiffness
matrix of a planar truss in order to calculate joint displacements, reactions and bar
forces of the system by using various functions which will be presented below.
clc
jntpel=2;
dofpjnt=2;
totdof=jnts*dofpjnt;
[gdof,k,kk]=zeromtrx(totdof,jntpel,dofpjnt);
% coordinates of joints
% x1, x2 and y1, y2
y1=jcoords(j1,2);
x2=jcoords(j2,1);
y2=jcoords(j2,2);
L(iel)=(sqrt((x2-x1)^2+(y2-y1)^2))*1000;
l=L(iel);
ex=[x1 x2];
ey=[y1 y2];
jd=[j1 j2];
End
fark=length(cnsval)-sum(cnsval);
i=1;
j=1;
for k=1:length(cnsval)
if cnsval(k)==0
cnsvalN(i)=k;
i=i+1;
else
cnsvalN(j+fark)=k;
j=j+1;
end
end
for i=1:length(cnsval)
for j=1:length(cnsval)
kk_new(i,j)=kk(cnsvalN(i),cnsvalN(j));
end
end
%% PRINTING ALL STIFFNESS MATRICES TO SCREEN
disp('Global Stiffness Matrix of the Truss "kk" is:')
disp(' ')
disp(kk)
disp('Rearranged Global Stiffness Matrix of the Truss "kk_new" is:')
disp(' ')
disp(kk_new)
%% FORCE VECTOR REARRANGED & DETERMINATION OF STIFFNESS MATRIX FOR DISPLACEMENT
for i=1:fark
ff_new(i)=ff(cnsvalN(i));
for j=1:fark
k(i,j)=kk_new(i,j);
end
end
%% DETERMINATION OF STIFFNESS MATRIX FOR REACTION FORCES
m=1;
for i=(fark+1):length(cnsval)
n=1;
for j=1:fark
k1(m,n)=kk_new(i,j);
n=n+1;
end
m=m+1;
end
%% CALCULATION OF DISPLACEMENTS (@FREE NODES) AND REACTION FORCES (@SUPPORTS)
defrm=k\ff_new';
% Displacement
React=k1*defrm;
% Reaction
j=1;
for i=1:length(cnsval)
if cnsval(i)==1
deform(i)=0;
else
deform(i)=defrm(j);
j=j+1;
end
end
for i=1:jnts
X(i)=deform((i-1)*2+1);
Y(i)=deform((i-1)*2+2);
end
for i=1:jnts
j=1;
dfrm(i,j)=X(i);
j=j+1;
dfrm(i,j)=Y(i);
end
dfrm=sfactor*(dfrm/1000);
jdeformed=jcoords+dfrm;
%% BAR FORCE
[F]=barforce(elmnts,elends,jcoords,jntpel,dofpjnt,E,A,deform);
display(' ')
for i=1:elmnts
fprintf('Bar force of Element #%g is %g kN \n',i,F(1,i))
end
%% DRAWING DEFORMED AND UNDEFORMED TRUSS ELEMENT. THIS OPEARTION IS FOR DRAWING THE
INITIAL AND THE DISPLACED GEOMETRY OF THE TRUSS.
for i=1:jnts
axis on
axis equal
grid on
plot(jcoords(i,1),jcoords(i,2),'.','Color','k')
end
for j=1:elmnts
X1=jcoords(elends(j,1),1);
X2=jcoords(elends(j,2),1);
Y1=jcoords(elends(j,1),2);
Y2=jcoords(elends(j,2),2);
line([X1 X2],[Y1 Y2],'Color','k','LineWidth',2)
hold on
end
for j=1:elmnts
X1=jdeformed(elends(j,1),1);
X2=jdeformed(elends(j,2),1);
Y1=jdeformed(elends(j,1),2);
Y2=jdeformed(elends(j,2),2);
line([X1 X2],[Y1 Y2],'Color','r','LineWidth',2)
hold off
end
%%
*****
NOTICE: When the code is completed for all the elements, global stiffness matrix of
the planar truss, joint displacements, reactions and bar forces will be shown in the
MATLAB screen.
2. FUNCTIONS
2.1. zeromtrx
This function generates zero matrices which will be then filled by calculated values.
function [index,k,kk]=zeromtrx(totdof,jntpel,dofpjnt)
index=zeros(jntpel*dofpjnt,1);
k=zeros(jntpel*dofpjnt,jntpel*dofpjnt);
kk=zeros(totdof,totdof);
2.2. connectivity
This function generates a connectivity vector which will be used for the global
stiffness matrix calculations for the element that is being considered.
function [gdof]=indxvec(jd,jntpel,dofpjnt)
k=0;
for i=1:jntpel
start=jd(i)*dofpjnt;
for j=1:dofpjnt
k=k+1;
if mod(j,2)~=0
gdof(k)=start-1;
else
gdof(k)=start;
end
end
end
2.3. elk
This function calculates the local stiffness matrix for the element that is being
considered and converts it to the global coordinate system.
function [k]=elk(ex,ey,E,A,L)
c=(ex(2)-ex(1))*1000/L;
s=(ey(2)-ey(1))*1000/L;
k=(E*A/L)*[c^2 c*s -c^2 -c*s; c*s s^2 -c*s -s^2;...
-c^2 -c*s c^2 c*s; -c*s -s^2 c*s s^2];
2.4. globalk
This function constructs the global stiffness matrix of the element by use of the
values calculated by the elk function, afterwards, locates the values to the global
stiffness matrix which is generated as a zeroes-matrix by the zeromtrx function.
function [kk]=globalk(kk,k,gdof)
edof=length(gdof);
for i=1:edof
ii=gdof(i);
for j=1:edof
jj=gdof(j);
kk(ii,jj)=kk(ii,jj)+k(i,j);
end
end
2.5. barforce
This function calculates the internal bar forces of truss members.
function [F]=barforce(elmnts,elends,jcoords,jntpel,dofpjnt,E,A,deform)
for iel=1:elmnts
j1=elends(iel,1);
j2=elends(iel,2);
x1=jcoords(j1,1);
y1=jcoords(j1,2);
x2=jcoords(j2,1);
y2=jcoords(j2,2);
e=E(iel);
a=A(iel);
L=(sqrt((x2-x1)^2+(y2-y1)^2))*1000;
ex=[x1 x2];
ey=[y1 y2];
jd=[j1 j2];
c=(ex(2)-ex(1))*1000/L;
s=(ey(2)-ey(1))*1000/L;
[gdof]=connectivity(jd,jntpel,dofpjnt);
[k]=elk(ex,ey,e,a,L);
F(iel)=[c s]*[k(3,:);k(4,:)]*[deform(gdof)];
end