Documente Academic
Documente Profesional
Documente Cultură
Introduction: MATLAB is a powerful computing system for handling the calculations involved in
scientific and engineering problems. The name MATLAB stands for MATrixLABoratory, because the
system was designed to make matrix computations.
One of the many things about MATLAB is that it can be use interactively. This means that, if we type
some commands at the special MATLAB prompt, we get the answer immediately. The problems
solved in this way can be very simple. As example finding the square root or finding the solution to
a system of differential equations is very simple in MATLAB. For many technical problems, the
solution (answer) can be obtained by just entering one or two commands.
There are two essential requirements for successful MATLAB programming:
Programmer need to learn the exact rules for writing MATLAB statements.
Programmer need to develop a logical plan of attack for solving particular problems.
Using MATLAB: To start MATLAB from windows, double click the MATLAB icon on your desktop or
laptop windows and wait for few minutes to open MATLAB window. When MATLAB starts, MATLAB
desktop opens as shown in Fig. 1. The window in the desktop that concerns us is the Command
Window, where the special >> prompt appears. This prompt means that MATLAB is waiting for a
command. MATLAB can be quit at any time with one of the following:
Go to File menu, select Exit MATLABand press Enter.
Typequit or exit at the Command Window prompt>> and press Enter.
Note: Do not click on the close box in the top right corner of the MATLAB desktop. This does not
allow MATLAB to terminate properly and in many cases it may cause problems with computer
operating software.
>> A = [2 1 4; 3 2 5; 1 3 2];
>> B=[3 4 1; 2 3 0; 4 2 3];
>> C=A+B
(d) We can multiply Matrices, C = A*B
2 4 2
A ;B
3 5 1
>> A = [2 4; 3 5]; B = [2; 1]; C = A*B
C=
8
11
Some useful functions for matrices
eye Identity matrix
zeros A mtrix of zeros
ones A Matrix of ones
diag Creates or extract diagonals
rand Random matrix
Try the following:
>>eye(3), zeros(3), ones(2), diag(eye(3))
Subscripting the Elements of a Matrix: We can refer or write the required element(s) of a matrix by
means of subscripts (i, j), where i is row number and j is Colum number.
2 4 5 7
3 6 8 9
(a) If A , write the values of all elements of 3rd Colum and the values of all
1 3 10 6
0 2 7 3
elements of 2nd row.
>> A = [2 4 5 7; 3 6 8 9; 1 3 10 6; 0 2 7 3];
>> B = A(:, 3) <Enter>
B=
5
8
10
7
>> C = A(2, :) <Enter>
C=
3 6 8 9
Note that colon inside the brocket indicates all.
The command A(:, 2:4) write the all elements of 2nd, 3rd and 4th colum of A Matrix.
(b) Try E1 = A([1 2], [1 2]), E2 = A([2 3], [2 3]) and E3 = A([3 4], [3 4])
Loops: for and while: Many programs require iteration or repetitive execution of a block of
statements. This can be done in MATLAB using for statement.
For example:
(a) If f1=1 and f2=2, then fi = fi-1 + fi-2 for i=3 to 30. The MATLAB code for this problem can be
written using for construction.
>> f=[1 2];
>>fori = 3:10;
f(i) = f(i-1)+f(i-2);
>>end
>> f <Enterr>
F = 1 2 3 5 8 13 21 34 55 89
(b) A program to generate n!can be written using for construction as
>> % Program to find 5!
>> n=5;
>>fact=1;
>>for k = 1:n
>> fact=fact*k;
>> disp([fact])
>>end
Note: If we write fact within [] in disp ( ) statement, it gives arrays of fact. If we write fact without [
], it gives final answer of fact (n!).
while statement: Sometimes, it is necessary to repeat statements based on a condition rather than
a fixed number of times. This is done with while statement.
For example,
(a) Suppose we have invested Rs. 1000 which draws 10% interest per year, compounded.
Calculate how long it takes for the investment to be double. MATLAB code for this
calculation can be written using while statement as follows:
>> % To calculate Interest on Bank balance
>> iniamount = 1000;
>> intrate = 0.1;
>> % Initialization of amount and year
>> bal = iniamount;
>> year = 0;
>> % Getting print out of year and amount in tabular form
>> disp(Ýear Balance’)
>> To calculate the balance until it becomes double (i.e. 2 times initial amount)
>> while bal<2*iniamount; bal = bal + intrate*bal;
>> year = year+1;
>> disp([year bal])
>> end
(b) If x= 10, x=x/2 do the calculation until x value is greater than 1.
>> % Initial value of x
>> x=10;
>> while x>1; x=x/2;
>> end
Conditionals or Decisions, if and else: Often a function needs to branch based on runtime
conditions. The computations which are to be done based on certain condition can be done using if
and else statements. For example try the following.
>> x =2;
>> If x <0 disp(‘negative’), else disp(‘positive’), end
1
2
500 N
3 A1 900mm2 ; A2 600mm2
1 2
E1 200GPa; E2 70GPa
600 mm 500 mm
Fig. 1
MATLAB Code:
Let
elenodes = Global nodal connectivity of elements
ne = Number of elements used in the discritized domain
nn = Total number of nodes
disp = Nodal displacement vector
force = Nodal force vector
stiffness = Stiffness Matrix
eledof = dof of element
Using the above notations, the MATLAB code can be written as follows:
>>%MATLAB Code for FEA of Bar
>>clear all
>>%Elements geometric and material data
>>A(1)=900; A(2)=600;
>>L(1)=600; L(2)=500;
>>E(1)=200.0E+3; E(2)=70.0E+3;
>>% ne is the Number of Elements
>>ne= 2;
>>%nn is the number of nodes
>>nn=ne+1;
>>%gdof is total number of global dof. Since each node has 1 dof,
>>gdof= 1*nn;
>>%Element Nodal Connectivity
>>elenodes=[1 2; 2 3];
>>%Initialization of matrix and vectors
>>%Displacement vector
>>disps=zeros(gdof,1);
>>%Force vector
>>force=zeros(gdof,1);
>>%Stiffness matrix
>>stiffness=zeros(gdof,gdof);
>>%Applied load at node 3
>>force(3)= 500;
>>%Computation of system stiff matrix
>>for e=1:ne;
>>%elementDof is the element dof
>>eledof=elenodes(e,:);
>> ke = (A(e)*E(e)/L(e))*[1 -1; -1 1];
>>stiffness(eledof,eledof)=...
stiffness(eledof,eledof)+ke;
>>end
>>%Boundary conditions
>>%predof is the prescribed boundary conditions.
>>% Since displacement at node 1 is prescribed
>>predof=[1];
>>% activedof is the unknown displacement to be determined
>>activedof=setdiff([1:gdof]',[predof]);
>>%solution
>>disps= stiffness(activedof,activedof)\ force(activedof);
>>% Positioning all displacements
>>disps1=zeros(gdof,1);
>>disps1(activedof)=disps;
>> % Output displacements/Reactions
>>disp('Displacements')
>>jj=1:gdof;
>> [jj', disps1]
>> %Reactions
>> R=stiffness*disps1;
>> reactions=R(predof);
>> disp('Reactions')
>> [predof, reactions]
Run the program to get the following results and compare these
results with the ANSYS results.
Displacements
ans =
1.0000 0
2.0000 0.0017
3.0000 0.0076
Reactions
ans =
1.0000 -500.0000
Comparison of Results with ANSYS Results:
Node MATLAB ANSYS
Displacements
(mm) 1 0 0
2 0.0017
3 0.0076 0.007619
Exercise 2: Write MATLAB Code for FEA of a four bar truss shown in Fig. 2 and obtain unknown
displacements, support reaction and element stresses. Compare the results with the results of
ANSYS. Take E = 210 GPa and A = 0.1 m2.
2.5 kN
4 4
3
3
3m 2
1 1
2 kN
2
4m
Fig. 2
v4 v3 q8 q6
u4 4 q7 4
4 3 u3 4 3 q5
3 3
2 2
v1 q2
v2 q4
1 1
2
u2 2
q3
1 u1 1 q1
Fy3 F8
Fy3 F6
Fx 3 4 F7 4
4 3 Fx 3 4 3 F5
3 3
2 2
F y1 F2
Fy 2 F4
1 1
2
Fx 2 2
F3
1 Fx1 1 F1
Run the program to get the following results and compare these results with ANSYS results.
Displacements
ans =
1.0000 0 0
2.0000 0.0004 0
3.0000 0.0001 -0.0003
4.0000 0 0
Reactions
ans =
1.0e+03 *
0.0010 -1.5833
0.0020 0.3125
0.0040 2.1875
0.0070 -0.4167
0.0080 0
Element Stresses
ans =
0.0200
-0.0219
-0.0052
0.0042
Comparison of Results with ANSYS Results:
Node MATLAB ANSYS
Displacements Ux Uy Ux Uy
(mm) 0 0 0 0
1
2 0.0004 0 0.000381 0
3 0.0001 -0.0003 0.000079 0.00031
4 0 0 0 0
Support Fx Fy Fx Fy
Reactions (N) -1583.3 312.5 -1583.3 312.5
1
2 2187.5 2187.5
4 -416.7 0 -416.67 0
Element Stresses Max 0.02 0.02
(N/mm2) Min -0.0219 -0.021875
Exercise 3: Write MATLAB Code for FEA of a planar truss shown in Fig. 3 and obtain unknown
displacements, support reaction and internal stresses developed in each member. Compare the
results with the results of ANSYS.
Member Cross sectional area Young’s modulus
(A) in mm2 (E) in N/mm2
1 200
2 200
2x105
3 100
4 100 10 kN
4
3
4 1000 mm
500 mm 1 2
1 2
1000 mm 1000 mm
Fig. 3
q8 , F8
q7 , F7
4
q6 , F6 3 (2000, 1000)
q5 , F5
3 (500, 500)
4
2
1
q1 , F1 q4 , F4
q2 , F2 q3 , F3
1(0, 0) 2 (1000, 0)
Reactions
ans =
1.0e+04 *
0.0001 -1.0000
0.0002 -1.0000
0.0003 1.0000
0.0004 2.0000
Element Stresses
ans =
70.7107
-35.3553
158.1139
-212.1320
20 kN
0.3 m
2m
0.2 m
4m
Fig. 4
bh 3 0.2 * 0.33
Here, I 4.5 10 4 m 4 , E 210 109 N / m 2
12 12
w1 w2 q1 q3
1 2 3
w3 q2 q4 q6 q5
1 2 1 2
1 2 3 1 2 3 Global nodes
x=0 x = 2m x = 4m
Run the program to get the following results and compare with ANSYS results.
Deflection
ans =
1.0000 0
3.0000 -0.0003
5.0000 0
Slope
ans =
2.0000 -0.0002
4.0000 -0.0000
6.0000 0.0002
Reactions
ans =
1.0e+04 *
0.0001 1.0000
0.0005 1.0000
Exercise 5: Write MATLAB Code for FEA of a cantilever beam shown in Fig. 5 and obtain unknown
displacements, slopes and support reaction. Compare the results with the results of ANSYS.
Assume rectangular cross sectional area of 0.2 x 0.3 m2. Take E = 210 GPa. Use two elements.
10 kN
0.3 m
0.2 m
5m
Fig. 5
bh 3 0.2 * 0.33
Here, I 4.5 10 4 m 4 , E 210 109 N / m 2
12 12
w1 w2 q1 q3
1 2 3
w3 q2 q4 q6 q5
1 2 1 2
1 2 3 1 2 3 Global nodes
x=0 x = 2.5 x=5
Fig. 5(a) Nodal coordinates and Global nodal displacements
le
F1 F3
F2 F1
F2 F3 F5
M1 M2 2 M3 F4 2
F6
1 1
1 2 3 1 2 3
e l
This problem is same as previous problem, but Concentrated load is at node 3 (Fig. 5(b)), therefore
F(5) = - 10E3; predof is the boundary conditions, for cantilever the deflection and slope at support
i.e. at node 1 are zero. Therefore (see Fig. 5(a)), w1 q1 0 and 1 q2 0 and hence, predof =
[1; 2] i.e. (gdof 1 for q1 0 and 2 for q2 0 ).
MATLAB Code:
>>% MATLAB Code for FEA of Beam, Problem-1
>> clear all %Clear memory
>> %Elements geometric and material data
>> % E=Modulus of elesticity, I: M.I of beam (m^4),L= length of beam
>> e=210e9; i=4.5e-4;
>> % Nelements is the Number of Elements
>> ne = 2;
>> % Nnodes is number of nodes
>> nn = ne+1;
>> %Gdof is total number of global dof. Since each node has 2 dof,
>> gdof = 2*nn;
>> % Elements Nodal Connectivity
>> elenodes = [1 2; 2 3];
>> % Nodal coordinates
>> xx = [0; 2.5; 5];
>> % Concentrated load
>> f=zeros(Gdof,1); % Intialize F to zero
>> f(5) = -10e3;
>> % Didtributed load
>> po = zers(gdof, 1)
>> po(1) = 0; po(2) = 0;
>> % Generation of Stiffness matrix and force vector in subroutine
>> [stiffness, force] = stiff_beam(gdof, ne,elenodes,nn,...
xx,e,i,po);
>> % addition of concentrated load to global load vector of udl
>> for i = 1, gdof;
>> force(i)= force(i)+f(i);
>> end
>> % Boundary conditions:
>> % For simply support, q1=0, q5=0 i.e Gdof 1 and 5 are prescribed
>> predof = [1;2];
>> % Solution
>> activedof = setdiff([1:gdof]',[predof]);
>> disp= stiffness(activedof,activedof)\ force(activedof);
>> % Positioning all displacements
>> disp1=zeros(Gdof,1);
>> disp1(activeDof)=disp;
>> % Output deflection, slope/Reactions
>> disp('Deflection')
>> jj=1:2:Gdof;
>> [jj', disp1(1:2:gdof)]
>> % Slopes
>> disp('Slope')
>> kk = 2:2:Gdof;
>> [kk', disp1(2:2:gdof)]
>> %Reactions
>> R = stiffness*disp1;
>> reactions = R(predof);
>> disp('Reactions')
>> [predof, reactions]
>> % Drawing of deformed shape
>> u=zeros(gdof,1);
>> u = disp1(1:2:gdof);
>> plot(Nodecoords,u,'o')
Function file stiff_beam.m: This m file is to be saved in MATLAB folder. This function file will be same
as previous problem for all beam problems.
Exercise 6: Write MATLAB Code for FEA of a cantilever beam shown in Fig. 6 and obtain unknown
displacements, slopes and support reaction. Compare the results with the results of ANSYS.
Assume rectangular cross sectional area of 0.2 x 0.3 m2. Take E = 210 GPa.
12 kN/m
0.3 m
4m
0.2 m
6m
bh 3 0.2 * 0.33
Here, I 4.5 10 4 m 4 , E 210 109 N / m 2
12 12
w1 w2 q1 q3
1 2 3
w3 q2 q4 q6 q5
1 2 1 2
1 2 3 1 2 3 Global nodes
x=0 x = 4m x = 6m
F1 Fl2e F3 F2 F1
F3 F5
M1 M2 2 M3 F4 2
F6
1 1
1 2 3 1 2 3
Fig. 7
q2 q1 q4 q3 q5 q7 q9
q6 q8 q10
1 2 3 4
1 2 3 4 5 Global nodes
x=0 x = 1m x = 2m x = 3m x = 4m
Exercise 8: Write MATLAB Code for FEA of a cantilever beam shown in Fig. 8 and obtain unknown
displacements, slopes and support reaction. Compare the results with the results of ANSYS.
Assume rectangular cross sectional area of 0.2 x 0.3 m2. Take E = 210 GPa.
12 kN-m 6 kN 6 kN
0.3 m
2m 2m 2m 2m 0.2 m
Fig. 8
q2 q1 q4 q3 q5 q7 q9
q6 q8 q10
1 2 3 4
1 2 3 4 5 Global nodes
x=0 x = 2m x = 4m x = 6m x = 8m