Sunteți pe pagina 1din 20

MATLAB

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.

Fig. 1: MATLAB Window


Once you started MATLAB, try the following exercises in the command window. If necessary, make
the command window active by clicking anywhere inside its border.
(a) Type 8+9 after the >> prompt, followed by Enter, you get the answer 17.
>> 8 + 9 <Enter>
ans =
17
(b) Try the following, you get the answer as indicated:
(i) >> 20*40 <Enter>
ans =
800
(ii) >> 1/20 <Enter>
ans =
0.0500
(iii) >> 2^3 <Enter>
Ans =
8
 In the above examples, *, / and ^ are used for multiplication, division and exponentiation.
 MATLAB can also handle, 0/1 (i.e zero divided by some number) and 1/0. Try it
(i) >> 0/2 <Enter>
ans =
0
(ii) >> 2/0 <Enter>
Ans =
Inf
(iii) >> 0/0 <Enter>
Ans =
NaN
 NaN means Not-a-Number.
Assigning Values to Variables: We can assign values for variables for the arithmetical operations.
(a) Try the following:
(i) >> a = 3, b = 2 <Enter>
a=
3
b=
2
>> a = a*10 + b*20 <Enter>
a=
70
(ii) >>ra = 3., ba = 2., sum = ra + ba <Enter>
ra =
3
ba =
2
sum =
5
We can assign values for several variables and write several commands on one line by separating
them by commas or semicolons.
(b) Try the followings:
(i) >>a = 2; b = 3; c = a*b <Enter>
c=
6
Semicolon (;) in front of variable prevents the value of variables (a and b) from being displayed on
MATLAB window.
MATLAB has all of the usual mathematical functions that are available on scientific calculator such
as pi (  ), sin, cos, log (natural log). In trigonometric functions like sin(x), the argument x should be
in radian. If the angle is in degree, multiply it by  / 180 to get radians.
(a) Try to find a  
>> a = sqrt(pi) <Enter>
a=
1.7725
 If you assign any number for variable pi, the MATLAB will assign 4 for pi not 3.1416. Try the
following
>>pi =4; <Enter>
>>sqrt(pi) <Enter>
ans =
2
 Do not assign values for built in mathematical functions like pi, sin, cos, etc.
(b) Try to find a= sin  and b= cos  when  =30o.
>> theta= 30*pi/180;
>> a = sin(theta) <Enter>
ans =
0.5000
>> b = cos(theta)
ans =
0.8660
Assigning Arrays to a Variable: MATLAB can also assign arrays to the variables.
(a) The easiest way of defining a array where the elements increases by the same amount is with
the statement like,
>> a = 0 : 10 <Enter>
a=
0 1 2 3 4 5 6 7 8 9 10
(b) We can give the required increment to the array with the statement like,
>> a = 0 : 2 : 10 <Enter>
a=
0 2 4 6 8 10
(c) We can create array in descending order like,
>> a = 10: -2 : 0 <Enter>
a=
10 8 6 4 2 0
(d) Try a = 1: 2: 6
>> a = 1: 2: 6
a=
1 3 5
 Note that the last element is not allowed to exceed the value after the second colon i.e. 5.
(e) Try a = 0: -2: -5
(d) Now try a = 1: 0
>> a = 1: 0
1×0 empty double row vector (a complicated way of generating a array).
Drawing a Graph: We can draw a graph in MATLAB using plot command.
(a) For example, if z = sin (x), draw a graph of z vs x when x varies from 0 to 10 radians with
the increments of 0.1 radian. This can be done as follows,
>> x = 0 : 0.1 : 10; <Enter>
>> z = sin (x); <Enter>
>>plot(x,z) <Enter>
>>grid <Enter>
>>xlabel(‘x radian’) <Enter>
>>ylabel(‘z’) <Enter>
We will get the graph as shown in Fig. 2.
Fig. 2: Plot of z = sin (x)
Matrices in MATLAB: Matrices can be created in MATLAB.
 2
1 2 3  
(a) Matrices A    ; B  3  ; C  5 6 7  can be created as
 4 5 6   4

>> A = [1 2 3; 4 5 6]; B = [2; 3; 4]; C = [5 6 7];


 Rows are separated by semicolon and columns are separated by a space between each
number.
(b) We can also create transpose of matrices by simple writing ‘ to the variables.
>> A = [1 2 3; 4 5 6]’ <Enter>
A=
1 4
2 5
3 6
(c) We can add matrices, for example we can obtain C = A+B
 2 1 4 3 4 1
 
Where, A  3 2 5; B  2 3 0
1 3 2 4 2 3

>> 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

MATLAB for FEM


(a) MATLAB Code to Solve Axially Loaded Bars:
Exercise 1: Write MATLAB Code for FEA of an axially loaded bar shown in Fig. 1 and obtain
unknown displacements and support reaction. Compare the results with the results of ANSYS.

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

Support 1 -500 -500


Reactions (N)

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

Global nodal displacements

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

Global nodal forces


Number of nodes, Nnodes = 4; No. of elements, Nelements= 4; Global DOF, Gdof=2*Nnodes;
x- coordinates of nodes 1, 2, 3, 4 are, xx = [0; 4000, 4000, 0]. y- coordinates are, yy = [0; 0; 3000,
3000].
Concentrated loads are F(3) = + 2E3 and F(6) = - 2.5e3; predof is the boundary conditions, since
q1  0; q2  0; q4  0; q7  0; q8  0 , predof = [1; 2; 4; 7; 8] (Gdof 1 for q1  0 , 2 for q2  0 and so
on).
MATLAB Code:
>> % MATLAB Code for FEA of 2D Truss, Problem-1
>> clear all %Clear memory
>> %Elements geometric and material data
>> % e=Modulus of elesticity, a: Cross sectional area
>> E(1)=210E3; E(2)=210E3; E(3)=210E3; E(4)=210E3;
>> A(1)= 0.1E6; A(2)= 0.1E6; A(3)= 0.1E6; A(4)= 0.1E6;
>> % ne is the Number of Elements
>> ne = 4;
>> % nn is number of nodes
>> nn = 4;
>> %gdof is total number of global dof. Since each node has 2 dof,
>> gdof = 2*nn;
>> % Elements Nodal Connectivity
>> elenodes = [1 2; 2 3; 1 3; 3 4];
>> % Nodal coordinates
>> % Nodal x-coordinates
>> xx = [0; 4000; 4000; 0];
>> % Nodal y-coordinates
>> yy = [0; 0; 3000; 3000];
>> %Initialization of matrix and vectors
>> %Displacement vector
>> disp=zeros(gdof,1);
>> %Force vector
>> force=zeros(gdof,1);
>> % Stiffness matrix
>> stiffness = zeros(gdof, gdof);
>> %Applied load at node 2 (i.e at gdof 3) and 3 (at gdof 6)
>> force(3)= + 2E3; force(6)= - 2.5E3;
>> % Generation of system stiff matrix in subroutine stiff_truss
>> % Computation of stiffness matrix
>> for e = 1: ne;
>> indice = elenodes(e,:);
>> % eleDof is the element dof
>> eledof = [2*indice(1)-1 2*indice(1)...
2*indice(2)-1 2*indice(2)];
>> % computation of Element length
>> dx = xx(indice(2))-xx(indice(1));
>> dy = yy(indice(2))-yy(indice(1));
>> % l(e) is the length of element
>> L(e) = sqrt(dx*dx+dy*dy);
>> % Computation of direction cosines
>> c = dx/L(e); s = dy/L(e);
>> % Ke is the element stiffness matrix
>> ke = (A(e)*E(e)/L(e))*[c*c c*s -c*c -c*s; c*s s*s -c*s -s*s;...
-c*c -c*s c*c c*s; -c*s -s*s c*s s*s];
>> stiffness(eledof, eledof)=stiffness(eledof, eledof)+ke
>> end
>> %Boundary conditions
>> %Prescribed dofs
>> predof=[1; 2; 4; 7; 8];
>> % 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 deflection, slope/Reactions
>> disp('Displacements')
>> jj=1:nn;
>> [jj', disps1(1:2:gdof), disps1(2:2:gdof)]
>> % SUPPORT REACTIONS
>> R = stiffness*disps1;
>> reactions = R(predof);
>> disp('Reactions')
>> [predof, reactions]
>> % Computation of stress at elements
>> for e = 1: ne;
>> indice = elenodes(e,:);
>> % eleDof is the element dof
>> eledof = [2*indice(1)-1 2*indice(1)...
2*indice(2)-1 2*indice(2)];
>> % computation of Element length
>> dx = xx(indice(2))-xx(indice(1));
>> dy = yy(indice(2))-yy(indice(1));
>> % le is the length of element
>> L(e) = sqrt(dx*dx+dy*dy);
>> % Computation of direction cosines
>> c = dx/L(e); s = dy/L(e);
>> % sigma(e) is the element stress
>> sigma(e) = (E(e)/L(e))*[-c -s c s]*disps1(eledof);
>> end
>> disp('Element Stresses')
>> sigma'

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)

Fig. 3(a) Global nodal displacements/ forces

No. of elements, Nelements= 4; Number of nodes, Nnodes = 4; Global DOF, Gdof=2*Nnodes;


x- coordinates of nodes 1, 2, 3, 4 are, xx = [0; 1000, 500, 2000]. y- coordinates are, yy = [0; 0; 500,
1000]. Concentrated loads F(8) = + 10E3; predof is the boundary conditions, since
q1  0; q2  0; q3  0; q4  0 , predof = [1; 2; 3;4].
Modify the MATLAB code of the previous problem accordingly and obtain the following results:
Displacements
ans =
1.0000 0 0
2.0000 0 0
3.0000 0.2652 0.0884
4.0000 3.4790 -5.6003

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

Finally, compare these results with ANSYS results of same problem.


Exercise 4: Write MATLAB Code for FEA of a simply supported beam shown in Fig. 4 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.

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

Fig. 4(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

Fig. 4(b) Global nodal Forces/Moments


le
No. of elements, Nelements=2; Number of nodes, Nnodes = 3; Global DOF, Gdof=2*Nnodes;
x-coordinates of node 1, 2, 3 are, xx = [0; 2; 4]; udl, Po = 0, Concentrated load at node 2 (see Fig.
4(b)), F(3) = - 20E3; predof is the boundary conditions, for simply supported beam deflection at
supports i.e. at node 1 and at node 3 are zero. Therefore (see Fig. 4(a)), w1  q1  0 and
w5  q5  0 and hence, predof = [1; 5] (Gdof 1 for q1  0 and 5 for q5  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;
>> % ne is the Number of Elements
>> ne = 2;
>> % nn 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;4];
>> % f is the concentrated load
>> f=zeros(gdof,1);
>> f(3) = -20e3;
>> % po is the Didtributed load
>> po = zeros(gdof,1)
>> po(1) = 0; po(2) = 0;
>> % Computation of Stiffness matrix and force vector
>> %Initialization of stiffness matrix and force vector to zero
>> force = zeros(gdof,1);
>> stiffness = zeros(gdof,gdof);
>> %Calculation of stiffness matrix and force vector
>> for e =1:ne;
>> indice = elenodes(e,:);
>> % eleDof is the element dof
>> eledof = [2*indice(1)-1 2*indice(1)...
2*indice(2)-1 2*indice(2)];
>> % le is the length of element
>> Le = xx(indice(2))-xx(indice(1));
>> % Ke is the element stiffness matrix
>> ke = (I*E/Le^3)*[12 6*Le -12 6*Le; 6*Le 4*Le^2 -6*Le 2*Le^2;...
-12 -6*Le 12 -6*Le; 6*Le 2*Le^2 -6*Le 4*Le^2];
>> % te is the load vector due to udl
>> te = [po*Le/2; po*Le^2/12; po*Le/2; -po*Le^2/12];
>> % Global stiffness and load vector
>> stiffness(eledof, eledof)=stiffness(eledof, eledof)+ke;
>> force(eledof) = force(eledof)+te;
>> end
>> % 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;5];
>> % Solution
>> activedof = setdiff([1:gdof]',[predof]);
>> disps= stiffness(activedof,activedof)\ force(activedof);
>> % Positioning all displacements
>> disps1=zeros(gdof,1);
>> disps1(activedof)=disps;
>> % Output deflection, slope/Reactions
>> disp('Deflection')
>> jj=1:2:gdof;
>> [jj', disps1(1:2:gdof)]
>> % Slopes
>> disp('Slope')
>> kk = 2:2:gdof;
>> [kk', disps1(2:2:gdof)]
>> %Reactions
>> R = stiffness*disps1;
>> reactions = R(predof);
>> disp('Reactions')
>> [predof, reactions]
>> % Drawing of deformed shape
>> u=zeros(gdof,1);
>> u = disps1(1:2:gdof);
>> plot(Nodecoords,u,'o')

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

Comparison of Results with ANSYS Results:


Node MATLAB ANSYS
Max. deflection, 2 -0.0003
Dmax (m)

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

Fig. 5(b) Global nodal Forces/Moments

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

Fig. 6(a) Nodal coordinates and Global nodal displacements

F1 Fl2e F3 F2 F1
F3 F5
M1 M2 2 M3 F4 2
F6
1 1
1 2 3 1 2 3

Fig. 6(b) Global nodal Forces/Moments


In this problem, there is nol e concentrated load and hence need not specify F( ). But there is an udl
on element 1. Therefore, Po(1) = - 12E3 and Po(2) = 0. For simply support at the ends of beam (see
Exercise 4), predof = [1; 5] (gdof 1 for q1  0 and 5 for q5  0 ). X-coordinate of node 1, 2, 3 are, xx
= [0; 4; 6]. Remaining data are same as Ex. 4. So modify the program of Ex. 6 and run to get the
following results and compare these results with ANSYS results.
Exercise 7: Write MATLAB Code for FEA of a cantilever beam shown in Fig. 7 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.
100 N 200 N
o
300 N
60 45o 30o
0.3 m
1m 1m 1m 1m 0.2 m

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

Fig. 7(a) Nodal coordinates and Global nodal displacements


le
F1 F3 F5 F7
F2 F4 F6 F8 F10 F9
1 2 3 4
1 2 3 4 5 Global nodes
x=0 x = 1m x = 2m x = 3m x = 4m

Fig. 7(b) Global nodal forces/moments


le

No. of elements, Nelements=4; Number of nodes, Nnodes = 5; Global DOF, Gdof=2*Nnodes;


x-coordinates of node 1, 2, 3, 4, 5 are, xx = [0; 1; 2; 3; 4]; udl, Po = 0,
Concentrated load are at nodes 2, 3, 4 (see Figs. 7 and 7(b)) and they are,
F3  100 sin 60  86.6 N ; F5  200 sin 45  141.42 N ; F7  300 sin 30  150 N (see Fig. 7(b)).
Therefore, F(3) = - 86.6; F(5) = -141.42; F(7) = -150;
predof is the boundary conditions, for simply supported beam deflection at supports i.e. at node 1
and at node 5 are zero. Therefore (see Fig. 7(a)), q1  0 and q9  0 and hence, predof = [1; 9] (Gdof
1 for q1  0 and 9 for q9  0 ).

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

Fig. 8(a) Nodal coordinates and Global nodal displacements


le
F1 F3 F5 F7
F2 F4 F6 F8 F10 F9
1 2 3 4
1 2 3 4 5 Global nodes
x=0 x = 2m x = 4m x = 6m x = 8m

Fig. 8(b) Global nodal forces/moments


le

No. of elements, Nelements=4; Number of nodes, Nnodes = 5;


x-coordinates of node 1, 2, 3, 4, 5 are, xx = [0; 2; 4; 6; 8]; udl, Po = 0,
Concentrated loads/moments are at nodes 2, 3, 5 (see Figs. 8 and 8(b)) and they are,
F4  12E3N  m; F5  6E3N ; F9  6E3N (see Fig. 8(b)).
Therefore, F(4) = 12E3; F(5) = -6E3; F(9) = -6E3;
predof is the boundary conditions, for simply supported beam deflection at supports i.e. at node 1
and at node 4 are zero. Therefore (see Fig. 8(a)), q1  0 and q7  0 and hence, predof = [1; 7] (Gdof
1 for q1  0 and 7 for q7  0 ).

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