Sunteți pe pagina 1din 98

Department of Civil, Structural and

Environmental Engineering

Student Name &


Alejandro Jimenez Rios
number

Student signature

Module S2

Lecturer(s) Dermot ODwyer

Assignment Title Programming exercises

Date 12 January 2017

I have read and I understand the plagiarism provisions in the General Regulations of
the University Calendar for the current year, found at: http://www.tcd.ie/calendar
I have also completed the Online Tutorial on avoiding plagiarism Ready, Steady,
Write, located at http://tcd-ie.libguides.com/plagiarism/ready-steady-write
I declare that the assignment being submitted represents my own work and has not
been taken from the work of others save where appropriately referenced in the body
of the assignment.

Signed Date
Table of Contents
1. Introduction .......................................................................................................................... 1
1.1. FEM ............................................................................................................................... 1

1.2. FDM ............................................................................................................................... 1

1.3. Stepping methods ......................................................................................................... 2

2. Beam finite element code ..................................................................................................... 3


2.1. MSTIFFG Function ....................................................................................................... 13

2.2. STORES Function ......................................................................................................... 13

2.3. MDISPG Function ........................................................................................................ 14

2.4. MTRANS Function ....................................................................................................... 15

2.5. MDISPL Function ......................................................................................................... 15

2.6. MSTIFFL Function ........................................................................................................ 16

2.7. MFORCEL Function ...................................................................................................... 16

2.8. MFORCEG Function ..................................................................................................... 16

2.9. STORER Function ......................................................................................................... 17

2.10. Beam example ......................................................................................................... 17

3. Finite difference code ......................................................................................................... 22


3.1. Beam on elastic foundation. ....................................................................................... 22

3.1.1. INIPAR Function .................................................................................................. 26

3.1.2. ALLPAR Function ................................................................................................. 28

3.1.3. Beam on elastic foundation example.................................................................. 31

3.2. Torsion of non-circular shafts. .................................................................................... 34

4. Structural dynamics ............................................................................................................ 40


5. Conclusions ......................................................................................................................... 45
6. References........................................................................................................................... 46
7. Annexes ............................................................................................................................... 47
7.1. FEM Beam code........................................................................................................... 47

7.2. FEM Plane frames code............................................................................................... 56

7.3. FEM Space trusses code .............................................................................................. 68

7.4. FEM Grids code ........................................................................................................... 76

7.5. FEM Space frames code .............................................................................................. 85

ii
List of Figures
Figure 1: Beam example.............................................................................................................. 18
Figure 2: Shear, moment and deflection graphs of the example beam. .................................... 21
Figure 3: Beam on elastic foundation example. ......................................................................... 31
Figure 4: FDM solution diagrams. ............................................................................................... 33
Figure 5: Analytical solution diagrams ........................................................................................ 34
Figure 6: SAP200 solution diagrams............................................................................................ 34
Figure 7: Phi values for the rectangular shaft. ............................................................................ 39
Figure 8: Displacement vs time plots for a free vibration SDOF system. .................................... 42
Figure 9: Modes of the SDOF system. ......................................................................................... 44

iii
1. Introduction
This report presents the description of the work carried out within the S2 Advanced Structural
Analysis Course along with the MATLAB codes generated. Three main structural analysis
methods were studied and applied:
1. Finite Elements Method (FEM)
2. Finite Difference Method (FDM)
3. Stepping Methods, in this case, Runge-Kutta Method.
Description of the process is given in the following points.

1.1. FEM
The FEM programs were developed following the book of Kassimali (Kassimali, 1999). In this
book the author develops 6 different programs in order to solve 6 different types of structures
which are:
1. Plane trusses.
2. Beams.
3. Plane frames.
4. Space trusses.
5. Grids.
6. Space frames.
Starting for the simplest formulation to solve plane trusses and escalating in complexity until
attain the solution of space frames, 6 different programs were developed. All of them are
general, capable of generate and automatically assemble the global stiffness matrix from
geometrical and material member properties provided by the user. Different types of load are
handle for the programs, among them, uniformly distributed loads and nodal point loads.
For the specific case of the beam program, the Euler beam element was used and the structural
actions such as moments, shears x axial forces and deflections are described with the use of
shape functions corresponding to each type of structural element.

1.2. FDM
The FDM was applied two solve two different engineering problems:
1. Beam on an elastic foundation.
2. Torsion of non-circular prismatic sections.
General guidance was taken from Nevilles book (Ghali, 2009) to program the FDM. The first
problem was also solved using the analytical solution provided by Hetny (Hetnyi, 1946) in order
to compare the results with those obtained with the FDM and see how the discretization of the
problem influences in the final results. A third set of results was obtained using the commercial

1
software called SAP2000. All results and comparison between them is presented in further
chapters of this report.
For the second problem a simple rectangular section was used and in order to solve the system
of equation the advantages provided by MATLAB where exploded. In this case the use of \
provided the searched results in an efficient way and there was no need to implement the
Gauss-Seidel relaxation solution scheme. Same operator was used to solve all other system of
equations in the different methods studied.

1.3. Stepping methods


A Runge-Kutta routine was written to obtain the time-step response of a single degree of
freedom (SDOF) elastic structure. The SDOF was also solved using the analytical mathematical
expression and both results were compared as well.
After that a 3 building storey building was studied in order to obtain its mass and stiffness
matrices, its natural frequencies and Eigen vectors. Finally the generalized mas and stiffness
matrices were computed which will allow to proceed with the modal analysis of the structure
providing an external excitation is applied to it.

2
2. Beam finite element code
In this chapter the program to solve plane trusses is presented and explained in detail. The other
5 programs to solve the corresponding structure types are presented in the annexes and follow
the same logic.
First of all, the program is divided in three main modules:
1. Input module
2. Analysis module
3. Results module
Comment lines in MATLAB code are preceded by a % symbol and are in red colour. They were
introduced in order to document the actual code lines and as a reminder for future access to the
codes.
*Note: the present example shows the values correspondent to the truss shown in Figure 4.2 of
the mentioned reference and the results obtained with the developed code match those
presented for the author.

The first line is common to all programs generated and is used to erase any possible existing
variables, close all figures and clean the command window.

%Code to analyse plane trusses


%Using the code number technique

clear all; close all; clc;

This is the beginning of the Input module. Here the users can introduce all the necessary
information regarding, nodal coordinates, member connectivity and properties as well as the
loads acting in the structure and the supports.

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Input Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%

The joint data is stored in a matrix called COORD. It contains both the X and Y global
coordinates of all the members of the truss. The number of lines of COORD is equivalent to the
number of nodes of the structure, NJ.

%Joint data

%COORD = Joint coordinate matrix


%NJ = Number of joints

%COORD=[X Y]
COORD=[0 0;
288 0;
576 0;
864 0;
288 216;
576 216];

3
[NJ x]=size(COORD);

The support data is stored in a matrix called MSUP. The first column of this matrix contain the
joint number that is supported, NS, and the second and third columns contain the support
condition of the respective joint. A number 1 represents a restrained coordinate and a number
0 a free coordinate. A plane truss has two coordinates per node, translation in X and Y
directions.

%Support data

%MSUP = Support data matrix


%NS = Number of supports

%MSUP=[NS TX TY]
MSUP=[1 1 1;
3 0 1;
4 0 1];

[NS x]=size(MSUP);

Since this is a linear elastic problem, only the modulus of Young is required to describe the
behaviour of the different materials of the structure. Each different modulus is stored in the
different rows of the vector called EM. The number of different materials is equivalent to the
length of the EM vector and is identified with NMP.

%Material property data

%EM = Elastic modulus vector


%NMP = Number of materials used in the structure

%EM=[E]
EM=[29000;
10000];

NMP=length(EM);

Since a truss element only carries axial forces, the only geometrical property required to
describe its stress condition is the cross sectional area. The values of the different elements
are stored in a vector called CP. The number of different sections is equivalent to the length of
the CP vector and is identified with a variable called NCP.

%Cross-sectional property data

%CP = Cross-sectional property vector


%NCP = Number of cross-sections used in the structure

%CP=[A]
CP=[8;
12;
16];

NCP=length(CP);

The creation of the matrix MPRP is necessary in order to describe the member connectivity
and to assign their correspondent material and cross-sectional property. The first column of
this matrix contain the initial node of each element, NB, the second column contain the final

4
node of each element, NE, the third column specifies the material type of each element with
reference to the place that it occupies in the rows of the EM vector and finally the forth
column contain the cross-sectional type of each element with reference to the place that it
occupies in the rows of the CP vector. The number of rows of the MPRP matrix correspond
with the number of elements of the structure and is assigned to the variable called NM.

%Member data

%MPRP = Member data matrix


%NM = Number of members

%MPRP=[NB NE EM(I) CP(I)]


MPRP=[1 2 1 1;
2 3 1 1;
3 4 2 3;
5 6 1 1;
2 5 1 1;
3 6 1 1;
1 5 1 2;
2 6 1 2;
3 5 1 2;
4 6 2 3];

[NM x]=size(MPRP);

Since a plane truss only carries point loads applied to its nodes only Node loads are described
in this part of the code. Nevertheless, for more complex structures, such as beams, element
loads as well as node loads can be applied. For further information about it please consult the
mentioned reference.
To assign the loads to the structure first a vector needs to be created where the nodes under
an external force will be specified in each one of the rows of this vector, JP. After this, a matrix
is created, PJ, where the correspondent X and Y force components will be described in global
coordinates. The number of rows of this matrix is equivalent to the number of joints loaded,
NJL, and its number of columns correspond to the number of coordinates per node in a plane
truss structure, NCJT.

%Load data

%JP = Loaded joints vector


%PJ = Load data matrix
%NJL = Number of joints loaded
%NCJT = Number of coordinates per joint

%JP=[NN]
JP=[2;
5;
6];

%PJ=[FX FY]
PJ=[0 -75;
25 0;
0 -60];

[NJL NCJT]=size(PJ);

5
This is the last part of the Input module, the following code lines are part of the Analysis
module. This module is divided at its time in sections:
1. Assignment of structure coordinate numbers.
2. Generation of the structure stiffness matrix.
3. Formation of the joint load vector.
4. Solution of the linear equations system to obtain the node displacements.
5. Calculation of member forces and support reactions.

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Analysis Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%

In order to assemble the structure stiffness matrix a technique called member code numbers
is used. This technique was first introduced by S. S. Tezcan, for more details consult (Kassimali,
1999).

%Assignment of structure coordinate numbers

The total number of coordinates of the structure can be obtained by multiplying the number of
nodes, NJ, times the number of coordinates per node, NCJT. The number of restrained
coordinates is obtained by counting the number of non-zero elements in the second and third
columns of the MSUP matrix. After this, obtaining the number of degrees of freedom, NDOF, is
relatively easy, all is needed to do is subtract the number of NR to the number of total
coordinates of the structure.

%NR = Number of restrained coordinates


%NDOF = Number of degrees of freedom

NR=nnz(MSUP(:,2:NCJT+1));

NDOF=NCJT*NJ-NR;

%NSC = Structure coordinate number vector


%I, I1, I2, I3, J, K, ICOUNTUNT = Counters and flags

A vector called NSC is created in order to relate the structure coordinates to the local
coordinates of each node. This is the so called structure coordinate number vector.

NSC=zeros(NCJT*NJ,1);
I=1;
J=0;
K=NDOF;

while I<=NJ
ICOUNT=0;
I1=1;

while I1<=NS

if MSUP(I1,1)==I
ICOUNT=1;
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;

6
if MSUP(I1,I2+1)==1
K=K+1;
NSC(I3,1)=K;
I2=I2+1;
else J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

end

I1=I1+1;
else I1=I1+1;
end

end

if ICOUNT==0
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;
J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

I=I+1;
else I=I+1;
end

end

In order to assemble the structure stiffness matrix, the following code lines perform an
iterative process throughout all the elements, first obtaining each individual member
properties from the Input module information, then calculating its global stiffness matrix and
finally storing the appropriate terms of that element matrix into the structure stiffness matrix.
The function MSTIFFG computes the elements global stiffness matrix and the function STORES
assemble the structure stiffness matrix using the structure coordinate numbers vector NSC.
Both functions are presented at the end of the explanation of the main program within the
present chapter.

%Generation of the structure stiffness matrix

%JB = Initial node


%JE = Final node
%E = Young modulus
%A = Cross-section area
%XB = X coordinate of initial node
%YB = Y coordinate of initial node
%XE = X coordinate of final node
%YE = Y coordinate of final node
%BL = Length
%CX = Cosine
%CY = Sine
%IM, I = Counters and flags

%GK = Global stiffness matrix

7
%S = Structure stiffness matrix

%MSTIFFG : Function that forms the member global stiffness matrix GK


%STORES : Function that stores the pertinent elements of the member
global
%stiffness matrix GK in the structure stiffness matrix S

GK=zeros(2*NCJT);
S=zeros(NDOF);

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
A=CP(I);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
BL=((XE-XB)^2+(YE-YB)^2)^.5;
CX=(XE-XB)/BL;
CY=(YE-YB)/BL;

%Call of MSTIFFG

GK=MSTIFFG(E,A,BL,CX,CY,GK);

%Call of STORES

S=STORES(JB,JE,NCJT,NDOF,NSC,GK,S);

IM=IM+1;
end

In order to assemble the joint load vector, the following code lines perform an iterative
process through the load data vectors provided by the user in the Input mode. It iterates NJL
(Number of joints loaded) times and assigns the correspondent joint loads to the joint load
vector using once again the information contained within the structure coordinate numbers
vector, NSC.

%Formation of the joint load vector

%P = Joint load vector


%I, I1, I2, N = Counters and flags

P=zeros(NDOF,1);
I=1;

while I<=NJL
I1=JP(I);

8
I2=(I1-1)*NCJT;
J=1;

while J<=NCJT
I2=I2+1;
N=NSC(I2);

if N<=NDOF
P(N)=P(N)+PJ(I,J);
J=J+1;
else J=J+1;
end
end

I=I+1;
end

Since MATLAB is especially useful to work with matrices, it has a series of functions and
operators predefined that provide fast and efficient results. One of those is the \ operator
which provides the solution of a system of linear equations by writing only one code line thus
avoiding typing a whole routine or creating another function to apply Gauss, Gauss-Seidel or
any other solution process. By applying this operator, the free coordinate displacements of the
structures are determined using the structure stiffness matrix and the joint load vector
assembled in previous steps of the code.

%Solution for joint displacements

%d = Joint displacements vector

d=S\P;

At this point of the program the initial problem has been solved. Once the displacements are
known, compatibility and equilibrium equations can be applied to determine member forces
and support reactions. Once again the program performs an iterative process passing
throughout each element, obtaining its properties from the Input module and computing with
the use of different functions the members global displacements vector, transformation
matrix, local displacements vector, local stiffness matrix, local force vector and global force
vector. Finally, the support reactions are computed by assigning the elements of this last
vector to their correspondent reaction vector position using once again the NSC vector.
The function MDISPG computes the member global displacements vector, MTRANS assembles
the member transformation matrix, MDISPL calculates the member local stiffness matrix,
MSTIFFL computes the member local stiffness matrix, MFORCEL calculates the member local
forces vector, MFORCEG provides the member global forces vector and finally STORER
assemble the structure support reactions vector. All functions are presented at the end of the
explanation of the main program within the present chapter.

%Calculation of member forces and support reactions

%R = Reactions vector
%V = Global displacement vector
%T = Transformation matrix
%U = Local displacement vector
%F = Global force vector
%Q = Local force vector
%BK = Local stiffness matrix
%IM = Counters and flags

9
%MDISPG : Function that determines the member global displacement
vector V
%from the joint displacement vector P
%MTRANS : Function that forms the member transformation matrix T
%MDISPL : Function that calculates the member local displacement
vector U=T*V
%MSTIFFL : Function that forms the member local stiffness matrix BK
%MFORCEL : Function that evaluates the member local force vector
Q=BK*U
%it also creates the member axial force table to show in the results
%MFORCEG : Function that determines the member global force vector
F=T'*Q
%STORER : Function that stores the pertinent elements of the member
global
%force vector F in the reaction vector R

R=zeros(NR,1);
memaxifortab=zeros(NM,2);
memaxifortab(:,1)=(1:NM)';

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
A=CP(I);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
BL=((XE-XB)^2+(YE-YB)^2)^.5;
CX=(XE-XB)/BL;
CY=(YE-YB)/BL;

V=zeros(2*NCJT,1);
T=zeros(2*NCJT);
U=zeros(2*NCJT,1);
BK=zeros(2*NCJT);
Q=zeros(2*NCJT,1);
F=zeros(2*NCJT,1);

%Call of MDISPG

V=MDISPG(JB,JE,NCJT,NDOF,NSC,d,V);

%Call of MTRANS

T=MTRANS(CX,CY,NCJT,T);

%Call of MDISPL

U=MDISPL(V,T,U);

10
%Call of MSTIFFL

BK=MSTIFFL(E,A,BL,BK);

%Call of MFORCEL

[Q memaxifortab]=MFORCEL(BK,U,IM,memaxifortab);

%Call of MFORCEG

F=MFORCEG(T,Q);

%Call of STORER

R=STORER(JB,JE,NCJT,NDOF,NSC,F,R);

IM=IM+1;
end

At this point all the information of interest when a structural problem is solved has been
already obtained. The last module of the program, the Results Module, presents in a graphical
way the mentioned numerical values and plots of the original geometry and the deformed
shape of the structures due to the applied set of loads.

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Results Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%

%Tables

%General structural data

COORD_table=[(1:NJ)' COORD]';
MSUP_table=MSUP';
EM_table=[(1:NMP)' EM]';
CP_table=[(1:NCP)' CP]';
MPRP_table=[(1:NM)' MPRP]';
JPPJ_table=[JP PJ]';

%Call of JOIDISTAB

joidistab=JOIDISTAB(NJ,NCJT,NSC,NDOF,d);

%Call of SUPREATAB

supreatab=SUPREATAB(MSUP,R,NS,NCJT);

fprintf('\t General Structural Data \n\n');


fprintf('Structure type : Plane Truss \n');
fprintf('Number of joints : %g \n', NJ);
fprintf('Number of members : %g \n', NM);
fprintf('Number of material property sets (E) : %g \n', NMP);
fprintf('Number of cross-sectional property sets : %g \n', NCP);
fprintf('\n*User must be conscious of the use of consistent units
\n\n');

11
fprintf('\n\t\t Joint Coordinates \n\n');
fprintf('Joint No \tX Coordinate \tY Coordinate \n');
fprintf('\t%g \t\t\t%g \t\t\t%g \n',COORD_table);
fprintf('\n\t\t\t Supports \n\n');
fprintf('\t*1 represents a constraint\n\n');
fprintf('Joint No \tX Restraint \tY Restraint \n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \n',MSUP_table);
fprintf('\n\t\t Material Properties \n\n');
fprintf('Material No \tModulus of Elasticity (E) \n');
fprintf('\t%g \t\t\t\t\t%g \n',EM_table);
fprintf('\n\t Cross-Sectional Properties \n\n');
fprintf('\t\tProperty No \tArea (A) \n');
fprintf('\t\t\t%g \t\t\t\t%g \n',CP_table);
fprintf('\n\t\t\t\t\t\t Member Data \n\n');
fprintf('Member No \tBeginning Joint \tEnd Joint \tMaterial No
\tCross-Sectional Property No \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g \t\t\t\t\t%g
\n',MPRP_table);
fprintf('\n\t\t\t Joint Loads \n\n');
fprintf('Joint No \t\tX Force \t\tY Force \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \n',JPPJ_table);
fprintf('\n\t\t\t End of Input Data \n\n');
fprintf('\n\n\t\t\t Results of analysis \n');
fprintf('\n\t\t\t Joint Displacements \n\n');
fprintf('Joint No \t\tX Translation \t\t\tY Translation \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \n',joidistab');
fprintf('\n\t\t\t Member Axial Forces \n\n');
fprintf('\t\t *Values < 0 represent Tension \n\n');
fprintf('\t\t\tMember \t\tAxial Force \n');
fprintf('\t\t\t\t%g \t\t\t%g \n',memaxifortab');
fprintf('\n\t\t\t Support reactions \n\n');
fprintf('Joint No \t\tX Force \t\t\tY Force \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \n',supreatab');
fprintf('\n\t\t\t\t End of Analysis \n\n');

%Figures

IM=1;
while IM<=NM

%Undeformed geometry

JB=MPRP(IM,1);
JE=MPRP(IM,2);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
BL=((XE-XB)^2+(YE-YB)^2)^.5;

x1=[XB;XE];
y1=[YB;YE];

figure(1)
hold on
plot(x1,y1,'-o')

%Deformed geometry

%ampfac = Amplification factor

12
ampfac=100;

XBD=XB+joidistab(JB,2)*ampfac;
XED=XE+joidistab(JE,2)*ampfac;
YBD=YB+joidistab(JB,3)*ampfac;
YED=YE+joidistab(JE,3)*ampfac;

x2=[XBD;XED];
y2=[YBD;YED];

plot(x2,y2,'r--x')

IM=IM+1;
end

A few remarks need to be make at this point. Even though the code presented and explained
in this report is the one for solving Plane trusses, the rest of the programs are structured and
work in a similar way, but, as said at the beginning of the chapter, increase in complexity. The
codes for plane trusses, space trusses, beams and plane frames provide numerical and
graphical results of the problem, nevertheless, the plane frame code can only provide correct
plots of element forces and deflections for horizontal elements. The two more complex codes
for grids and space frames can only provide numerical results, lack of time prevented further
code development in order to obtain accurate graphical representations for this las two types
of structures as well as for vertical and inclined elements in the plane frames code.

2.1. MSTIFFG Function

%MSTIFFG : Function that forms the member global stiffness matrix GK

function GK=MSTIFFG(E,A,BL,CX,CY,GK)

%E = Young modulus
%A = Cross-section area
%BL = Length
%CX = Cosine
%CY = Sine
%GK = Global stiffness matrix
%Z, Z1, Z2, Z3 = Intermediate calculations

Z=(E*A)/BL;
Z1=Z*CX^2;
Z2=Z*CY^2;
Z3=Z*CX*CY;

%Formula 3.73 page 86 Kassimali

GK=[Z1 Z3 -Z1 -Z3;


Z3 Z2 -Z3 -Z2;
-Z1 -Z3 Z1 Z3;
-Z3 -Z2 Z3 Z2];

2.2. STORES Function

%STORES : Function that stores the perinent elements of the member


global

13
%stiffness matrix GK in the structure stiffness matrix S

function S=STORES(JB,JE,NCJT,NDOF,NSC,GK,S)

%JB = Initial node


%JE = Final node
%NCJT = Number of coordinates per joint
%NDOF = Number of degrees of freedom
%NSC = Structure coordinate number vector
%GK = Global stiffness matrix
%S = Structure stiffness matrix
%I, J, I1, I2, N1, N2 = Counters and flags

I=1;

while I<=2*NCJT

if I<=NCJT
I1=(JB-1)*NCJT+I;
N1=NSC(I1);
else I1=(JE-1)*NCJT+(I-NCJT);
N1=NSC(I1);
end

if N1<=NDOF
J=1;

while J<=2*NCJT

if J<=NCJT
I1=(JB-1)*NCJT+J;
N2=NSC(I1);
else I1=(JE-1)*NCJT+(J-NCJT);
N2=NSC(I1);
end

if N2<=NDOF
S(N1,N2)=S(N1,N2)+GK(I,J);
J=J+1;
else J=J+1;
end

end

I=I+1;
else I=I+1;
end
end

2.3. MDISPG Function

%MDISPG : Function that determines the member global displacement


vector V

function V=MDISPG(JB,JE,NCJT,NDOF,NSC,d,V)

%JB = Initial node

14
%JE = Final node
%NCJT = Number of coordinates per joint
%NDOF = Number of degrees of freedom
%NSC = Structure coordinate number vector
%d = Joint displacements vector
%I, J, N = Counters and flags

I=1;
J=(JB-1)*NCJT;

while I<=NCJT
J=J+1;
N=NSC(J);

if N<=NDOF
V(I)=d(N);
I=I+1;
else I=I+1;
end
end

I=NCJT+1;
J=(JE-1)*NCJT;

while I<=2*NCJT
J=J+1;
N=NSC(J);

if N<=NDOF
V(I)=d(N);
I=I+1;
else I=I+1;
end
end

2.4. MTRANS Function

%MTRANS : Function that forms the member transformation matrix T

function T=MTRANS(CX,CY,NCJT,T)

%CX = Cosine
%CY = Sine
%NCJT = Number of coordinates per joint
%T = Transformation matrix

%Equation 3.61 Kassimali

T=[CX CY 0 0;
-CY CX 0 0;
0 0 CX CY;
0 0 -CY CX];

2.5. MDISPL Function

15
%MDISPL : Function that calculates the member local displacement
vector U=T*V

function U=MDISPL(V,T,U)

%V = Global displacement vector


%T = Transformation matrix
%U = Local displacement vector

U=T*V;

2.6. MSTIFFL Function

%MSTIFFL : Function that forms the member local stiffness matrix BK

function BK=MSTIFFL(E,A,BL,BK)

%E = Young modulus
%A = Cross-section area
%BL = Length
%Z = Intermediate calculations

Z=(E*A)/BL;

%Equation 3.27 Kassimali

BK=[Z 0 -Z 0;
0 0 0 0;
-Z 0 Z 0;
0 0 0 0];

2.7. MFORCEL Function

%MFORCEL : Function that evaluates the member local force vector


Q=BK*U

function [Q memaxifortab sign]=MFORCEL(BK,U,IM,memaxifortab,sign)

%U = Local displacement vector


%Q = Local force vector
%BK = Local stiffness matrix
%IM = Counter and flag

%Equation 3.7 Kassimali


Q=BK*U;

%Generation of the member axial forces table

memaxifortab(IM,2)=Q(1);

2.8. MFORCEG Function

16
%MFORCEG : Function that determines the member global force vector
F=T'*Q

function F=MFORCEG(T,Q)

%T = Transformation matrix
%F = Global force vector
%Q = Local force vector

%Equation 3.66 Kassimali


F=T'*Q;

2.9. STORER Function

%STORER : Function that stores the pertinent elements of the member


global
%force vector F in the reaction vector R

function R=STORER(JB,JE,NCJT,NDOF,NSC,F,R)

%JB = Initial node


%JE = Final node
%NCJT = Number of coordinates per joint
%NDOF = Number of degrees of freedom
%NSC = Structure coordinate number vector
%R = Reactions vector
%F = Global force vector
%I, I1, N = Counters and flags

I=1;

while I<=2*NCJT

if I<=NCJT
I1=(JB-1)*NCJT+I;
N=NSC(I1);
else I1=(JE-1)*NCJT+(I-NCJT);
N=NSC(I1);
end

if N>NDOF
R(N-NDOF)=R(N-NDOF)+F(I);
I=I+1;
else I=I+1;
end
end

2.10. Beam example

In order to show the results of the developed code the beam example presented in Kassimalis
Figure 5.19 was solved. This example was selected thus a comparison could be make between
the results presented in the reference and the results obtained and therefore the code could be
verified. The problem is shown in Figure 1.

17
Figure 1: Beam example.

The output of the code is shown in the following lines. It presents the information of both the
Input and the Results modules:

General Structural Data

Structure type : Beam


Number of joints : 4
Number of members : 3
Number of material property sets (E) : 1
Number of cross-sectional property sets : 2

*User must be conscious of the use of consistent units

Joint Coordinates

Joint No X Coordinate
1 0
2 120
3 360
4 480

Supports

*1 represents a constraint

Joint No Y Restraint Rotational Restraint


1 1 0
2 1 0

18
3 1 0
4 1 1

Material Properties

Material No Modulus of Elasticity (E)


1 29000

Cross-Sectional Properties

Property No Moment of Inertia (I)


1 350
2 500

Member Data

Member No Beginning Joint End Joint Material No Cross-Sectional Property No


1 1 2 1 1
2 2 3 1 1
3 3 4 1 2

Joint Loads

Joint No Y Force Moment


1 0 -480

Member Loads

Member No Load Type Magnitude (W, M, w or w1) Magnitud (w2) Distance l1 Distance l2
2 3 0.1667 0 0 120
2 1 25 0 180 0
3 4 0.25 0 0 0

End of Input Data

Results of analysis

Joint Displacements

19
Joint No Y Translation Rotation (Rad)
1 0 -5.571925e-04
2 0 -1.723053e-03
3 0 1.623819e-03
4 0 0.000000e+00

Member End Forces in Local Coordinates

Member Joint Shear Force Moment


1 1 -9.64354 -480
1 2 9.64354 -677.225
2 2 20.0546 677.225
2 3 24.9494 -964.846
3 3 20.3106 964.846
3 4 -5.31057 272.423

Support reactions

Joint No Y Force Moment


1 -9.64354 0
2 29.6981 0
3 45.26 0
4 -5.31057 272.423

End of Analysis

And finally shear, moment and deflection graphs are shown in Figure 2. The results obtained
with the MATLAB code are practically identical to those presented in the reference. The
graphical representation of the results can definitely be improved in further coding,
unfortunately that is something that goes beyond the scope of this assignment.

20
Figure 2: Shear, moment and deflection graphs of the example beam.

21
3. Finite difference code

3.1. Beam on elastic foundation.

Unfortunately, the FDM code developed is not of general application. It is limited to certain load
types and support conditions due to limitations in the analytical equations of the problem
implemented on it. In order to be able to carry out a comparison of the results obtained by three
different ways; Analytical solution, FDM and commercial software SAP2000, the case of a simple
beam on elastic foundation with free ends and a uniformly distributed load all along its length
was solved and presented in this report. Regardless of the present limitations of the code, other
support and load types could be implemented as well as multiple elements with slight
modifications.

First of all, the code will be described and results will be presented in further points of the report.
The code is divided in five main modules:
1. Input module
2. Analytical solution module
3. FDM solution module
4. Results module
5. Figures module
As for the FEM code, the comment lines, code lines and text colours apply in a similar way. The
first code line is used to delete all existing variables, close all open figures and clean the
command screen in MATLAB.

%Program that computes deflections, slopes, moments, shears and


reactions
%of a beam suported by an elastic foundation using Krilov's functions
%and the finite difference method

clear all; close all; clc;

In the input module the user has to introduce all the necessary information to define the
problem. It includes the basic parameters such as:
Modulus of elasticity of the material.
Modulus of elasticity of the foundation.
Beam length.
Beams dimensions.
Load matrix
Using this basic information, the code will proceed to compute the value of some other
fundamental parameters such as:
Winklers constant.
Bending stiffness.
Characteristic of the system.

22
Relative stiffness of the beam.

%% Input module

%Basic parameters

%E = Material modulus of elasticity


%k0 = Modulus of the foundation
%l = Beam length
%b, h = Beam's base and high

E=3e7;
k0=50000;
l=10;
b=1.1;
h=.5;

%Load matrix

%LOAMAT = Load information matrix


%NL = Number of loads

%LOAMAT = [Load type Magnitud X0 X1,0]


LOAMAT=[3 200 0 10];

[NL x]=size(LOAMAT);

%% Beam and soil characteristics

%Wikler's constant

k=k0*b;

%I = Inertia
%EI = Bending stiffness
%alpha = Characteristic of the system
%al = Relative stiffness of the beam

I=b*h^3/12;
EI=E*I;
alpha=(k/(4*EI))^(1/4);
al=alpha*l;

The analytical solution implemented in the code is the one that uses the so called Krilovs
functions. Initial parameters of such functions and final computation of deflexions, slopes,
moments, shears and reactions are computed in two different functions, INIPAR and ALLPAR
respectively. Both functions are presented at the end of the explanation of the main program
within the present chapter.

%% Analysis module Krilov's functions

%Initial parameters and Bar parameters

%Call of INIPAR

23
[inipar]=INIPAR(alpha,l,LOAMAT,NL);

%Computation of deflexions, slopes, moments, shears and reactions

%Call of ALLPAR

[x1,VV,PHIPHI,MM,QQ]=ALLPAR(inipar,k,EI,alpha,l,NL,LOAMAT);

RR=k*VV/EI;

The FDM solution is performed in a new function called FDMSOL. The input variables for this
function are the number of elements in which the beam will be discretized, NNFD, a vector of
X coordinates for each one of the new nodes of the beam, xfdm, the size of the new elements,
lam, the value of the uniform distributed load applied in the beam, qi, an auxiliary square
matrix with size NNFD and the properties of both materials, soil and beam, k and EI
respectively.

%% Analysis module Finite Differences Method

%NNFD = Number of point divisions of the beam


%lam = Lamda
%xfdm = Vector of point divisions of the beam
%kfdm = Matrix of cofficients of influence for FDM
%qfdm = Vector of load at each point for FDM
%dfdm = Vector of displacements at each point for FDM

NNFD=10;

xfdm=linspace(0,l,NNFD);
lam=l/(NNFD-1);
qi=LOAMAT(1,2);
auxmat=zeros(NNFD);

%Call of FDMSOL function

[dfdm,mfdm,rfdm]=FDMSOL(NNFD,xfdm,lam,qi,auxmat,k,EI);

fdm_table=[[1:NNFD]' dfdm mfdm rfdm];

The results module groups and organise the results obtained in the two precedent modules. It
plots the numerical values in a tabulated way in the command window of the software.

%% Results module

MEMLOAD_table=LOAMAT';

SUMMARY_table=[];
i=1;
j=1;

while i<=length(x1)+1

if i==101
i=100;
else i=i;

24
end

SUMMARY_table(j,1)=x1(i);
SUMMARY_table(j,2)=VV(i);
SUMMARY_table(j,3)=PHIPHI(i);
SUMMARY_table(j,4)=MM(i);
SUMMARY_table(j,5)=QQ(i);
SUMMARY_table(j,6)=RR(i);

j=j+1;
i=i+10;

end

fprintf('\t General Structural Data \n\n');


fprintf('Structure type : Beam on elastic foundations \n');
fprintf('Beam relative stiffness : %g \n', al);
fprintf('\n*Results valid for a range between 0.5 and 5 \n\n');
fprintf('Material modulus of elasticity : %g \n', E);
fprintf('Modulus of the foundation : %g \n', k0);
fprintf('\n*User must be conscious of the use of consistent units
\n\n');
fprintf('\n\t\t\t\t\t Member Loads \n\n');
fprintf('Load Type \tMagnitude (W, M, w) \tDistance l1 \tDistance l2,
0 \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t\t%g \n',MEMLOAD_table);
fprintf('\n\n\t\t Summary Results Table of Analytical Solution \n\n');
fprintf('x \t\t\tV(x)=EIy(x) \t\tPHI(x)=EIphi(x) \t\t\tM(x)
\t\t\t\t\tQ(x) \t\t\t\t\tR(x)\n');
fprintf('%.2f \t\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t\t%g
\t\t\t\t%g\n',SUMMARY_table');
fprintf('\n\n\t\t Summary Results Table of FDM Solution \n\n');
fprintf('x \t\t\tV(x)=y(x) \t\t\tM(x) \t\t\t\t\tR(x)\n');
fprintf('%.2f \t\t%g \t\t\t\t%g \t\t\t\t%g\n',fdm_table');

Finally, the figures module create plots for the different diagrams of interest. The analytical
solution allows to create deflection, slope, moment, shear and reaction diagrams whereas that
the FDM solution only allows to plot deflection moment and reactions.

%% Figures module

%FDM figures

figure(1)
hold on

subplot(3,1,1)
plot(xfdm,dfdm)
title('FDM solution diagrams')
ylabel('Deflection')

subplot(3,1,2)
plot(xfdm,mfdm)
ylabel('Moments')

subplot(3,1,3)

25
plot(xfdm,rfdm)
ylabel('Reactions')
xlabel('Beam length')

%Analytical solution figures

figure(2)
hold on

subplot(5,1,1)
plot(x1,VV)
grid on
title('Analytical solution diagrams')
ylabel('Deflection')

subplot(5,1,2)
plot(x1,PHIPHI)
grid on
ylabel('Slope')

subplot(5,1,3)
plot(x1,MM)
grid on
ylabel('Moment')

subplot(5,1,4)
plot(x1,QQ)
grid on
ylabel('Shear')

subplot(5,1,5)
plot(x1,RR)
grid on
ylabel('Reaction')
xlabel('Beam length')

3.1.1. INIPAR Function

%Function that computes the initial parameters of the left using


boundary
%conditions

function [inipar]=INIBARPAR(alpha,l,LOAMAT,NL)

M0=0;
Q0=0;

%Computation of constants for the DE solution

syms x

Aax=cosh(alpha*x)*cos(alpha*x);
Bax=(cosh(alpha*x)*sin(alpha*x)+sinh(alpha*x)*cos(alpha*x))/2;
Cax=(sinh(alpha*x)*sin(alpha*x))/2;
Dax=(cosh(alpha*x)*sin(alpha*x)-sinh(alpha*x)*cos(alpha*x))/4;

%subsuation of constants at x=l

26
Aal=eval(subs(Aax,l));
Bal=eval(subs(Bax,l));
Cal=eval(subs(Cax,l));
Dal=eval(subs(Dax,l));

K=[4*alpha^2*Cal 4*alpha*Dal;
4*alpha^3*Bal 4*alpha^2*Cal];

%subsuation of constants for loads

P=zeros(2,1);
inipar=zeros(4,1);

i=1;

while i<=NL

if LOAMAT(i,1)==1
f=l-LOAMAT(i,3);
F=LOAMAT(i,2);

Daf=eval(subs(Dax,f));
Caf=eval(subs(Cax,f));
Baf=eval(subs(Bax,f));
Aaf=eval(subs(Aax,f));

Vbarx=Daf*F/alpha^3;
PHIbarx=Caf*F/alpha^2;
Mbarx=-Baf*F/alpha;
Qbarx=-Aaf*F;

P=P+[Mbarx Qbarx]';

i=i+1;

else if LOAMAT(i,1)==2
m=l-LOAMAT(i,3);
M=LOAMAT(i,2);

Cam=eval(subs(Cax,m));
Bam=eval(subs(Bax,m));
Aam=eval(subs(Aax,m));
Dam=eval(subs(Dax,m));

Vbarx=-Cam*M/alpha^2;
PHIbarx=-Bam*M/alpha;
Mbarx=Aam*M;
Qbarx=-4*alpha*Dam*M;

P=P+[Mbarx Qbarx]';

i=i+1;

else if LOAMAT(i,1)==3
n=l-LOAMAT(i,3);
k=l-LOAMAT(i,4);
q=LOAMAT(i,2);

27
Aan=eval(subs(Aax,n));
Aak=eval(subs(Aax,k));
Dan=eval(subs(Dax,n));
Dak=eval(subs(Dax,k));
Can=eval(subs(Cax,n));
Cak=eval(subs(Cax,k));
Ban=eval(subs(Bax,n));
Bak=eval(subs(Bax,k));

Vbarx=(-q/(4*alpha^4))*(Aan-Aak);
PHIbarx=(q/alpha^3)*(Dan-Dak);
Mbarx=(-q/alpha^2)*(Can-Cak);
Qbarx=(-q/alpha)*(Ban-Bak);

P=P+[Mbarx Qbarx]';

i=i+1;
end
end
end
end

d=K\-P;

inipar=[d;
M0;
Q0];

3.1.2. ALLPAR Function

%Function that computes all parameters of the beam

function [x1,VV,PHIPHI,MM,QQ]=ALLPAR(inipar,k,EI,alpha,l,NL,LOAMAT)

% clear all; close all; clc;


% inipar=[590.0349 -104.5144 0 0]';
% k=55000;
% EI=343750;
% alpha=.44721;
% l=10;
% LOAMAT=[1 250 1 0;
% 2 100 4 0;
% 3 200 5 10];
% [NL x]=size(LOAMAT);

syms x

Aax=cosh(alpha*x)*cos(alpha*x);
Bax=(cosh(alpha*x)*sin(alpha*x)+sinh(alpha*x)*cos(alpha*x))/2;
Cax=(sinh(alpha*x)*sin(alpha*x))/2;
Dax=(cosh(alpha*x)*sin(alpha*x)-sinh(alpha*x)*cos(alpha*x))/4;

x1=linspace(0,l);

i=1;

28
while i<=length(x1)

%General definition of constants

Aax1=eval(subs(Aax,x1(i)));
Bax1=eval(subs(Bax,x1(i)));
Cax1=eval(subs(Cax,x1(i)));
Dax1=eval(subs(Dax,x1(i)));

K=[Aax1 Bax1/alpha -Cax1/alpha^2 -


Dax1/alpha^3;
-4*alpha*Dax1 Aax1 -Bax1/alpha -
Cax1/alpha^2;
4*alpha^2*Cax1 4*alpha*Dax1 Aax1
Bax1/alpha;
4*alpha^3*Bax1 4*alpha^2*Cax1 -4*alpha*Dax1
Aax1];

%Member loads influence

barpar=zeros(4,1);

ii=1;

while ii<=NL

if LOAMAT(ii,1)==1
F=LOAMAT(ii,2);

if x1(i)<LOAMAT(ii,3)
ii=ii+1;
else
f=x1(i)-LOAMAT(ii,3);

Daf=eval(subs(Dax,f));
Caf=eval(subs(Cax,f));
Baf=eval(subs(Bax,f));
Aaf=eval(subs(Aax,f));

Vbar=Daf*F/alpha^3;
PHIbar=Caf*F/alpha^2;
Mbar=-Baf*F/alpha;
Qbar=-Aaf*F;

barpar=barpar+[Vbar PHIbar Mbar Qbar]';

ii=ii+1;
end

else if LOAMAT(ii,1)==2
M=LOAMAT(ii,2);

if x1(i)<LOAMAT(ii,3)
ii=ii+1;

else
m=x1(i)-LOAMAT(ii,3);

29
Cam=eval(subs(Cax,m));
Bam=eval(subs(Bax,m));
Aam=eval(subs(Aax,m));
Dam=eval(subs(Dax,m));

Vbar=-Cam*M/alpha^2;
PHIbar=-Bam*M/alpha;
Mbar=Aam*M;
Qbar=-4*alpha*Dam*M;

barpar=barpar+[Vbar PHIbar Mbar Qbar]';

ii=ii+1;
end

else if LOAMAT(ii,1)==3
q=LOAMAT(ii,2);

if x1(i)<LOAMAT(ii,3)
ii=ii+1;

else if LOAMAT(ii,3)<=x1(i) && x1(i)<LOAMAT(ii,4)

n=x1(i)-LOAMAT(ii,3);
k=0;

Aan=eval(subs(Aax,n));
Aak=eval(subs(Aax,k));
Dan=eval(subs(Dax,n));
Dak=eval(subs(Dax,k));
Can=eval(subs(Cax,n));
Cak=eval(subs(Cax,k));
Ban=eval(subs(Bax,n));
Bak=eval(subs(Bax,k));

Vbar=(-q/(4*alpha^4))*(Aan-Aak);
PHIbar=(q/alpha^3)*(Dan-Dak);
Mbar=(-q/alpha^2)*(Can-Cak);
Qbar=(-q/alpha)*(Ban-Bak);

barpar=barpar+[Vbar PHIbar Mbar Qbar]';

ii=ii+1;

else if LOAMAT(ii,4)<=x1(i)
n=x1(i)-LOAMAT(ii,3);
k=x1(i)-LOAMAT(ii,4);

Aan=eval(subs(Aax,n));
Aak=eval(subs(Aax,k));
Dan=eval(subs(Dax,n));
Dak=eval(subs(Dax,k));
Can=eval(subs(Cax,n));
Cak=eval(subs(Cax,k));
Ban=eval(subs(Bax,n));
Bak=eval(subs(Bax,k));

Vbar=(-q/(4*alpha^4))*(Aan-Aak);
PHIbar=(q/alpha^3)*(Dan-Dak);

30
Mbar=(-q/alpha^2)*(Can-Cak);
Qbar=(-q/alpha)*(Ban-Bak);

barpar=barpar+[Vbar PHIbar Mbar


Qbar]';

ii=ii+1;
end
end
end
end
end
end
end

%Computation of all parameters

d=K*inipar+barpar;

VV(i)=d(1);
PHIPHI(i)=d(2);
MM(i)=d(3);
QQ(i)=d(4);

i=i+1;
end

x1;
VV;
PHIPHI;
MM;
QQ;

3.1.3. Beam on elastic foundation example

The beam of the example is a single spam beam supported on elastic foundation with a length
of 10 m. A uniform distributed load is applied all along its length. This is shown in Figure 3.

Figure 3: Beam on elastic foundation example.

The output of the code is shown in the following lines. It presents the information of the Input,
Analytical solution and FDM solution modules:

31
General Structural Data

Structure type : Beam on elastic foundations


Beam relative stiffness : 4.47214

*Results valid for a range between 0.5 and 5

Material modulus of elasticity : 3e+07


Modulus of the foundation : 50000

*User must be conscious of the use of consistent units

Member Loads

Load Type Magnitude (W, M, w) Distance l1 Distance l2, 0


3 200 0 10

Summary Results Table of Analytical Solution

x V(x)=EIy(x) PHI(x)=EIphi(x) M(x) Q(x) R(x)


0.00 1250 0 0 0 200
1.01 1250 -7.10543e-15 0 0 200
2.02 1250 0 0 5.68434e-14 200
3.03 1250 -1.13687e-13 0 1.13687e-13 200
4.04 1250 -4.54747e-13 0 1.13687e-13 200
5.05 1250 -4.54747e-13 0 2.84217e-14 200
6.06 1250 -9.09495e-13 0 -1.13687e-13 200
7.07 1250 0 -1.42109e-14 0 200
8.08 1250 0 0 0 200
9.09 1250 4.54747e-13 0 -1.81899e-12 200
10.00 1250 3.63798e-12 0 0 200

Summary Results Table of FDM Solution

x V(x)=y(x) M(x) R(x)


1.00 0.00363636 0 200
2.00 0.00363636 1.20753e-13 200
3.00 0.00363636 -7.24518e-13 200

32
4.00 0.00363636 -4.83012e-13 200
5.00 0.00363636 -4.83012e-13 200
6.00 0.00363636 -7.24518e-13 200
7.00 0.00363636 -8.45271e-13 200
8.00 0.00363636 -4.83012e-13 200
9.00 0.00363636 3.62259e-13 200
10.00 0.00363636 0 200

And finally element and deflection graphs are shown in Figure 4, Figure 5 and Figure 6 for the
FDM, analytical and SAP2000 solutions respectively. The results obtained with the three
different ways show practically the same value of 200 kN/m for the reactions and 0.0036 m for
the deflections.

Figure 4: FDM solution diagrams.

33
Figure 5: Analytical solution diagrams

Figure 6: SAP200 solution diagrams.

3.2. Torsion of non-circular shafts.

The application of FDM to solve the problem of non-circular shafts was implemented following
the handouts of the course. The discretization of a rectangular section was performed
successfully and the values of the function were found. By integrating this values the total
torque, T, of the shaft was computed. Unfortunately, the developed code was not able to find
the values of shear due to a singularity of the equation system formed when the equations for
shear in the x and z direction were solved using the FDM. The developed code is presented in
the following lines as well as the graph of the values found in Figure 7. The value computed
for the torque is:
= 7.803 104
This torque value is meaningless in the since that the set-up of the problem was completely
random and we cannot conclude whether or not it is found within reasonable boundaries.

34
%Finite differences method
%Applied to solve torsion in a non-circular shaft

clear all; close all; clc;

%% Geometry and initial data

%X = Total horizontal dimension


%Y = Total vertical dimension

X=7;
Y=4;

%Other properties

%G = Shear modulus
%theta = Angular rotation

G=1000;
theta=1;

%% Step I : Mesh matrix

%Nx = Number of divisions in X direction


%Ny = Number of divisions in Y direction
%dx = Increment in X direction
%dy = Increment in Y direcction

Nx=4;
Ny=4;
dx=X/Nx;
dy=Y/Ny;

%% Step II : Creation of linear equations system

%forvec = Force vector


%smat = Stiffness matrix

NDOF=(Ny-1)*(Nx-1);
smat=zeros(NDOF);
k=1;
i=1;

while i<=Ny-1

j=1;

while j<=Nx-1

if i==1
if j==1
smat(k,i*j)=-(2/dx^2+2/dy^2);
smat(k,j+1)=1/dx^2;
smat(k,Nx)=1/dy^2;
j=j+1;
k=k+1;
else if j==Nx-1
smat(k,Nx-1)=-(2/dx^2+2/dy^2);

35
smat(k,Nx-2)=1/dx^2;
smat(k,(Nx-1)*2)=1/dy^2;
j=j+1;
k=k+1;
else
smat(k,j-1)=1/dx^2;
smat(k,j)=-(2/dx^2+2/dy^2);
smat(k,j+1)=1/dx^2;
smat(k,Nx-1+j)=1/dy^2;
j=j+1;
k=k+1;
end
end
else if i==Ny-1
if j==1
smat(k,(Nx-1)*(Ny-2)+1)=-(2/dx^2+2/dy^2);
smat(k,(Nx-1)*(Ny-2)+2)=1/dx^2;
smat(k,(Nx-1)*(Ny-3)+1)=1/dy^2;
j=j+1;
k=k+1;
else if j==Nx-1
smat(k,(Nx-1)*(Ny-1))=-(2/dx^2+2/dy^2);
smat(k,(Nx-1)*(Ny-1)-1)=1/dx^2;
smat(k,(Nx-1)*(Ny-2))=1/dy^2;
j=j+1;
k=k+1;
else
smat(k,(Nx-1)*(Ny-2)+j)=-(2/dx^2+2/dy^2);
smat(k,(Nx-1)*(Ny-2)+j-1)=1/dx^2;
smat(k,(Nx-1)*(Ny-2)+j+1)=1/dx^2;
smat(k,(Nx-1)*(Ny-3)+j)=1/dy^2;
j=j+1;
k=k+1;
end
end
else if i~=1 && i ~=Ny-1
if j==1
smat(k,(Nx-1)*(i-1)+1)=-(2/dx^2+2/dy^2);
smat(k,(Nx-1)*(i-1)+2)=1/dx^2;
smat(k,(Nx-1)*(i-2)+1)=1/dy^2;
smat(k,(Nx-1)*i+1)=1/dy^2;
j=j+1;
k=k+1;
else if j==Nx-1
smat(k,(Nx-1)*i)=-(2/dx^2+2/dy^2);
smat(k,(Nx-1)*i-1)=1/dx^2;
smat(k,(Nx-1)*(i-1))=1/dy^2;
smat(k,(Nx-1)*(i+1))=1/dy^2;
j=j+1;
k=k+1;
else if j~=1 && j~=Nx-1
smat(k,(Nx-1)*(i-1)+j)=-(2/dx^2+2/dy^2);
smat(k,(Nx-1)*(i-1)+j-1)=1/dx^2;
smat(k,(Nx-1)*(i-1)+j+1)=1/dx^2;
smat(k,(Nx-1)*(i-2)+j)=1/dy^2;
smat(k,(Nx-1)*i+j)=1/dy^2;
j=j+1;
k=k+1;
end
end
end

36
end
end
end

end

i=i+1;

end

forvec=ones(NDOF,1)*-2*G*theta;

%% Step III : Solution of linear equations to obtain values of phi

%phi = Function to satisfy equation

phi=smat\forvec;

%Plot of values of phi

aux1=linspace(0,X,Nx+1);
aux2=linspace(0,Y,Ny+1);
aux3=zeros(Nx+1,Ny+1);
k=1;
i=1;
while i<=Ny-1

j=1;

while j<=Nx-1

aux3(1+i,1+j)=phi(k);
j=j+1;
k=k+1;

end

i=i+1;

end
figure(1)
surf(aux1,aux2,aux3)
xlabel('X axis')
ylabel('Y axis')
title('Values of phi')

%% Step IV : Torque and shear computations

%tauxz = Shear stress in xz


%tauyz = Shear stress in yz
%T = Torque

%Torque computation

T=phi.*dx.*dy;
T=sum(T);

37
T=2*T;

%Shear stresses

phivec=aux3(:);
tauxzmat=zeros(length(phivec));

k=1;
i=1;

while i<=Ny+1

j=1;

while j<=Nx+1

if i==1
tauxzmat(k,Nx+j+1)=1/(2*dx);
j=j+1;
k=k+1;
else if i==Ny+1
tauxzmat(k,(Nx+1)*(Ny-1)+j)=-1/(2*dx);
j=j+1;
k=k+1;
else
tauxzmat(k,(Nx+1)*(i-2)+j)=-1/(2*dx);
tauxzmat(k,(Nx+1)*i+j)=1/(2*dx);
j=j+1;
k=k+1;
end
end
end

i=i+1;

end

tauxz=tauxzmat\phivec;

tauyzmat=zeros(length(phivec));

k=1;
i=1;

while i<=Ny+1

j=1;

while j<=Nx+1

if j==1
tauyzmat(k,(Nx+1)*(i-1)+1)=1/(2*dy);
j=j+1;
k=k+1;
else if j==Nx+1
tauyzmat(k,(Nx+1)*i)=-1/(2*dy);
j=j+1;
k=k+1;

38
else
tauyzmat(k,(Nx+1)*(i-1)+(j-1))=-1/(2*dy);
tauyzmat(k,(Nx+1)*(i-1)+(j+1))=1/(2*dy);
j=j+1;
k=k+1;
end
end
end

i=i+1;

end

tauyz=tauyzmat\phivec;

%Net shear stress

tau=(tauxz.^2+tauyz.^2).^.5;

Figure 7: Phi values for the rectangular shaft.

39
4. Structural dynamics

The structural dynamics code is basically divided into two sections. The first one provides the
solution of a SDOF system using both the exact analytical solution and a numerical time-stepping
solution, Runge-Kutta.

%S2 Structural Dynamics assignment

clear all; close all; clc;

First the initial properties of the system, mass, stiffness, time step and time vector as well as
damping coefficient, are provided by the user.

%% Runge-Kutta

% Free vibration of SDOF

%Differential equation
%m*x2+c*x1+k*x=0

m=4; %Mass
k=2000; %Stiffness
dt=.01; %Time step
t=0:dt:10; %Time vector
t=t';

wn=(k/m)^.5; %Angular frequency


T=(2*pi)/wn; %Period
f=1/T; %Frequency

psi=.1;
ccr=2*m*wn;
c=psi*ccr;
wd=wn*(1-psi^2)^.5;

%Analytical solution

u0=5;
u10=0;

The analytical solution is simply obtained by substituting the values of the time vector in the
next equation:

u=exp(-
psi.*wn.*t).*(u0.*cos(wd.*t)+((u10+u0.*psi.*wn)./wd).*sin(wd.*t));

The numerical solution on the other hand performs and iterative process for each time step in
order to compute the values for each time instant of displacement, velocity and acceleration.

% %Time stepping

x=zeros(length(t),1); %Displacements vector

40
x1=zeros(length(t),1); %Velocities vector
x2=zeros(length(t),1); %Accelerations vector

%Initial conditions

taux=zeros(4,1);
xaux=zeros(4,1);
x1aux=zeros(4,1);
x2aux=zeros(4,1);

taux(1)=t(1);
xaux(1)=5;
x1aux(1)=0;
x2aux(1)=0;

x(1)=xaux(1);
x1(1)=x1aux(1);
x2(1)=(-k*xaux(1)-c*x1aux(1))/m;

for i=2:length(t)

j=1;

while j<=4

if j==1
taux(j)=t(i-1);
xaux(j)=x(i-1);
x1aux(j)=x1(i-1);
x2aux(j)=(-k*xaux(j)-c*x1aux(j))/m;
j=j+1;
else if j==4
taux(j)=t(i-1)+dt;
xaux(j)=x(i-1)+x1aux(j-1)*dt;
x1aux(j)=x1(i-1)+x2aux(j-1)*dt;
x2aux(j)=(-k*xaux(j)-c*x1aux(j))/m;
j=j+1;
else
taux(j)=t(i-1)+dt/2;
xaux(j)=x(i-1)+x1aux(j-1)*dt/2;
x1aux(j)=x1(i-1)+x2aux(j-1)*dt/2;
x2aux(j)=(-k*xaux(j)-c*x1aux(j))/m;
j=j+1;
end
end

x(i)=x(i-
1)+(dt/6)*(x1aux(1)+2*x1aux(2)+2*x1aux(3)+x1aux(4));
x1(i)=x1(i-
1)+(dt/6)*(x2aux(1)+2*x2aux(2)+2*x2aux(3)+x2aux(4));
x2(i)=(-k*x(i-1)-c*x1(i-1))/m;

end

end

Finally, both set of values of displacement obtained in the two different ways are plotted
against time. The results are practically identical for both solution methods.

41
%Figures

figure(1)

subplot(2,1,1)
plot(t,u)
ylabel('Displacements, m')
xlabel('Time, s')
title('Analytical solution of a free vibration SDOF system')

subplot(2,1,2)
plot(t,x)
ylabel('Displacements, m')
xlabel('Time, s')
title('Runge-Kutta solution of a free vibration SDOF system')

Figure 8: Displacement vs time plots for a free vibration SDOF system.

The second part of the code provides the solution for a MDOF system. In this case a three story
structure was analysed. Mass was supposed to be lumped at each floor and hypothetical
values for the stiffness of the columns were used. The percentage damping and the stories
heights needs to be provided by the user as well.

%% Cantilever structure

%Input

stohig=[0,4,8,12]';

m1=2; %First story mass [T]


m2=2; %Second story mass [T]
m3=3; %Third story mass [T]
k1=3e2; %First story columns stiffness [kN/m]
k2=2e2; %Second story columns stiffness [kN/m]
k3=1e2; %Third story columns stiffness [kN/m]

psi=.05; %General damping percentage

%Assembling of matrices

42
M=[m1 0 0;
0 m2 0;
0 0 m3]; %Mass matrix
K=[2*k1+2*k2 -2*k2 0;
-2*k2 2*k2+2*k3 -2*k3;
0 -2*k3 2*k3]; %Stiffness matrix

The natural frequencies and Eigen vectors of the system are computed. The Eigen vectors were
normalized with respect to the third floor of the system.

%% Natural frequencies and Eigen vectors

%Calculation of natural frecuencies

syms w2
w2mdof=solve(det(K-w2*M)==0,w2);
wmdof=sort(real(vpa(w2mdof.^.5)));

%Calculation of eigenvectors
%Normalization about the last DOF

eigvec=zeros(length(wmdof));

for i=1:length(wmdof)

auxvec=zeros(length(wmdof),1);
auxvec(length(wmdof))=1;

auxmat=K-wmdof(i)^2*M;
auxmat(length(wmdof),:)=0;
auxmat(length(wmdof),length(wmdof))=1;

eigvecaux=linsolve(auxmat,auxvec);

eigvec(:,i)=eigvecaux;
end

The modes of the structure are shown in Figure 9.

%Figures

plotaux= zeros(length(wmdof)+1,length(wmdof));
plotaux(2:length(wmdof)+1,:)=eigvec;

figure(2)

subplot(1,3,1)
plot(zeros(4,1)',stohig,plotaux(:,1),stohig)
ylabel('Storey High, m')
xlabel('Displacements, m')
title('Mode 1')

subplot(1,3,2)
plot(zeros(4,1)',stohig,plotaux(:,2),stohig)
ylabel('Storey High, m')
xlabel('Displacements, m')
title('Mode 2')

43
subplot(1,3,3)
plot(zeros(4,1)',stohig,plotaux(:,3),stohig)
ylabel('Storey High, m')
xlabel('Displacements, m')
title('Mode 3')

Figure 9: Modes of the SDOF system.

%% Modal analysis

GMM=eigvec'*M*eigvec; %Generalized mass matrix


GKM=eigvec'*K*eigvec; %Generalized stiffness matrix

44
5. Conclusions
Developing the codes to solve some of the most common structural engineering problems
presented on the everyday practice of an engineer is helpful in order to understand the
theoretical development and programming structure of some of the most used commercial
available software. Even though the scope of the codes presented in this report is very limited,
further developments can be carry out with slight or maybe more significant modifications, it
will depend on the aim of further work in the subject, to produce a more advanced and complete
structural analysis software package, thus the codes presented set the bases for the creation of
such engineering tool.

45
6. References

Ghali, A. (2009). Structural analysis : a unified classical and matrix approach (6th ed.). London:
Taylor & Francis.
Hetnyi, M. (1946). Beams on elastic foundation : theory with applications in the fields of civil and
mechanical engineering. Ann Arbor, Mich.: University of Michigan Press.
Kassimali, A. (1999). Matrix analysis of structures. Pacific Grove, CA ; London: Brooks/Cole.

46
7. Annexes
7.1. FEM Beam code

%Code to analyse beams


%Using the code number technique
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Input Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%

%Joint data

%NCJT = Number of coordinates per joint

NCJT=2;

%COORD = Joint coordinate matrix


%NJ = Number of joints

%COORD=[X]
COORD=[0;
5];

NJ=length(COORD);

%Support data

%MSUP = Support data matrix


%NS = Number of supports

%MSUP=[NS TY RZ]
MSUP=[1 1 1;
2 1 1];

[NS x]=size(MSUP);

%Material property data

%EM = Elastic modulus vector


%NMP = Number of materials used in the structure

%EM=[E]
EM=[1.999e8];

NMP=length(EM);

%Cross-sectional property data

%CP = Cross-sectional property vector


%NCP = Number of cross-sections used in the structure (I)

%CP=[IZ]

47
CP=[6.572e-5];

NCP=length(CP);

%Member data

%MPRP = Member data matrix


%NM = Number of members

%MPRP=[NB NE EM(I) CP(I)]


MPRP=[1 2 1 1];

[NM x]=size(MPRP);

%Load data

%JP = Loaded joints vector


%PJ = Joint load data matrix
%NJL = Number of joints loaded

%JP=[NN]
JP=[];

%PJ=[FY MZ]
PJ=[];

[NJL x]=size(PJ);

%Member load data

%MP = Loaded members vector


%PM = Member load data matrix
%NML = Number of element loads

%MP=[NM LOADTYPE]
MP=[1 4];

%PM=[W,M,w,w1 0,w2 BL1 0,BL2]


PM=[50 100 1 1];

[NML x]=size(PM);

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Analysis Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Assignment of structure coordinate numbers

%NR = Number of restrained coordinates


%NDOF = Number of degrees of freedom

NR=nnz(MSUP(:,2:NCJT+1));

NDOF=NCJT*NJ-NR;

%NSC = Structure coordinate number vector


%I, I1, I2, I3, J, K, ICOUNTUNT = Counters and flags

48
NSC=zeros(NCJT*NJ,1);
I=1;
J=0;
K=NDOF;

while I<=NJ
ICOUNT=0;
I1=1;

while I1<=NS

if MSUP(I1,1)==I
ICOUNT=1;
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;

if MSUP(I1,I2+1)==1
K=K+1;
NSC(I3,1)=K;
I2=I2+1;
else J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

end

I1=I1+1;
else I1=I1+1;
end

end

if ICOUNT==0
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;
J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

I=I+1;
else I=I+1;
end

end

%Generation of the structure stiffness matrix

%JB = Initial node


%JE = Final node
%E = Young modulus
%ZI = Cross-section moment of inertia

49
%XB = X coordinate of initial node
%XE = X coordinate of final node
%BL = Length
%IM, IML, I = Counters and flags

%BK = Local stiffness matrix


%S = Structure stiffness matrix
%P = Structure load vector
%QF = Fixed-end force vector

%MSTIFFL : Function that forms the member local stiffness matrix BK


%STORES : Function that stores the perinent elements of the member
local
%stiffness matrix BK in the structure stiffness matrix S
%MFEFLL : Function that calculates the member fixed-end force vector
QF
%STOREPF : Function that stores the negative values of the pertinent
%elements of the member fixed-end force vector QF in the structure
load
%vector P

S=zeros(NDOF);
P=zeros(NDOF,1);
BK=zeros(2*NCJT);

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
ZI=CP(I);
XB=COORD(JB);
XE=COORD(JE);
BL=XE-XB;

%Call of MSTIFFL

BK=MSTIFFL(E,ZI,BL,BK);

%Call of STORES

S=STORES(JB,JE,NCJT,NDOF,NSC,BK,S);

%Member loads calculation and storage

if NML>0
QF=zeros(2*NCJT,1);
IML=1;

while IML<=NML

if IM==MP(IML,1)

50
%Call of MFEFLL

QF=MFEFLL(IML,BL,MP,PM,QF);
IML=IML+1;
else IML=IML+1;
end

end

%Call of STOREPF

P=STOREPF(JB,JE,NCJT,NDOF,NSC,QF,P);

IM=IM+1;
else IM=IM+1;
end

end

%Formation of the joint load vector

%P = Joint load vector


%I, I1, I2, N = Counters and flags

I=1;

while I<=NJL
I1=JP(I);
I2=(I1-1)*NCJT;
J=1;

while J<=NCJT
I2=I2+1;
N=NSC(I2);

if N<=NDOF
P(N)=P(N)+PJ(I,J);
J=J+1;
else J=J+1;
end
end

I=I+1;
end

%Solution for joint displacements

%d = Joint displacements vector

d=S\P;

%Calculation of member forces and support reactions

%R = Reactions vector
%U = Local displacement vector
%Q = Local force vector
%BK = Local stiffness matrix

51
%IM = Counters and flags

%MDISPL : Function that calculates the member local displacement


vector U=T*V
%MSTIFFL : Function that forms the member local stiffness matrix BK
%MFORCEL : Function that evaluates the member local force vector
Q=BK*U
%it also creates the member axial force table to show in the results
%STORER : Function that stores the pertinent elements of the member
global
%force vector F in the reaction vector R

R=zeros(NR,1);
memendfortab=zeros(NM*2,4);

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
ZI=CP(I);
XB=COORD(JB);
XE=COORD(JE);
BL=XE-XB;

U=zeros(2*NCJT,1);
BK=zeros(2*NCJT);
Q=zeros(2*NCJT,1);

%Call of MDISPL

U=MDISPL(JB,JE,NCJT,NDOF,NSC,d,U);

%Call of MSTIFFL

BK=MSTIFFL(E,ZI,BL,BK);

%Member loads calculation

QF=zeros(2*NCJT,1);

if NML>0
IML=1;

while IML<=NML

if IM==MP(IML,1)

%Call of MFEFLL

QF=MFEFLL(IML,BL,MP,PM,QF);
IML=IML+1;

52
else IML=IML+1;
end

end

%Call of MFORCEL

[Q memendfortab]=MFORCEL(JB,JE,BK,U,QF,IM,memendfortab);

else %Call of MFORCEL

[Q memendfortab]=MFORCEL(JB,JE,BK,U,QF,IM,memendfortab);
end

%Call of STORER

R=STORER(JB,JE,NCJT,NDOF,NSC,Q,R);

IM=IM+1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Results Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%

%Tables

%General structural data

COORD_table=[(1:NJ)' COORD]';
MSUP_table=MSUP';
EM_table=[(1:NMP)' EM]';
CP_table=[(1:NCP)' CP]';
MPRP_table=[(1:NM)' MPRP]';
JPPJ_table=[JP PJ]';
MEMLOAD_table=[MP PM]';

%Call of JOIDISTAB

joidistab=JOIDISTAB(NJ,NCJT,NSC,NDOF,d);

%Call of SUPREATAB

supreatab=SUPREATAB(MSUP,R,NS,NCJT);

fprintf('\t General Structural Data \n\n');


fprintf('Structure type : Beam \n');
fprintf('Number of joints : %g \n', NJ);
fprintf('Number of members : %g \n', NM);
fprintf('Number of material property sets (E) : %g \n', NMP);
fprintf('Number of cross-sectional property sets : %g \n', NCP);
fprintf('\n*User must be conscious of the use of consistent units
\n\n');
fprintf('\n\t\t Joint Coordinates \n\n');
fprintf('\t\tJoint No \tX Coordinate \n');
fprintf('\t\t\t%g \t\t\t%g \n',COORD_table);
fprintf('\n\t\t\t Supports \n\n');

53
fprintf('\t*1 represents a constraint\n\n');
fprintf('Joint No \tY Restraint \tRotational Restraint \n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \n',MSUP_table);
fprintf('\n\t\t Material Properties \n\n');
fprintf('Material No \tModulus of Elasticity (E) \n');
fprintf('\t%g \t\t\t\t\t%g \n',EM_table);
fprintf('\n\t Cross-Sectional Properties \n\n');
fprintf('\tProperty No \tMoment of Inertia (I) \n');
fprintf('\t\t%g \t\t\t\t\t%g \n',CP_table);
fprintf('\n\t\t\t\t\t\t Member Data \n\n');
fprintf('Member No \tBeginning Joint \tEnd Joint \tMaterial No
\tCross-Sectional Property No \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g \t\t\t\t\t%g
\n',MPRP_table);
fprintf('\n\t\t\t Joint Loads \n\n');
fprintf('Joint No \t\tY Force \t\tMoment \n');
fprintf('\t%g \t\t\t\t%g \t\t\t%g \n',JPPJ_table);
fprintf('\n\t\t\t Member Loads \n\n');
fprintf('Member No \tLoad Type \tMagnitude (W, M, w or w1) \tMagnitud
(w2) \tDistance l1 \tDistance l2\n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \t\t\t\t\t%g \t\t\t\t%g
\t\t\t\t%g\n',MEMLOAD_table);
fprintf('\n\t\t\t End of Input Data \n\n');
fprintf('\n\n\t\t\t Results of analysis \n');
fprintf('\n\t\t\t Joint Displacements \n\n');
fprintf('Joint No \t\tY Translation \t\t\tRotation (Rad) \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%e \n',joidistab');
fprintf('\n\t\t Member End Forces in Local Coordinates\n\n');
fprintf('Member \t\tJoint \t\tShear Force \t\tMoment \n');
fprintf('\t%g \t\t%g \t\t\t%g \t\t\t%g\n',memendfortab');
fprintf('\n\t\t\t Support reactions \n\n');
fprintf('Joint No \t\tY Force \t\t\tMoment \n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \n',supreatab');
fprintf('\n\t\t\t\t End of Analysis \n\n');

%Figures

%MFEFLLDEF : Function that calculates the member deformation, shear


%and moment due to member loads

%dia_she = Shear values along the discretized member


%dia_mom = Moment values along the discretized member
%y2 = Deflection values along the discretized member

IM=1;

while IM<=NM

%Deformed geometry, shear and moment diagrams

JB=MPRP(IM,1);
JE=MPRP(IM,2);
XB=COORD(JB,1);
XE=COORD(JE,1);
BL=XE-XB;
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
ZI=CP(I);

54
%Shape functions

%x1 = Discretization vector of the member (100 points default)

x1=linspace(0,BL);
y1=zeros(length(x1),1);

%Deflection

N1DEF=1-3*(x1/BL).^2+2*(x1/BL).^3; %Shear 1
N2DEF=BL*(x1/BL-2*(x1/BL).^2+(x1/BL).^3); %Bending 1
N3DEF=3*(x1/BL).^2-2*(x1/BL).^3; %Shear 2
N4DEF=(x1.^2/BL).*(-1+x1/BL); %Bending 2

%Moment

N1MOM=E*ZI*(12*x1/BL^3-6/BL^2);
N2MOM=E*ZI*(BL*(6*x1/BL^3-4/BL^2));
N3MOM=E*ZI*(6/BL^2-12*x1/BL^3);
N4MOM=E*ZI*(-2*(BL-3*x1)/BL^2);

%Shear

N1SHE=E*ZI*(12/BL^3);
N2SHE=E*ZI*(6/BL^2);
N3SHE=E*ZI*(-12/BL^3);
N4SHE=E*ZI*(6/BL^2);

%ampfac = Amplification factor


%dd = Joint displacements vector for each element
%ss = Joint shear vector for each element
%mm = Joint moment vector for each element
%x2 = Beam discretized points
%y2 = Beam vertical displacements

ampfac=1;
dd=[joidistab(JB,2) joidistab(JB,3) joidistab(JE,2)
joidistab(JE,3)]';

x2=x1+XB;

y2=y1'+N1DEF*dd(1)*ampfac+N2DEF*dd(2)*ampfac+N3DEF*dd(3)*ampfac+N4DEF*
dd(4)*ampfac;

%Shear diagrams

dia_she=zeros(1,length(x1));
dia_she=dia_she+N1SHE*dd(1)+N2SHE*dd(2)+N3SHE*dd(3)+N4SHE*dd(4);

%Moment diagrams

dia_mom=zeros(1,length(x1));
dia_mom=dia_mom+N1MOM*dd(1)+N2MOM*dd(2)+N3MOM*dd(3)+N4MOM*dd(4);

%Deformation, shear and moment due to element forces

if NML>0
IML=1;

55
while IML<=NML

if IM==MP(IML,1)

[y2,dia_she,dia_mom]=MFEFLLDEF(dia_she,dia_mom,IML,BL,MP,PM,y2,x1,ampf
ac,E,ZI);
IML=IML+1;

else IML=IML+1;
end

end

end

%Undeformed geometry

y=zeros(NJ,1);

figure(1)
hold on

subplot(3,1,1)
plot(COORD,y,'-o')
hold on
plot(x2,dia_she,'r')
ylabel('Shear diagram')

hold on

subplot(3,1,2)
plot(COORD,y,'-o')
hold on
plot(x2,dia_mom,'r')
ylabel('Moment diagram')

hold on

subplot(3,1,3)
plot(COORD,y,'-o')
hold on
plot(x2,y2,'r')
xlabel('Element length')
ylabel('Deflection')

IM=IM+1;

end

7.2. FEM Plane frames code

%Code to analyse plane frames


%Using the code number technique
clear all; close all; clc;

56
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Input Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%

%Joint data

%NCJT = Number of coordinates per joint

NCJT=3;

%COORD = Joint coordinate matrix


%NJ = Number of joints

%COORD=[X Y Z]
COORD=[0 0 0;
5 0 0];

[NJ x]=size(COORD);

%Support data

%MSUP = Support data matrix


%NS = Number of supports

%MSUP=[NS TX TY RZ]
MSUP=[1 1 1 0;
2 0 1 0];

[NS x]=size(MSUP);

%Material property data

%EM = Elastic modulus vector


%NMP = Number of materials used in the structure

%EM=[E]
EM=[1.999e8];

NMP=length(EM);

%Cross-sectional property data

%CP = Cross-sectional property vector


%NCP = Number of cross-sections used in the structure

%CP=[A IZ]
CP=[4.2653e-3 6.572e-5];

NCP=length(CP);

%Member data

%MPRP = Member data matrix


%NM = Number of members

%MPRP=[NB NE EM(I) CP(I)]

57
MPRP=[1 2 1 1];

[NM x]=size(MPRP);

%Load data

%JP = Loaded joints vector


%PJ = Load data matrix
%NJL = Number of joints loaded

%JP=[NN]
JP=[];

%PJ=[FX FY MZ]
PJ=[];

[NJL X]=size(PJ);

%Member load data

%MP = Loaded members vector


%PM = Member load data matrix
%NML = Number of element loads

%MP=[NM LOADTYPE]
MP=[1 6];

%PM=[W,M,w,w1 0,w2 BL1 0,BL2]


PM=[-500 -500 1 1];

[NML x]=size(PM);

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Analysis Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Assignment of structure coordinate numbers

%NR = Number of restrained coordinates


%NDOF = Number of degrees of freedom

NR=nnz(MSUP(:,2:NCJT+1));

NDOF=NCJT*NJ-NR;

%NSC = Structure coordinate number vector


%I, I1, I2, I3, J, K, ICOUNTUNT = Counters and flags

NSC=zeros(NCJT*NJ,1);
I=1;
J=0;
K=NDOF;

while I<=NJ
ICOUNT=0;
I1=1;

58
while I1<=NS

if MSUP(I1,1)==I
ICOUNT=1;
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;

if MSUP(I1,I2+1)==1
K=K+1;
NSC(I3,1)=K;
I2=I2+1;
else J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

end

I1=I1+1;
else I1=I1+1;
end

end

if ICOUNT==0
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;
J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

I=I+1;
else I=I+1;
end

end

%Generation of the structure stiffness matrix

%JB = Initial node


%JE = Final node
%E = Young modulus
%A = Cross-section area
%ZI = Cross-section moment of inertia
%XB = X coordinate of initial node
%YB = Y coordinate of initial node
%XE = X coordinate of final node
%YE = Y coordinate of final node
%BL = Length
%CX = Cosine
%CY = Sine
%IM, I = Counters and flags

%BK = Local stiffness matrix

59
%GK = Global stiffness matrix
%S = Structure stiffness matrix
%T = Transformation matrix
%QF = Fixed-end force vector
%P = Structure load vector
%FF = Member global fixed-end force vector

%MSTIFFL : Function that forms the member local stiffness matrix BK


%MSTIFFG : Function that forms the member global stiffness matrix GK
%STORES : Function that stores the perinent elements of the member
global
%stiffness matrix GK in the structure stiffness matrix S
%MTRANS : Function that forms the member transformation matrix T
%MFEFLL : Function that calculates the member fixed-end force vector
QF
%STOREPF : Function that stores the negative values of the pertinent
%elements of the member fixed-end force vector QF in the structure
load
%vector P
%MFEFG : Function that ecaluates the member global fixed-end force
vector
%FF using the relationship FF=T'*QF

GK=zeros(2*NCJT);
S=zeros(NDOF);
BK=zeros(2*NCJT);
T=zeros(2*NCJT);
P=zeros(NDOF,1);

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
A=CP(I,1);
ZI=CP(I,2);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
BL=((XE-XB)^2+(YE-YB)^2)^.5;
CX=(XE-XB)/BL;
CY=(YE-YB)/BL;

%Call of MSTIFFL

BK=MSTIFFL(E,A,ZI,BL,BK);

%Call of MTRANS

T=MTRANS(CX,CY,NCJT,T);

%Call of MSTIFFG

60
GK=MSTIFFG(BK,T,GK);

%Call of STORES

S=STORES(JB,JE,NCJT,NDOF,NSC,GK,S);

%Member loads calculation and storage

if NML>0
QF=zeros(2*NCJT,1);
IML=1;

while IML<=NML

if IM==MP(IML,1)

%Call of MFEFLL

QF=MFEFLL(IML,BL,MP,PM,QF);
IML=IML+1;
else IML=IML+1;
end

end

%Call of MFEFG

FF=zeros(2*NCJT,1);
FF=MFEFG(T,QF,FF);

%Call of STOREPF

P=STOREPF(JB,JE,NCJT,NDOF,NSC,FF,P);

IM=IM+1;
else IM=IM+1;
end
end

%Storage of joint loads into the structure load vector

%I, I1, I2, N = Counters and flags

I=1;

while I<=NJL
I1=JP(I);
I2=(I1-1)*NCJT;
J=1;

while J<=NCJT
I2=I2+1;
N=NSC(I2);

if N<=NDOF
P(N)=P(N)+PJ(I,J);

61
J=J+1;
else J=J+1;
end
end

I=I+1;
end

%Solution for joint displacements

%d = Joint displacements vector

d=S\P;

%Calculation of member forces and support reactions

%R = Reactions vector
%V = Global displacement vector
%U = Local displacement vector
%F = Global force vector
%Q = Local force vector
%IM = Counters and flags

%MDISPG : Function that determines the member global displacement


vector V
%from the joint displacement vector P
%MDISPL : Function that calculates the member local displacement
vector U=T*V
%MFORCEL : Function that evaluates the member local force vector
Q=BK*U
%it also creates the member axial force table to show in the results
%MFORCEG : Function that determines the member global force vector
F=T'*Q
%STORER : Function that stores the pertinent elements of the member
global
%force vector F in the reaction vector R

R=zeros(NR,1);
memendfortab=zeros(NM*2,5);

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
A=CP(I,1);
ZI=CP(I,2);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
BL=((XE-XB)^2+(YE-YB)^2)^.5;
CX=(XE-XB)/BL;

62
CY=(YE-YB)/BL;

V=zeros(2*NCJT,1);
T=zeros(2*NCJT);
U=zeros(2*NCJT,1);
BK=zeros(2*NCJT);
Q=zeros(2*NCJT,1);
F=zeros(2*NCJT,1);

%Call of MDISPG

V=MDISPG(JB,JE,NCJT,NDOF,NSC,d,V);

%Call of MTRANS

T=MTRANS(CX,CY,NCJT,T);

%Call of MDISPL

U=MDISPL(V,T,U);

%Call of MSTIFFL

BK=MSTIFFL(E,A,ZI,BL,BK);

%Member loads calculation and storage

QF=zeros(2*NCJT,1);

if NML>0
IML=1;

while IML<=NML

if IM==MP(IML,1)

%Call of MFEFLL

QF=MFEFLL(IML,BL,MP,PM,QF);
IML=IML+1;
else IML=IML+1;
end

end

%Call of MFORCEL

[Q memendfortab]=MFORCEL(JB,JE,BK,U,QF,IM,memendfortab);

else %Call of MFORCEL

[Q memendfortab]=MFORCEL(JB,JE,BK,U,QF,IM,memendfortab);

end

%Call of MFORCEG

63
F=MFORCEG(T,Q);

%Call of STORER

R=STORER(JB,JE,NCJT,NDOF,NSC,F,R);

IM=IM+1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Results Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%

%Tables

%General structural data

COORD_table=[(1:NJ)' COORD]';
MSUP_table=MSUP';
EM_table=[(1:NMP)' EM]';
CP_table=[(1:NCP)' CP']';
MPRP_table=[(1:NM)' MPRP]';
JPPJ_table=[JP PJ]';
MEMLOAD_table=[MP PM]';

%Call of JOIDISTAB

joidistab=JOIDISTAB(NJ,NCJT,NSC,NDOF,d);

%Call of SUPREATAB

supreatab=SUPREATAB(MSUP,R,NS,NCJT);

fprintf('\t General Structural Data \n\n');


fprintf('Structure type : Plane Frame \n');
fprintf('Number of joints : %g \n', NJ);
fprintf('Number of members : %g \n', NM);
fprintf('Number of material property sets (E) : %g \n', NMP);
fprintf('Number of cross-sectional property sets : %g \n', NCP);
fprintf('\n*User must be conscious of the use of consistent units
\n\n');
fprintf('\n\t\t Joint Coordinates \n\n');
fprintf('Joint No \tX Coordinate \tY Coordinate \n');
fprintf('\t%g \t\t\t%g \t\t\t%g \n',COORD_table);
fprintf('\n\t\t\t Supports \n\n');
fprintf('\t*1 represents a constraint\n\n');
fprintf('Joint No \tX Restraint \tY Restraint \tRotational Restraint
\n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \t\t\t\t\t%g \n',MSUP_table);
fprintf('\n\t\t Material Properties \n\n');
fprintf('Material No \tModulus of Elasticity (E) \n');
fprintf('\t%g \t\t\t\t\t%g \n',EM_table);
fprintf('\n\t Cross-Sectional Properties \n\n');
fprintf('Property No \tArea (A) \tMoment of Inertia (I) \n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \n',CP_table);
fprintf('\n\t\t\t\t\t\t Member Data \n\n');
fprintf('Member No \tBeginning Joint \tEnd Joint \tMaterial No
\tCross-Sectional Property No \n');

64
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g \t\t\t\t\t%g
\n',MPRP_table);
fprintf('\n\t\t\t\t\t Joint Loads \n\n');
fprintf('Joint No \t\tX Force \t\tY Force \t\tMoment \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g \n',JPPJ_table);
fprintf('\n\t\t\t\t\t Member Loads \n\n');
fprintf('Member No \tLoad Type \tMagnitude (W, M, w or w1) \tMagnitud
(w2) \tDistance l1 \tDistance l2\n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \t\t\t\t\t%g \t\t\t\t%g
\t\t\t\t%g\n',MEMLOAD_table);
fprintf('\n\t\t\t End of Input Data \n\n');
fprintf('\n\n\t\t\t Results of analysis \n');
fprintf('\n\t\t\t Joint Displacements \n\n');
fprintf('Joint No \t\tX Translation \t\t\tY Translation \t\t\tRotation
(Rad) \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t\t\t%e \n',joidistab');
fprintf('\n\t\t Member End Forces in Local Coordinates\n\n');
fprintf('Member \t\tJoint \t\tAxial Force \t\tShear Force \t\tMoment
\n');
fprintf('\t%g \t\t%g \t\t\t%g \t\t\t%g \t\t\t%g\n',memendfortab');
fprintf('\n\t\t\t\t\t Support reactions \n\n');
fprintf('Joint No \t\tX Force \t\tY Force \t\t\tMoment \n');
fprintf('\t%g \t\t\t%g \t\t\t%g \t\t\t\t%g \n',supreatab');
fprintf('\n\t\t\t\t End of Analysis \n\n');

%Figures

%MFEFLLDEF : Function that calculates the member deformation, shear


%moment and axial due to member loads

%dia_axi = Axial values along the discretized member


%dia_she = Shear values along the discretized member
%dia_mom = Moment values along the discretized member
%y2 = Deflection values along the discretized member

IM=1;
while IM<=NM

%%Deformed geometry, axial, shear and moment diagrams

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
A=CP(I,1);
ZI=CP(I,2);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
BL=((XE-XB)^2+(YE-YB)^2)^.5;
CX=(XE-XB)/BL;
CY=(YE-YB)/BL;

%Shape functions

65
%x1 = Discretization vector of the member (100 points default)

x1=linspace(0,BL);
y1=zeros(length(x1),1);

%Deflection

N1DEF=1-x1/BL; %Axial 1
N2DEF=1-3*(x1/BL).^2+2*(x1/BL).^3; %Shear 1
N3DEF=BL*(x1/BL-2*(x1/BL).^2+(x1/BL).^3); %Bending 1
N4DEF=x1/BL; %Axial 2
N5DEF=3*(x1/BL).^2-2*(x1/BL).^3; %Shear 2
N6DEF=(x1.^2/BL).*(-1+x1/BL); %Bending 2

%Moment

N1MOM=E*ZI*(12*x1/BL^3-6/BL^2);
N2MOM=E*ZI*(BL*(6*x1/BL^3-4/BL^2));
N3MOM=E*ZI*(6/BL^2-12*x1/BL^3);
N4MOM=E*ZI*(-2*(BL-3*x1)/BL^2);

%Shear

N1SHE=E*ZI*(12/BL^3);
N2SHE=E*ZI*(6/BL^2);
N3SHE=E*ZI*(-12/BL^3);
N4SHE=E*ZI*(6/BL^2);

%Axial

N1AXI=-E*A/BL;
N2AXI=E*A/BL;

%ampfac = Amplification factor


%dd = Joint displacements vector for each element
%aa = Joint axial vector for each element
%ss = Joint shear vector for each element
%mm = Joint moment vector for each element
%x2 = Beam discretized points
%y2 = Beam vertical displacements

ampfac=1;
ampfacaxi=10;
dd=[joidistab(JB,2) joidistab(JB,3) joidistab(JB,4)
joidistab(JE,2) joidistab(JE,3) joidistab(JE,4)]';

x2=x1+XB+N1DEF*dd(1)*ampfacaxi+N4DEF*dd(4)*ampfacaxi;

y2=y1'+N2DEF*dd(2)*ampfac+N3DEF*dd(3)*ampfac+N5DEF*dd(5)*ampfac+N6DEF*
dd(6)*ampfac;

%Axial diagrams

dia_axi=zeros(1,length(x1));
dia_axi=dia_axi+N1AXI*dd(1)+N2AXI*dd(4);

%Shear diagrams

66
dia_she=zeros(1,length(x1));
dia_she=dia_she+N1SHE*dd(2)+N2SHE*dd(3)+N3SHE*dd(5)+N4SHE*dd(6);

%Moment diagrams

dia_mom=zeros(1,length(x1));
dia_mom=dia_mom+N1MOM*dd(2)+N2MOM*dd(3)+N3MOM*dd(5)+N4MOM*dd(6);

%Deformation, axial, shear and moment due to element forces

if NML>0
IML=1;

while IML<=NML

if IM==MP(IML,1)

[x2,y2,dia_she,dia_mom,dia_axi]=MFEFLLDEF(ampfacaxi,dia_axi,dia_she,di
a_mom,IML,BL,MP,PM,y2,x2,x1,ampfac,E,ZI,A);
IML=IML+1;

else IML=IML+1;
end

end

end

%Undeformed geometry

x=[XB;XE];
y=[YB;YE];

figure(1)
hold on

subplot(4,1,1)
plot(x,y,'-o')
hold on
plot(x2,dia_axi,'r')
ylabel('Axial diagram')

hold on

subplot(4,1,2)
plot(x,y,'-o')
hold on
plot(x2,dia_she,'r')
ylabel('Shear diagram')

hold on

subplot(4,1,3)
plot(x,y,'-o')
hold on
plot(x2,dia_mom,'r')
ylabel('Moment diagram')

67
hold on

subplot(4,1,4)
plot(x,y,'-o')
hold on
plot(x2,y2,'r')
xlabel('Element length')
ylabel('Deformation')

IM=IM+1;
end

7.3. FEM Space trusses code

%Code to analyse space trusses


%Using the code number technique
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Input Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%

%Joint data

%COORD = Joint coordinate matrix


%NJ = Number of joints

COORD=[-72 0 96;
144 0 96;
72 0 -96;
-144 0 -96;
0 288 0];

[NJ x]=size(COORD);

%Support data

%MSUP = Support data matrix


%NS = Number of supports

MSUP=[1 1 1 1;
2 1 1 1;
3 1 1 1;
4 1 1 1];

[NS x]=size(MSUP);

%Material property data

%EM = Elastic modulus vector


%NMP = Number of materials used in the structure

EM=[10000];

68
NMP=length(EM);

%Cross-sectional property data

%CP = Cross-sectional property vector


%NCP = Number of cross-sections used in the structure

CP=[8.4];

NCP=length(CP);

%Member data

%MPRP = Member data matrix


%NM = Number of members

MPRP=[1 5 1 1;
2 5 1 1;
3 5 1 1;
4 5 1 1];

[NM x]=size(MPRP);

%Load data

%JP = Loaded joints vector


%PJ = Load data matrix
%NJL = Number of joints loaded
%NCJT = Number of coordinates per joint

JP=[5];

PJ=[0 -100 -50];

[NJL NCJT]=size(PJ);

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Analysis Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Assignment of structure coordinate numbers

%NR = Number of restrained coordinates


%NDOF = Number of degrees of freedom

NR=nnz(MSUP(:,2:NCJT+1));

NDOF=NCJT*NJ-NR;

%NSC = Structure coordinate number vector


%I, I1, I2, I3, J, K, ICOUNTUNT = Counters and flags

NSC=zeros(NCJT*NJ,1);
I=1;
J=0;
K=NDOF;

69
while I<=NJ
ICOUNT=0;
I1=1;

while I1<=NS

if MSUP(I1,1)==I
ICOUNT=1;
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;

if MSUP(I1,I2+1)==1
K=K+1;
NSC(I3,1)=K;
I2=I2+1;
else J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

end

I1=I1+1;
else I1=I1+1;
end

end

if ICOUNT==0
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;
J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

I=I+1;
else I=I+1;
end

end

%Generation of the structure stiffness matrix

%JB = Initial node


%JE = Final node
%E = Young modulus
%A = Cross-section area
%XB = X coordinate of initial node
%YB = Y coordinate of initial node
%ZB = Z coordinate of initial node
%XE = X coordinate of final node
%YE = Y coordinate of final node
%ZE = Z coordinate of final node
%BL = Length

70
%CX = X cosine
%CY = Y cosine
%CZ = Z cosine
%IM, I = Counters and flags

%GK = Global stiffness matrix


%S = Structure stiffness matrix

%MSTIFFG : Function that forms the member global stiffness matrix GK


%STORES : Function that stores the perinent elements of the member
global
%stiffness matrix GK in the structure stiffness matrix S

GK=zeros(2*NCJT);
S=zeros(NDOF);

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
A=CP(I);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
ZB=COORD(JB,3);
ZE=COORD(JE,3);
BL=((XE-XB)^2+(YE-YB)^2+(ZE-ZB)^2)^.5;
CX=(XE-XB)/BL;
CY=(YE-YB)/BL;
CZ=(ZE-ZB)/BL;

%Call of MSTIFFG

GK=MSTIFFG(E,A,BL,CX,CY,CZ,GK);

%Call of STORES

S=STORES(JB,JE,NCJT,NDOF,NSC,GK,S);

IM=IM+1;
end

%Formation of the joint load vector

%P = Joint load vector


%I, I1, I2, N = Counters and flags

P=zeros(NDOF,1);
I=1;

71
while I<=NJL
I1=JP(I);
I2=(I1-1)*NCJT;
J=1;

while J<=NCJT
I2=I2+1;
N=NSC(I2);

if N<=NDOF
P(N)=P(N)+PJ(I,J);
J=J+1;
else J=J+1;
end
end

I=I+1;
end

%Solution for joint displacements

%d = Joint displacements vector

d=S\P;

%Calculation of member forces and support reactions

%R = Reactions vector
%V = Global displacement vector
%T = Transformation matrix
%U = Local displacement vector
%F = Global force vector
%Q = Local force vector
%BK = Local stiffness matrix
%IM = Counters and flags

%MDISPG : Function that determines the member global displacement


vector V
%from the joint displacement vector P
%MTRANS : Function that forms the member transformation matrix T
%MDISPL : Function that calculates the member local displacement
vector U=T*V
%MSTIFFL : Function that forms the member local stiffness matrix BK
%MFORCEL : Function that evaluates the member local force vector
Q=BK*U
%it also creates the member axial force table to show in the results
%MFORCEG : Function that determines the member global force vector
F=T'*Q
%STORER : Function that stores the pertinent elements of the member
global
%force vector F in the reaction vector R

R=zeros(NR,1);
memaxifortab=zeros(NM,2);
memaxifortab(:,1)=(1:NM)';

IM=1;

while IM<=NM

72
%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
A=CP(I);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
ZB=COORD(JB,3);
ZE=COORD(JE,3);
BL=((XE-XB)^2+(YE-YB)^2+(ZE-ZB)^2)^.5;
CX=(XE-XB)/BL;
CY=(YE-YB)/BL;
CZ=(ZE-ZB)/BL;

V=zeros(2*NCJT,1);
T=zeros(2,2*NCJT);
U=zeros(2,1);
BK=zeros(2,2);
Q=zeros(2,1);
F=zeros(2*NCJT,1);

%Call of MDISPG

V=MDISPG(JB,JE,NCJT,NDOF,NSC,d,V);

%Call of MTRANS

T=MTRANS(CX,CY,CZ,T);

%Call of MDISPL

U=MDISPL(V,T,U);

%Call of MSTIFFL

BK=MSTIFFL(E,A,BL,BK);

%Call of MFORCEL

[Q memaxifortab]=MFORCEL(BK,U,IM,memaxifortab);

%Call of MFORCEG

F=MFORCEG(T,Q);

%Call of STORER

R=STORER(JB,JE,NCJT,NDOF,NSC,F,R);

IM=IM+1;
end

73
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Results Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%

%Tables

%General structural data

COORD_table=[(1:NJ)' COORD]';
MSUP_table=MSUP';
EM_table=[(1:NMP)' EM]';
CP_table=[(1:NCP)' CP]';
MPRP_table=[(1:NM)' MPRP]';
JPPJ_table=[JP PJ]';

%Call of JOIDISTAB

joidistab=JOIDISTAB(NJ,NCJT,NSC,NDOF,d);

%Call of SUPREATAB

supreatab=SUPREATAB(MSUP,R,NS,NCJT);

fprintf('\t General Structural Data \n\n');


fprintf('Structure type : Space Truss \n');
fprintf('Number of joints : %g \n', NJ);
fprintf('Number of members : %g \n', NM);
fprintf('Number of material property sets (E) : %g \n', NMP);
fprintf('Number of cross-sectional property sets : %g \n', NCP);
fprintf('\n*User must be conscious of the use of consistent units
\n\n');
fprintf('\n\t\t\t\t Joint Coordinates \n\n');
fprintf('Joint No \tX Coordinate \tY Coordinate \tZ Coordinate\n');
fprintf('\t%g \t\t\t%g \t\t\t%g \t\t\t%g\n',COORD_table);
fprintf('\n\t\t\t\t\t Supports \n\n');
fprintf('\t\t\t*1 represents a restraint\n\n');
fprintf('Joint No \tX Restraint \tY Restraint \tZ Restraint\n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \t\t\t\t%g\n',MSUP_table);
fprintf('\n\t\t\t\t Material Properties \n\n');
fprintf('\t\tMaterial No \tModulus of Elasticity (E) \n');
fprintf('\t\t\t%g \t\t\t\t\t%g \n',EM_table);
fprintf('\n\t\t\t Cross-Sectional Properties \n\n');
fprintf('\t\t\t\tProperty No \tArea (A) \n');
fprintf('\t\t\t\t\t%g \t\t\t\t%g \n',CP_table);
fprintf('\n\t\t\t\t\t\t Member Data \n\n');
fprintf('Member No \tBeginning Joint \tEnd Joint \tMaterial No
\tCross-Sectional Property No \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g \t\t\t\t\t%g
\n',MPRP_table);
fprintf('\n\t\t\t\t\t Joint Loads \n\n');
fprintf('Joint No \t\tX Force \t\tY Force \t\tZ Force\n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g\n',JPPJ_table);
fprintf('\n\t\t\t\t End of Input Data \n\n');
fprintf('\n\n\t\t\t\t Results of analysis \n');
fprintf('\n\t\t\t\t Joint Displacements \n\n');
fprintf('Joint No \t\tX Translation \t\t\tY Translation \t\t\tZ
Translation\n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t\t%g\n',joidistab');
fprintf('\n\t\t\t Member Axial Forces \n\n');

74
fprintf('\t\t *Values < 0 represent Tension \n\n');
fprintf('\t\t\tMember \t\tAxial Force \n');
fprintf('\t\t\t\t%g \t\t\t%g \n',memaxifortab');
fprintf('\n\t\t\t Support reactions \n\n');
fprintf('Joint No \t\tX Force \t\t\tY Force \t\t\tZ Force\n');
fprintf('\t%g \t\t\t\t%g \t\t\t%g \t\t\t%g\n',supreatab');
fprintf('\n\t\t\t\t End of Analysis \n\n');

%Figures

IM=1;
while IM<=NM

%Undeformed geometry

JB=MPRP(IM,1);
JE=MPRP(IM,2);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
ZB=COORD(JB,3);
ZE=COORD(JE,3);
BL=((XE-XB)^2+(YE-YB)^2+(ZE-ZB)^2)^.5;

x1=[XB;XE];
y1=[YB;YE];
z1=[ZB;ZE];

figure(1)
hold on
plot3(x1,y1,z1,'-o')

%Deformed geometry

%ampfac = Amplification factor

ampfac=100;

XBD=XB+joidistab(JB,2)*ampfac;
XED=XE+joidistab(JE,2)*ampfac;
YBD=YB+joidistab(JB,3)*ampfac;
YED=YE+joidistab(JE,3)*ampfac;
ZBD=ZB+joidistab(JB,4)*ampfac;
ZED=ZE+joidistab(JE,4)*ampfac;

x2=[XBD;XED];
y2=[YBD;YED];
z2=[ZBD,ZED];

plot3(x2,y2,z2,'r--x')
xlabel('X axis');
ylabel('Y axis');
zlabel('Z axis');

IM=IM+1;
end

75
7.4. FEM Grids code

%Code to analyse grids


%Using the code number technique
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Input Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%

%Joint data

%NCJT = Number of coordinates per joint

NCJT=3;

%COORD = Joint coordinate matrix


%NJ = Number of joints

COORD=[0 0;
8 0;
0 6;
8 6];

[NJ x]=size(COORD);

%Support data

%MSUP = Support data matrix


%NS = Number of supports

MSUP=[1 1 1 1;
2 1 1 1
3 1 1 1];

[NS x]=size(MSUP);

%Material property data

%EM = Elastic modulus vector


%NMP = Number of materials used in the structure

EM=[200e6 76e6];

[NMP x]=size(EM);

%Cross-sectional property data

%CP = Cross-sectional property vector


%NCP = Number of cross-sections used in the structure

CP=[115e-6 347e-6];

[NCP x]=size(CP);

%Member data

76
%MPRP = Member data matrix
%NM = Number of members

MPRP=[1 4 1 1;
2 4 1 1;
3 4 1 1];

[NM x]=size(MPRP);

%Load data

%JP = Loaded joints vector


%PJ = Load data matrix
%NJL = Number of joints loaded

JP=[];

PJ=[];

[NJL x]=size(PJ);

%Member load data

%MP = Loaded members vector


%PM = Member load data matrix
%NML = Number of element loads

MP=[2 3;
3 3];

PM=[20 0 0 0;
20 0 0 0];

[NML x]=size(PM);

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Analysis Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Assignment of structure coordinate numbers

%NR = Number of restrained coordinates


%NDOF = Number of degrees of freedom

NR=nnz(MSUP(:,2:NCJT+1));

NDOF=NCJT*NJ-NR;

%NSC = Structure coordinate number vector


%I, I1, I2, I3, J, K, ICOUNTUNT = Counters and flags

NSC=zeros(NCJT*NJ,1);
I=1;
J=0;
K=NDOF;

77
while I<=NJ
ICOUNT=0;
I1=1;

while I1<=NS

if MSUP(I1,1)==I
ICOUNT=1;
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;

if MSUP(I1,I2+1)==1
K=K+1;
NSC(I3,1)=K;
I2=I2+1;
else J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

end

I1=I1+1;
else I1=I1+1;
end

end

if ICOUNT==0
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;
J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

I=I+1;
else I=I+1;
end

end

%Generation of the structure stiffness matrix

%JB = Initial node


%JE = Final node
%E = Young modulus
%G = Shear modulus
%J = Cross-section torsion constant
%ZI = Cross-section moment of inertia
%XB = X coordinate of initial node
%ZB = Z coordinate of initial node
%XE = X coordinate of final node
%ZE = Z coordinate of final node
%BL = Length

78
%CX = X cosine
%CZ = Z cosine
%IM, I = Counters and flags

%BK = Local stiffness matrix


%GK = Global stiffness matrix
%S = Structure stiffness matrix
%T = Transformation matrix
%QF = Fixed-end force vector
%P = Structure load vector
%FF = Member global fixed-end force vector

%MSTIFFL : Function that forms the member local stiffness matrix BK


%MSTIFFG : Function that forms the member global stiffness matrix GK
%STORES : Function that stores the perinent elements of the member
global
%stiffness matrix GK in the structure stiffness matrix S
%MTRANS : Function that forms the member transformation matrix T
%MFEFLL : Function that calculates the member fixed-end force vector
QF
%STOREPF : Function that stores the negative values of the pertinent
%elements of the member fixed-end force vector QF in the structure
load
%vector P
%MFEFG : Function that ecaluates the member global fixed-end force
vector
%FF using the relationship FF=T'*QF

GK=zeros(2*NCJT);
S=zeros(NDOF);
BK=zeros(2*NCJT);
T=zeros(2*NCJT);
P=zeros(NDOF,1);

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I,1);
G=EM(I,2);
I=MPRP(IM,4);
J=CP(I,1);
ZI=CP(I,2);
XB=COORD(JB,1);
XE=COORD(JE,1);
ZB=COORD(JB,2);
ZE=COORD(JE,2);
BL=((XE-XB)^2+(ZE-ZB)^2)^.5;
CX=(XE-XB)/BL;
CZ=(ZE-ZB)/BL;

%Call of MSTIFFL

BK=MSTIFFL(E,G,J,ZI,BL,BK);

79
%Call of MTRANS

T=MTRANS(CX,CZ,T);

%Call of MSTIFFG

GK=MSTIFFG(BK,T,GK);

%Call of STORES

S=STORES(JB,JE,NCJT,NDOF,NSC,GK,S);

%Member loads calculation and storage

if NML>0
QF=zeros(2*NCJT,1);
IML=1;

while IML<=NML

if IM==MP(IML,1)

%Call of MFEFLL

QF=MFEFLL(IML,BL,MP,PM,QF);
IML=IML+1;
else IML=IML+1;
end

end

%Call of MFEFG

FF=zeros(2*NCJT,1);
FF=MFEFG(T,QF,FF);

%Call of STOREPF

P=STOREPF(JB,JE,NCJT,NDOF,NSC,FF,P);

IM=IM+1;
else IM=IM+1;
end
end

%Storage of joint loads into the structure load vector

%I, I1, I2, N = Counters and flags

I=1;

while I<=NJL
I1=JP(I);
I2=(I1-1)*NCJT;
J=1;

80
while J<=NCJT
I2=I2+1;
N=NSC(I2);

if N<=NDOF
P(N)=P(N)+PJ(I,J);
J=J+1;
else J=J+1;
end
end

I=I+1;
end

%Solution for joint displacements

%d = Joint displacements vector

d=S\P;

%Calculation of member forces and support reactions

%R = Reactions vector
%V = Global displacement vector
%U = Local displacement vector
%F = Global force vector
%Q = Local force vector
%IM = Counters and flags

%MDISPG : Function that determines the member global displacement


vector V
%from the joint displacement vector P
%MDISPL : Function that calculates the member local displacement
vector U=T*V
%MFORCEL : Function that evaluates the member local force vector
Q=BK*U
%it also creates the member axial force table to show in the results
%MFORCEG : Function that determines the member global force vector
F=T'*Q
%STORER : Function that stores the pertinent elements of the member
global
%force vector F in the reaction vector R

R=zeros(NR,1);
memendfortab=zeros(NM*2,5);

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I);
I=MPRP(IM,4);
A=CP(I,1);
ZI=CP(I,2);

81
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
BL=((XE-XB)^2+(YE-YB)^2)^.5;
CX=(XE-XB)/BL;
CY=(YE-YB)/BL;

V=zeros(2*NCJT,1);
T=zeros(2*NCJT);
U=zeros(2*NCJT,1);
BK=zeros(2*NCJT);
Q=zeros(2*NCJT,1);
F=zeros(2*NCJT,1);

%Call of MDISPG

V=MDISPG(JB,JE,NCJT,NDOF,NSC,d,V);

%Call of MTRANS

T=MTRANS(CX,CY,T);

%Call of MDISPL

U=MDISPL(V,T,U);

%Call of MSTIFFL

BK=MSTIFFL(E,G,J,ZI,BL,BK);

%Member loads calculation and storage

QF=zeros(2*NCJT,1);

if NML>0
IML=1;

while IML<=NML

if IM==MP(IML,1)

%Call of MFEFLL

QF=MFEFLL(IML,BL,MP,PM,QF);
IML=IML+1;
else IML=IML+1;
end

end

%Call of MFORCEL

[Q memendfortab]=MFORCEL(JB,JE,BK,U,QF,IM,memendfortab);

else %Call of MFORCEL

82
[Q memendfortab]=MFORCEL(JB,JE,BK,U,QF,IM,memendfortab);

end

%Call of MFORCEG

F=MFORCEG(T,Q);

%Call of STORER

R=STORER(JB,JE,NCJT,NDOF,NSC,F,R);

IM=IM+1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Results Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%

%Tables

%General structural data

COORD_table=[(1:NJ)' COORD]';
MSUP_table=MSUP';
EM_table=[(1:NMP)' EM]';
CP_table=[(1:NCP)' CP]';
MPRP_table=[(1:NM)' MPRP]';
JPPJ_table=[JP PJ]';
MEMLOAD_table=[MP PM]';

%Call of JOIDISTAB

joidistab=JOIDISTAB(NJ,NCJT,NSC,NDOF,d);

%Call of SUPREATAB

supreatab=SUPREATAB(MSUP,R,NS,NCJT);

fprintf('\t General Structural Data \n\n');


fprintf('Structure type : Grid \n');
fprintf('Number of joints : %g \n', NJ);
fprintf('Number of members : %g \n', NM);
fprintf('Number of material property sets (E - G) : %g \n', NMP);
fprintf('Number of cross-sectional property sets (I - J) : %g \n',
NCP);
fprintf('\n*User must be conscious of the use of consistent units
\n\n');
fprintf('\n\t\t Joint Coordinates \n\n');
fprintf('Joint No \tX Coordinate \tZ Coordinate \n');
fprintf('\t%g \t\t\t%g \t\t\t%g \n',COORD_table);
fprintf('\n\t\t\t Supports \n\n');
fprintf('\t*1 represents a constraint\n\n');
fprintf('Joint No \tY Restraint \tXX Restraint \tZZ Restraint \n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \t\t\t\t\t%g \n',MSUP_table);
fprintf('\n\t\t Material Properties \n\n');
fprintf('Material No \tModulus of Elasticity (E) \tShear Modulus (G)
\n');

83
fprintf('\t%g \t\t\t\t\t%g \t\t\t\t\t%g \n',EM_table);
fprintf('\n\t\t\t Cross-Sectional Properties \n\n');
fprintf('Property No \tPolar Moment of Inertia (J) \tMoment of Inertia
(I) \n');
fprintf('\t%g \t\t\t\t\t%g \t\t\t\t\t%g \n',CP_table);
fprintf('\n\t\t\t\t\t\t Member Data \n\n');
fprintf('Member No \tBeginning Joint \tEnd Joint \tMaterial No
\tCross-Sectional Property No \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g \t\t\t\t\t%g
\n',MPRP_table);
fprintf('\n\t\t\t\t\t Joint Loads \n\n');
fprintf('Joint No \t\tX Force \t\tY Force \t\tMoment \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g \n',JPPJ_table);
fprintf('\n\t\t\t\t\t\t\t Member Loads \n\n');
fprintf('Member No \tLoad Type \tMagnitude (W, M, w or w1) \tMagnitud
(w2) \tDistance l1 \tDistance l2\n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \t\t\t\t\t%g \t\t\t\t%g
\t\t\t\t%g\n',MEMLOAD_table);
fprintf('\n\t\t\t\t\t End of Input Data \n\n');
fprintf('\n\n\t\t\t\t\t Results of analysis \n');
fprintf('\n\t\t\t\t\t Joint Displacements \n\n');
fprintf('Joint No \t\tY Translation \t\t\tXX Rotation (Rad) \t\t\tZZ
Rotation (Rad) \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t\t\t%e \n',joidistab');
fprintf('\n\t\t Member End Forces in Local Coordinates\n\n');
fprintf('Member \t\tJoint \t\tShear Force \t\tXX Moment \t\tZZ Moment
\n');
fprintf('\t%g \t\t%g \t\t\t%g \t\t\t%g \t\t\t%g\n',memendfortab');
fprintf('\n\t\t\t\t\t Support reactions \n\n');
fprintf('Joint No \t\tX Force \t\tY Force \t\t\tMoment \n');
fprintf('\t%g \t\t\t%g \t\t\t%g \t\t\t\t%g \n',supreatab');
fprintf('\n\t\t\t\t End of Analysis \n\n');

%Figures

IM=1;
while IM<=NM

%Undeformed geometry

JB=MPRP(IM,1);
JE=MPRP(IM,2);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
BL=((XE-XB)^2+(YE-YB)^2)^.5;

x=[XB;XE];
y=[YB;YE];

figure(1)
hold on
plot(x,y,'-o')

% %Shape functions
%
% %x1 = Discretization vector of the member (100 points default)
%
% x1=linspace(0,BL);

84
% y1=zeros(length(x1),1);
%
% N1=1-x1/BL; %Axial 1
% N2=1-3*(x1/BL).^2+2*(x1/BL).^3; %Shear 1
% N3=x1.*(1-x1/BL).^2; %Bending 1
% N4=x1/BL; %Axial
% N5=3*(x1/BL).^2-2*(x1/BL).^3; %Shear 2
% N6=(x1.^2/BL).*(-1+x1/BL); %Bending 2
%
% %Deformed shape
%
% %ampfac = Amplification factor
% %dd = Joint displacements vector for each element
%
% ampfac=10;
% dd=[joidistab(JB,2) joidistab(JB,3) joidistab(JB,4)
joidistab(JE,2) joidistab(JE,3) joidistab(JE,4)]';
%
% x2=x1+N1*dd(1)*ampfac+N4*dd(4)*ampfac;
%
y2=y1'+N2*dd(2)*ampfac+N3*dd(3)*ampfac+N5*dd(5)*ampfac+N6*dd(6)*ampfac
;
%
% figure(2)
% subplot(NM,1,IM)
% plot(x1,y1,x2,y2,'r')
%
% %Axial diagrams
%
% figure(5)
% subplot(NM,1,IM)
%
% xaxi=linspace(0,BL);
% yaxi=ones(1,length(xaxi))*memendfortab(IM*2-1,3);
%
% area(xaxi,yaxi)
%
% %Shear diagrams
%
%
%
% %Moment diagrams
%

IM=IM+1;
end

7.5. FEM Space frames code

%Code to analyse space frames


%Using the code number technique
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Input Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%

%Joint data

85
%NCJT = Number of coordinates per joint

NCJT=6;

%COORD = Joint coordinate matrix


%NJ = Number of joints

%COORD=[X Y Z]
COORD=[0 0 0;
-240 0 0;
0 -240 0;
0 0 -240];

[NJ x]=size(COORD);

%Support data

%MSUP = Support data matrix


%NS = Number of supports

%MSUP=[NS TX TY TZ RX RY RZ]
MSUP=[2 1 1 1 1 1 1;
3 1 1 1 1 1 1;
4 1 1 1 1 1 1];

[NS x]=size(MSUP);

%Material property data

%EM = Elastic modulus vector


%NMP = Number of materials used in the structure

%EM=[E G]
EM=[29000 11500];

[NMP x]=size(EM);

%Cross-sectional property data

%CP = Cross-sectional property vector


%NCP = Number of cross-sections used in the structure

%CP=[A IZ IY J]
CP=[32.9 716 236 15.1];

[NCP x]=size(CP);

%Member data

%MPRP = Member data matrix


%NM = Number of members
%NB = Number of begining joint
%NE = Number of ending joint

%MPRP=[NB NE NMP NCP PSI]


MPRP=[2 1 1 1 0;

86
3 1 1 1 90;
4 1 1 1 30];

[NM x]=size(MPRP);

%Load data

%JP = Loaded joints vector


%PJ = Load data matrix
%NJL = Number of joints loaded

%JP=[NJ]
JP=[1];

%PJ[PX PY PZ MX MY MZ]
PJ=[0 0 0 -1800 0 1800];

[NJL X]=size(PJ);

%Member load data

%MP = Loaded members vector


%PM = Member load data matrix
%NML = Number of element loads

%MP=[NM LOADTYPE]
MP=[1 3];

%PM[W,M,w,w1,MT 0,w2 BL1 0,BL2 x=0,y=1,z=2]


PM=[.25 0 0 0 1];

[NML x]=size(PM);

%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Analysis Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Assignment of structure coordinate numbers

%NR = Number of restrained coordinates


%NDOF = Number of degrees of freedom

NR=nnz(MSUP(:,2:NCJT+1));

NDOF=NCJT*NJ-NR;

%NSC = Structure coordinate number vector


%I, I1, I2, I3, J, K, ICOUNTUNT = Counters and flags

NSC=zeros(NCJT*NJ,1);
I=1;
J=0;
K=NDOF;

while I<=NJ
ICOUNT=0;
I1=1;

87
while I1<=NS

if MSUP(I1,1)==I
ICOUNT=1;
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;

if MSUP(I1,I2+1)==1
K=K+1;
NSC(I3,1)=K;
I2=I2+1;
else J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

end

I1=I1+1;
else I1=I1+1;
end

end

if ICOUNT==0
I2=1;

while I2<=NCJT
I3=(I-1)*NCJT+I2;
J=J+1;
NSC(I3,1)=J;
I2=I2+1;
end

I=I+1;
else I=I+1;
end

end

% Generation of the structure stiffness matrix

% JB = Initial node
% JE = Final node
% E = Young modulus
% G = Shear modulus
% A = Cross-section area
% IZ = Cross-section moment of inertia about Z
% IY = Cross-section moment of inertia about Y
% J = Tortional constant
% XB = X coordinate of initial node
% YB = Y coordinate of initial node
% ZB = Z coordinate of initial node
% XE = X coordinate of final node
% YE = Y coordinate of final node
% ZE = Z coordinate of final node

88
% BL = Length
% rxX, rxY, rxZ = Cosines about X axis
% PSI = Roll angle
% IM, I = Counters and flags

% BK = Local stiffness matrix


% GK = Global stiffness matrix
% S = Structure stiffness matrix
% T = Transformation matrix
% QF = Fixed-end force vector
% P = Structure load vector
% FF = Member global fixed-end force vector

% MSTIFFL : Function that forms the member local stiffness matrix BK


% MSTIFFG : Function that forms the member global stiffness matrix GK
% STORES : Function that stores the perinent elements of the member
global
% stiffness matrix GK in the structure stiffness matrix S
% MTRANS : Function that forms the member transformation matrix T
% MFEFLL : Function that calculates the member fixed-end force vector
QF
% STOREPF : Function that stores the negative values of the pertinent
% elements of the member fixed-end force vector QF in the structure
load
% vector P
% MFEFG : Function that ecaluates the member global fixed-end force
vector
% FF using the relationship FF=T'*QF

GK=zeros(2*NCJT);
S=zeros(NDOF);
BK=zeros(2*NCJT);
T=zeros(2*NCJT);
P=zeros(NDOF,1);

IM=1;

while IM<=NM

%Evaluation of member properties

JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I,1);
G=EM(I,2);
I=MPRP(IM,4);
A=CP(I,1);
IZ=CP(I,2);
IY=CP(I,3);
J=CP(I,4);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
ZB=COORD(JB,3);
ZE=COORD(JE,3);
BL=((XE-XB)^2+(YE-YB)^2+(ZE-ZB)^2)^.5;
rxX=(XE-XB)/BL;
rxY=(YE-YB)/BL;

89
rxZ=(ZE-ZB)/BL;
PSI=MPRP(IM,5);

%Call of MSTIFFL

BK=MSTIFFL(E,G,A,IZ,IY,J,BL,BK);

%Call of MTRANS

T=MTRANS(rxX,rxY,rxZ,PSI,XB,XE,ZB,ZE,T);

%Call of MSTIFFG

GK=MSTIFFG(BK,T,GK);

%Call of STORES

S=STORES(JB,JE,NCJT,NDOF,NSC,GK,S);

%Member loads calculation and storage

if NML>0
QF=zeros(2*NCJT,1);
IML=1;

while IML<=NML

if IM==MP(IML,1)

%Call of MFEFLL

QF=MFEFLL(IML,BL,MP,PM,QF);
IML=IML+1;
else IML=IML+1;
end

end

%Call of MFEFG

FF=zeros(2*NCJT,1);
FF=MFEFG(T,QF,FF);

%Call of STOREPF

P=STOREPF(JB,JE,NCJT,NDOF,NSC,FF,P);

IM=IM+1;
else IM=IM+1;
end
end

%Storage of joint loads into the structure load vector

%I, I1, I2, N = Counters and flags

I=1;

90
while I<=NJL
I1=JP(I);
I2=(I1-1)*NCJT;
J=1;

while J<=NCJT
I2=I2+1;
N=NSC(I2);

if N<=NDOF
P(N)=P(N)+PJ(I,J);
J=J+1;
else J=J+1;
end
end

I=I+1;
end

%Solution for joint displacements

%d = Joint displacements vector

d=S\P;

%Calculation of member forces and support reactions

%R = Reactions vector
%V = Global displacement vector
%U = Local displacement vector
%F = Global force vector
%Q = Local force vector
%IM = Counters and flags

%MDISPG : Function that determines the member global displacement


vector V
%from the joint displacement vector P
%MDISPL : Function that calculates the member local displacement
vector U=T*V
%MFORCEL : Function that evaluates the member local force vector
Q=BK*U
%it also creates the member axial force table to show in the results
%MFORCEG : Function that determines the member global force vector
F=T'*Q
%STORER : Function that stores the pertinent elements of the member
global
%force vector F in the reaction vector R

R=zeros(NR,1);
memendfortab=zeros(NM*2,5);

IM=1;

while IM<=NM

%Evaluation of member properties

91
JB=MPRP(IM,1);
JE=MPRP(IM,2);
I=MPRP(IM,3);
E=EM(I,1);
G=EM(I,2);
I=MPRP(IM,4);
A=CP(I,1);
IZ=CP(I,2);
IY=CP(I,3);
J=CP(I,4);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
ZB=COORD(JB,3);
ZE=COORD(JE,3);
BL=((XE-XB)^2+(YE-YB)^2+(ZE-ZB)^2)^.5;
rxX=(XE-XB)/BL;
rxY=(YE-YB)/BL;
rxZ=(ZE-ZB)/BL;
PSI=MPRP(IM,5);

V=zeros(2*NCJT,1);
T=zeros(2*NCJT);
U=zeros(2*NCJT,1);
BK=zeros(2*NCJT);
Q=zeros(2*NCJT,1);
F=zeros(2*NCJT,1);

%Call of MDISPG

V=MDISPG(JB,JE,NCJT,NDOF,NSC,d,V);

%Call of MTRANS

T=MTRANS(rxX,rxY,rxZ,PSI,XB,XE,ZB,ZE,T);

%Call of MDISPL

U=MDISPL(V,T,U);

%Call of MSTIFFL

BK=MSTIFFL(E,G,A,IZ,IY,J,BL,BK);

%Member loads calculation and storage

QF=zeros(2*NCJT,1);

if NML>0
IML=1;

while IML<=NML

if IM==MP(IML,1)

%Call of MFEFLL

92
QF=MFEFLL(IML,BL,MP,PM,QF);
IML=IML+1;
else IML=IML+1;
end

end

%Call of MFORCEL

[Q memendfortab]=MFORCEL(JB,JE,BK,U,QF,IM,memendfortab);

else %Call of MFORCEL

[Q memendfortab]=MFORCEL(JB,JE,BK,U,QF,IM,memendfortab);

end

%Call of MFORCEG

F=MFORCEG(T,Q);

%Call of STORER

R=STORER(JB,JE,NCJT,NDOF,NSC,F,R);

IM=IM+1;
end

%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% Results Module %%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%

%Tables

%General structural data

COORD_table=[(1:NJ)' COORD]';
MSUP_table=MSUP';
EM_table=[(1:NMP)' EM]';
CP_table=[(1:NCP)' CP]';
MPRP_table=[(1:NM)' MPRP]';
JPPJ_table=[JP PJ]';
MEMLOAD_table=[MP PM]';

%Call of JOIDISTAB

joidistab=JOIDISTAB(NJ,NCJT,NSC,NDOF,d);

%Call of SUPREATAB

supreatab=SUPREATAB(MSUP,R,NS,NCJT);

fprintf('\t General Structural Data \n\n');


fprintf('Structure type : Space Frame \n');
fprintf('Number of joints : %g \n', NJ);
fprintf('Number of members : %g \n', NM);

93
fprintf('Number of material property sets (E G) : %g \n', NMP);
fprintf('Number of cross-sectional property sets (A IZ IY J) : %g \n',
NCP);
fprintf('\n*User must be conscious of the use of consistent units
\n\n');
fprintf('\n\t\t\t\t Joint Coordinates \n\n');
fprintf('Joint No \tX Coordinate \tY Coordinate \tZ Coordinate \n');
fprintf('\t%g \t\t\t%g \t\t\t%g \t\t\t%g \n',COORD_table);
fprintf('\n\t\t\t\t\t\t\t\t\t\t\t Supports \n\n');
fprintf('\t\t\t\t\t\t\t\t\t*1 represents a constraint\n\n');
fprintf('Joint No \tX Restraint \tY Restraint \tZ Restraint \tXX
Restraint \tYY Restraint \tZZ Restraint \n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t\t\t%g \t\t\t\t%g
\t\t\t\t%g \n',MSUP_table);
fprintf('\n\t\t\t\t Material Properties \n\n');
fprintf('Material No \tModulus of Elasticity (E) \tShear Modulus (G)
\n');
fprintf('\t%g \t\t\t\t\t%g \t\t\t\t\t%g \n',EM_table);
fprintf('\n\t\t\t\t\t\t Cross-Sectional Properties \n\n');
fprintf('Property No \tArea (A) \tMoment of Inertia (IZ) \tMoment of
Inertia (IY) \tTorsional constant (J) \n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t\t\t%g
\n',CP_table);
fprintf('\n\t\t\t\t\t\t\t\t\t Member Data \n\n');
fprintf('Member No \tBeginning Joint \tEnd Joint \tMaterial No
\tCross-Sectional Property No \tAngle of Roll PSI \n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g \t\t\t\t\t%g
\t\t\t\t\t\t\t%g \n',MPRP_table);
fprintf('\n\t\t\t\t\t\t\t\t\t\t Joint Loads \n\n');
fprintf('Joint No \t\tX Force \t\tY Force \t\tZ Force \t\tXX Moment
\t\tYY Moment \t\tZZ Moment\n');
fprintf('\t%g \t\t\t\t%g \t\t\t\t%g \t\t\t%g \t\t\t%g \t\t\t%g
\t\t\t%g\n',JPPJ_table);
fprintf('\n\t\t\t\t\t\t\t\t\t\t Member Loads \n\n');
fprintf('Member No \tLoad Type \tMagnitude (W, M, w, w1 or MT)
\tMagnitud (w2) \tDistance l1 \tDistance l2 \tLoc. Coo. Dir. (x=0,
y=1, z=2) \n');
fprintf('\t%g \t\t\t%g \t\t\t\t%g \t\t\t\t\t\t%g \t\t\t\t%g \t\t\t\t%g
\t\t\t\t\t%g \n',MEMLOAD_table);
fprintf('\n\t\t\t\t\t\t\t\t\t End of Input Data \n\n');
fprintf('\n\n\t\t\t\t\t\t\t\t\t Results of analysis \n');
fprintf('\n\t\t\t\t\t\t\t\t\t Joint Displacements \n\n');
fprintf('Joint No \t\tX Translation \t\t\tY Translation \t\t\tZ
Translation \t\t\tX Rotation (Rad) \t\t\tY Rotation (Rad) \t\t\tZ
Rotation (Rad)\n');
fprintf('\t%g \t\t\t%g \t\t\t%g \t\t\t%g \t\t\t%e \t\t\t\t%e
\t\t\t\t%e \n',joidistab');
fprintf('\n\t\t\t\t\t\t\t Member End Forces in Local
Coordinates\n\n');
fprintf('Member \t\tJoint \t\tAxial Force \t\tShear Force Y \t\tShear
Force Z \t\tTorsion \t\tMoment Y \t\tMoment Z \n');
fprintf('\t%g \t\t%g \t\t\t%g \t\t\t%g \t\t\t%g \t\t\t%g \t\t%g \t\t%g
\n',memendfortab');
fprintf('\n\t\t\t\t\t\t\t\t\t\t Support reactions \n\n');
fprintf('Joint No \t\tX Force \t\tY Force \t\tZ Force \t\tX Moment
\t\tY Moment \t\tZ Moment \n');
fprintf('\t%g \t\t\t%g \t\t%g \t\t%g \t\t%g \t\t%g \t\t%g
\n',supreatab');
fprintf('\n\t\t\t\t\t\t\t\t\t\t End of Analysis \n\n');

%Figures

94
IM=1;
while IM<=NM

%Undeformed geometry

JB=MPRP(IM,1);
JE=MPRP(IM,2);
XB=COORD(JB,1);
XE=COORD(JE,1);
YB=COORD(JB,2);
YE=COORD(JE,2);
ZB=COORD(JB,3);
ZE=COORD(JE,3);
BL=((XE-XB)^2+(YE-YB)^2+(ZE-ZB)^2)^.5;

x1=[XB;XE];
y1=[YB;YE];
z1=[ZB;ZE];

figure(1)
hold on
plot3(x1,y1,z1,'-o')

IM=IM+1;
end

95

S-ar putea să vă placă și