Вы находитесь на странице: 1из 14

Ottawa Carleton Institute of Civil Engineering

Theory of Structure II

- What is Matlab?
MATLAB (matrix laboratory) is a numerical computing environment developed by MathWorks.
MATLAB allows matrix manipulations (plus, minus, multiply, inverse), plotting of functions and data,
implementation of algorithms, creation of user interfaces.
(Source: http://en.wikipedia.org/wiki/MATLAB).

- Why we use Matlab?
In this course, we will compute nodal internal forces and displacements in complex structures such as
plane trusses, beams, and grids. The statistically indeterminate structures may contain tens of members
and it is really hard to be computed by hand.
On the other hand, by constructing matrices from structural data and algorithms (programming), one can
use Matlap to do all calculations and give perfect results. Matlab is powerful and it is applied very much
in structural design.

- Guide materials for how to use Matlab?
You can get the materials from Profs lectures, Matlab Help window and Internet.

- What you can do to master the programming language in structure computation?
You should have good basis knowledge of Theory of Structures I and II.
You understand simple algorithms performed in Matlab, and know which syntax you will use to each sub-
problem you meet.
You read carefully guides of coding expressed in the text book.
Try to practise Matlab as much as possible.

Ottawa Carleton Institute of Civil Engineering
Theory of Structure II

1. Open Matlab:
When Matlab is opened, you see three main sub-windows as default. The right sub-window named
Command Window is where calculation results are displayed. We can also perform direct, simple and
very short calculations in this window.
The left-upper sub-window named Current Directory. This window displays the folder that contains
M-files you are doing. Note that to be able to run an M-file on the right Command Window, the folder
that contains it should be opened in the Current Directory window. If not, Matlab will not understand
where the command comes from.
The left-lower window named Command History expresses the commands you did in Command
Window. This is not important during our jobs.

2. Practicing on the Command Window:
Check to see that you know where the window is. In this window, you see the symbol >> and a blink
cursor | (>> |). This is where you type your command.
A command is a calculation requirement.
Examples:
>> a=130+150+150
After typing this command, you press Enter and see the result displayed below:
a =
430
>> |
As you see above, the result a=430 is displayed. After completing the command (a calculation), a new
command line (>> |) is automatically appeared so that you can insert your new calculation.
Some basic symbols often used:
Symbol Meaning
+
Plus
-
Subtract
*
Multiple
/
Divide
sqrt(a)
Square root of a
a^b
Example 3^2
a exponent b
a+bi
Example: 3-5i
A complex number

Now, you try doing some following calculations: Note that you can only perform one calculation in each
command line:
>>a=90-30+30
>>a=2424/232+1458*535-335^3
>>b=sqrt(4+7i)
>>c=538^(-1/3)
Matrix calculations:
Matrices
1 2.5
3 2
a
(
=
(


,
1.1 2 3.5 1.2
3 4 5 6 ^ 2
b
(
=
(

and
1/ 5 2 / 3
4 3*5
c
(
=
(

are inputted into Matlab by typing
syntaxes:
>> a=[1 2.5; 3 -2]
>> b=[1.1 2 3.5 -1.2; 3 4 5 6^2]
>> c=[1/5 -2/3; 4 3*5]
Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
Then, you perform some following requirements:
Requirement Meaning
>> a+c
Matrix a plus matrix c
>> a-c
a subtract c
>> a*b
a multiple b
>> inv(a)
Inverse matrix of matrix a
>> d=c/a
>> d=inv(a)*c
c divide a
Inverse matrix of a multiples with matrix c
>> b'
Transpose matrix of matrix b
>> (a*b)*(b*c)

>> (c*b)-(b*a)

3. Practicing on M-file
In programming, you often need to write many command lines (for example, 100 command lines) to
perform one task. If you manipulate on the Command Window, you will be distracted because after
entering each command line, result of that is displayed. Moreover, when you want to model a very
complicated problem, you need to perform some sub-calculations; these works take a lot of time. In these
cases, you should use M-files. You can save the M-file you are doing even that your work has completed
or not yet and continue doing it later.
To open an M-file, you select menu File, point the cursor to sub-menu New and then select
M-file. An M-file looks like this:




Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
Now, you type all command lines you did in the item 2 above in to this M-file. Each line in this M-file is
equivalent to one command.


Save the M-file by a name Example1. Select the folder that the M-file is saved to. Then, to perform
calculations in this M-file, you need to type the name of this M-file into the command line of the
Command window:
>> Example1
See the result!
Note: 1. To run an M-file, you have to open the folder that contains this M-file in the Current Directory
window of Matlab. If not, Matlab will not understand where the command comes from.
2. If you type a semi-colon right behind a command line, the calculation is still performed but not
be displayed on the Command Window when you call the M-file. Try putting semi-colons to all
command lines of this M-file, then save it and call it again in the Command window, what can
you see?

4. Creating a function in an M-file
Syntax of a function in an M-file looks like this:
function [y1,...,yN] = myfun(x1,...,xM)
.
.
end
function [y1,...,yN] = myfun(x1,...,xM) declares a function that accepts inputs
x1,...,xM and returns outputs y1,...,yN. This declaration statement must be the first
executable line of the function.
Save the function code in a M-file with a .m extension. The name of the file should match the
name of the first function in the file. Valid function names begin with an alphabetic character,
and can contain letters, numbers, or underscores.
Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
Example: We need to find two roots x
1
and x
2
of a second order equation
2
3 2 0 x x + = . So, we
create a M-file named Second_order_equation, type following command lines and then save
the M-file.

In the Command Window, you type [x1,x2]=Second_order_equation(a,b,c) to perform calculations of
this M-file:
>> [x1,x2]=Second_order_equation(a,b,c)
Do this and see the results!

- Now, we will learn how to use Matlab to help you solve problems in Theory of Structure II. There
will be 8 tutorials that you have to do during this course. Plan and questions in each tutorial are given
in Professor Beatrizs lectures.










Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
Tutorial 1: Understand how to input matrices from excel and text files, how to add, subtract,
multiply and divide matrices. Then, learn how to do a loop. Finally, learn how to display matrix
including its row numbers and column numbers.

Task 1: Input a matrix from a .xlsx (.xls) or .txt file into Matlab by using a function.
There are some matrices (A,B) presented in an .xlsx file, C presented in a .txt file. We now want to use
these matrices in Matlab calculations. (See the Fig.1)

Fig 1: Input data

Our problem is often long and includes many different comment lines, instead of manipulating on
Command window; M-files will be created to input all necessary matrices.
For example, we now want to input three matrices A, B and C at the same time. An M-file named
Input_matrix is created in which we type the following function:
function [A,B,C] = Input_matrix()
A = xlsread('Matrices.xlsx', 1, 'B5:E8')
B = xlsread('Matrices.xlsx', 1, 'I5:L8')
C=load ('matrixA.txt');
end
Then Save it. On the Command window, you type [A,B] = Input_matrix() at the command line.
You will see the matrices displayed on this window.
Notes: 1. You can open the sub-menu Help and select Product Help to know more about the syntax
xlsread as well as learn how to make a function in Matlab.
2. If you type a semi-colon right behind the 2nd and 3st command lines, the matrices A and B will
not be displayed on the Comment Window when you call them.


Task 2: Input a matrix from an excel or text file into Matlab without using a function:
We can input matrices in to an M-file without using a function. Example syntax is
For an .xlsx file: A = xlsread('Matrices.xlsx', 1, 'B5:E8')
For a .txt file: load matrixC.txt;
C=matrixC
For examples, we now create an M-file named input_matrices_1. Then we type the command line as
above. Save it and type a command: input_matrices_1 on the Command Window to see the result.

Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
Task 3: Create a matrix by using command zeros
Syntax: B = zeros(m,n) to create a matrix (m x n) in which m is number of rows while n is number of
columns. All elements in this matrix are zero. Then, depended on problem requirements, non-zero values
at some elements can be inputted to substitute for zero numbers.

For example:
Create an M-file named MatrixE in which we type:
E=zeros(3,3);
E(1,1)=2;
E(2,2)=10;
E(3,3)=15;
disp('Matrix E is:')
E
Then, type MatrixE at the Command line of Command Window to see the result.

Task 4.1: Add matrices
We now want to add matrix A with matrix B (Note: they should have the same size)
Create a function named Add_matrix in which we use example syntax as:
function Y = Add_matrix(A,B)
% function to add two matrices
Y=A+B;
end
Note: Because we havent inputted matrices A and B in this M-file yet, we cant run it alone. We will
create a final M-file that link all M-files of data input, calculations and result display. Where, the function
Add_matrices does one task (add matrices) and it will be called in the final M-file later. (We should
ensure that each M-file does one task).

Task 4.2: Subtract matrices
Create a function named Sub_matrix in which we use example syntax as:
function Y = Sub_matrix(A,B)
% function to add two matrices
Y=A-B;
end

Task 4.3: Multiply matrices
Create a function named Mult_matrix in which we use example syntax as:
function Y = Mult_matrix(A,B)
% function to add two matrices
Y=A*B;
end

Task 4.4: Solve a matrix equation
We want to find Y in a equation system B=A*Y, to do so we have to find inverse matrix of B to form
Y=A
-1
B. In Matlab, B/A or inv(A)*B can be understood as A
-1
B.
Create a function named Solve_matrix. Then use example syntax as:
function Y = Solve_matrix(A,B)
% function to add two matrices
Y=inv(A)*B;
End

Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
Task 5: Link (Connect) M-files to solve a problem.
PROBLEM1: Given: 4 matrices A, B, C, D.
Required:
Create an M-file,
input all four matrices,
display them on Command window,
Compute AB, BA, CA, AplusC, CminusD, Y=inv(A)*B, display the results.
Guide:
Create an M-file named your_calculations
Example syntax:
% Solve first problem
disp('PROBLEM 1')
%Input and display matrices
[A,B,C] = Input_matrix() % Call matrices A, B, C
load matrixD.txt
D = matrixD % Call matrix D
% Calculations
AB = Mult_matrix(A,B)
BA = Mult_matrix(B,A)
CA = Mult_matrix(C,A)
AplusC = Add_matrix(A,C)
CminusA = Sub_matrix(C,D)
Y= Solve_matrix(A,B)

Task 6: For loop
The loop allows us to repeat certain commands. If you want to repeat some action in a predetermined
way, you can use the for loop. All of the loop structures in matlab are started with a keyword such as
"for", or "while" and they all end with the word "end". Another deep thought, eh.
The for loop is written around some set of statements, and you must tell Matlab where to start and where
to end. Basically, you give a vector in the "for" statement, and Matlab will loop through for each value in
the vector.
On the Command Window, we do some following examples.
Example1: for i=1:10
i
end
Example2: for i=1:2:10
i
end
Example3: Create a row vector
for i=1:10
v(i)=i;
end
v
Example4: Create a matrix
for i=1:5
for j=1:5
K(i,j)=i+j;
end
end
K
Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
We can do all loop examples above in M-files.
Task7: if statements
There are times when you want certain parts of your program to be executed only in limited
circumstances. The way to do that is to put the code within an "if" statement. The most basic structure for
an "if" statement is the following:
if expression
statements
end
or when you have some expressions for a set:
if expression 1
statements 1
elseif expression 2
statements 2
...
else
statements n
end
Example: Count number of elements less than 15, greater 20 and number between 15 and 20 in matrix
A?
Example syntax:
[A,B,C] = Input_matrix(); % Call matrices A,B,C
n=0;
m=0;
k=0;
for i=1:4
for j=1:4
if A(i,j)<15
n=n+1;
elseif A(i,j)>20
m=m+1;
else
k=k+1;
end
end
end
disp('Number of elements smaller than 15:')
n
disp('Number of elements greater than 20:')
m
disp('Number of elements between 15 and 20:')
k


Task 8: Output results from Matlab to an excel file
We now want to output matrices A and B from Matlab to an excel file:
Example syntax:
Step 1: Create an input file (M-file) named Input_matrix (as we did in task 1).
Step 2: Create an output file (M-file) named Output_Matrix(A,B)as follows:
function [] = Output_Matrix(A,B)
a={'Matrix A:'};
xlswrite('outputAandB.xls', a, 1, 'A1');
xlswrite('outputAandB.xls', A, 1, 'B2:E5');
b={'Matrix B:'};
xlswrite('outputAandB.xls', b, 1, 'A7');
xlswrite('outputAandB.xls', B, 1, 'A8:D10');
end

Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
Step 3: a link M-file named Tutorial1.m is created in which we can use example syntax as:
% Input matrices:
disp('Matrices A and B:')
[A,B] = Input_matrix();
% Output matrices A and B:
Output_Matrix(A,B);
After creating three M-files, you need to put them in the same folder. To run (or to call) them, you need to
type the name of link M-file >>Tutorial1 in the Command window.

Task 9: Check to see if matrix A is orthogonal or not?
Example syntax:
A=xlsread('Matrices.xlsx', 1, 'B5:E8');
d=A*A';
icount=0; % icount is used to check the orthogonal property of the matrix. we
can use another symbol instead of using the name icount.
for i=1:4
for j=(i+1):4
if d(i,i)~=1 || d(i,j)~=0 || d(j,i)~=0 % Symbol "~=" means "not equal"
; and symbol "||" means "or"
icount=1; % icount is given another value (1) when the statement "if"
detects any element in matrix d is different to that in the
same size-identity matrix
end
end
end
if icount==1 % Call the value of icount to state our conclusion.
disp('Matrix a is not orthogonal');
else disp ('Matrix a is orthogonal');
end



Tutorial 3: Data input and output for a plane stress

The first step in a structural analysis by using software is to input data. The input data should be
reasonably arranged so that later calculations are performed correctly. This means that we need
to follow some rules in data inputting.
Some symbols of variables are listed on professors tutorial 3 such as NJ, COORD, NS, so on.
You should understand what they mean.
The first sub-steps should be done before using software to input data:
- Draw a line diagram of the structure, on which each joint and member is identified by a
number. For joint numbering, we should do from left to right and from lowest to highest.
(Done in your tutorial 3 material).
- Establish a XY global coordinate system.
- Determine number of displacements each joint may have.
- Identify the degrees of freedom (or joint displacements) and restrained coordinates of the
structures.
- Numbering the DOFs and restrained coordinates.
(Please see your class notes and notebook to surely understand the sub-steps).
Then, check your lecture notes to know how to establish input data matrices. An excel file named
Tutorial3 is created to list all the input data matrices. An example of input data of the
structure shown in page 130 of notebook looks like this:


Ottawa Carleton Institute of Civil Engineering
Theory of Structure II




M-files of input and output data are created. Those are named as Input_Joints, Input_Supports,
Input_Materials, Input_Members, Input_Supports, Input_Xsections and 6 their corresponding
output M-files. Each M-file does one task. After that, one main m-file named Tutorial3 should
be created to link all input and output files. All the M-files are stored in the same folder with the
excel file. Example:


In each M-file, we create a function to do its only task.
Example1: Syntax for one input M-file named Input_Joints:
function [NJ, COORD] = Input_Joints()
NJ = xlsread('Tutorial3.xlsx', 1, 'C3');
COORD = xlsread('Tutorial3.xlsx', 1, ['C6:D11']);
end
Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
The other input M-files are created similarly.

Example2: Syntax for one output M-file named Output_Joints:
Output from Matlab to an excel file:
function [] = Output_Joints(NJ,COORD)
a={'Number of joint:'};
xlswrite('output.xls', a, 1, 'A1');
xlswrite('output.xls', NJ, 1, 'B1');
b={'Joint coordinate: X-COORD Y-COORD'};
xlswrite('output.xls', b, 1, 'A3');
xlswrite('output.xls', COORD, 1, 'B4:C9');
end
Notes: 1. all data should be outputted to one excel file (here the file is named as'output.xls').
For each group of output matrices, we can change the sheet number (to 2, 3).
2. Look at pages 138 and 139 of notebook to see the output form (necessary text lines)
of data.
3. You cant run each m-file alone. This means that you have to create a final m-file (link
m-file) to connect the m-files to work together. For example:
If you want to output all matrices to a text file, an M-file name Tutorial 3 is created to link
input and output m-files. One example syntax looks like this:
Example 3:


[NJ, COORD] = Input_Joints() % Call matrices NJ, COORD
.
.
.
Output_Joints(NJ, COORD); %Output number of joints and joint coordinates
.
.
.


Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
Tutorial 4: After inputting data from an excel file into Matlab, we now can start writing M-files (coding)
to calculate internal forces and displacements of a plane truss. But this calculation is also very
complicated and we need to divide it in to sub-steps. Tutorial 4 is the first step of the calculations, in
which we need to create M-files to compute member stiffness matrix in the local coordinate system,
member transformation matrix, member stiffness matrix in the global coordinate system, and to make an
output of the calculations. Detailed requirement is presented in Professor Beatrizs tutorial 4.
Before coding, students surely need to understand what the matrices are? How to form them? (Please
review your professors class notes and notebook (page 105, item 3.8)).
Guide: Your final M-file is structured like this (continued from tutorial 3).
NCJT=2; % Number of displacements a joint may have. For truss, NCJT=2
% Call matrices, the matrices are inputted from Tutorial 3
[NJ, COORD] = Input_Joints(); % Call matrices NJ, COORD
.
.
.
% Start calculations:
for i=1:NM % i is index of i
th
member
JB = MPRP(i,1); JE = MPRP(i,2); % Code number of beginning joint and
that of end joint of i
th
member
j = MPRP(i,3); E = EM(j,1); % Material of i
th
member, referred from
third column of matrix MPRP to j
th
row
of matrix EM
n = MPRP(i,4); Area = CP(n,1); % Cross-section area of i
th
member,
referred from 4
th
column of matrix
MPRP to n
th
row of matrix CP
Code to compute member stiffness matrix in local coordinate system
Code to compute coordinate transformation matrix
Code to compute member stiffness matrix in global coordinate system
Call the function to output [k
g
] %Note: The function is created
outside this M-file. Similar to example 5
in task 6.
end

For each task, such as Code to compute member stiffness matrix in local coordinate
system, we can create a private M-file (a function) to compute for one general member, then call the
function there inside the loop. One other method is to code directly.
An example syntax to create a calculation function for general member
function [k] = Compute_local_matrix(A,B,C,D...)
k=zeros(A,B);
X=C*D...;
k(1,1)=X;
.
.
end
Where A, B, C, D are known as parameters (such as NCJT, NS, NM) inputted before. For each
function, we should determine how many variables (A, B, C, D, ) are needed. You can find the
variables needed for each function in professors class notes or coding guides in notebook.


Ottawa Carleton Institute of Civil Engineering
Theory of Structure II
Tutorial 5: All requirements and guides are presented in Professor Beatrizs documents. Please
review them before writing your codes.
Guide: In flowchart 2, the step ? I NS s is substituted by a for loop.