Matlab Programs
Matlab Programs
clc
disp(' FEA ANALYSIS of Plane Truss. Nodal solution for truss.')
fprintf('Enter the required data. The units must be consistent.\n')
fprintf('Please enter the number of elements in truss\n')
e=input('');
fprintf('\nPlease enter the number of nodes in truss\n')
N=input('');
Xcord=zeros(1,N); % Array containing X and Y coordinates of node
Ycord=zeros(1,N);
for i=1:N % Scan input values of X and Y coordinates of Nodes
fprintf('Enter the X coordinate of node %d\n',i)
Xcord(1,i)=input('');
fprintf('Enter the Y coordinate of node %d\n',i)
Ycord(1,i)=input('');
end
Xcord; %Display X and Y coordinates of nodes
Ycord;
nodtable=[Xcord',Ycord'];% Nodal coordinate table
fprintf('\nNow we will establish element connectivity for truss\n\n')
Elem_ass=zeros(e,2); %first column of element associativity table see
page no 109 Belegundu & chndrupatla
for i=1:e % generates element associativity table
fprintf('SPECIFY THE GLOBAL NODE FOR LOCAL NODE 1 FOR
ELEMENT %d\n',i)
Elem_ass(i,1)=input('');
fprintf('SPECIFY THE GLOBAL NODE FOR LOCAL NODE 2 FOR
ELEMENT %d\n',i)
Elem_ass(i,2)=input('');
end
Elem_ass; %element associativity table
calcx=zeros(e,2);
calcy=zeros(e,2);
for i=1:e
temp1= Elem_ass(i,1); % preperation of elemental x y coordinate
nodal table
temp2= Elem_ass(i,2);
calcx(i,1)=nodtable(temp1,1);
calcx(i,2)=nodtable(temp2,1);
calcy(i,1)=nodtable(temp1,2);
calcy(i,2)=nodtable(temp2,2);
end
calcx;% defines x coordinates of local nodes 1& 2 for all elements
calcy; % defines y coordinates of local nodes 1& 2 for all elements
calc=zeros(e,5);
s=1:1:e;
s=s';
calc(:,1)=s; % Preperation of calc table e x 5
calc(:,2)=calcx(:,1);
calc(:,3)=calcx(:,2);
calc(:,4)=calcy(:,1);
calc(:,5)=calcy(:,2);
calc;
for i=1:e
x1=calc(i,2); % calculation of elemental attributes le l & m
x2=calc(i,3);
y1=calc(i,4);
y2=calc(i,5);
a=[(x2-x1)*(x2-x1)]+[(y2-y1)*(y2-y1)];
le=sqrt(a);
l=(x2-x1)/le;
m=(y2-y1)/le;
elematb(i,1)=i;
elematb(i,2)=le;
elematb(i,3)=l;
elematb(i,4)=m;
end
elematb(:,1)=[]; % Element attribute table
%Enter the area of elements
fprintf('Please enter the material properties and cross sectional areas
for truss members\n')
p=2*e;
area=ones(p,1);
modulus=ones(p,1);
for i=1:e
fprintf('\nPlease enter the C/S Area for element no %d\n',i)
area(i,1)= input('');
end
%Enter the Youngs modulus of elements
for i=1:e
fprintf('\nPlease enter the youngs modulus for element no %d\n',i)
modulus(i,1)= input('');
end
%calculations of local stiffness matrices
bigmat= zeros(4*N,4); % define bigmatrix to store local matrices k1 k2
k3 & so on
for i=1:e
le=elematb(i,1); % take corrosponding elemental attributes
l=elematb(i,2);
m=elematb(i,3);
temp=[area(i,1)*modulus(i,1)]/elematb(i,1);
klocal=[l*l l*m -(l*l) (-l)*m;l*m m*m (-l)*m (-m)*m;(-l)*l (-l)*m
(l*l) l*m;-(l)*m (-m)*m l*m m*m];
klocal=temp*klocal;
a=(4*i)-3;
b=4*i;
bigmat([a:b],[1:4])=klocal; % replace appropriate portion of big
matrix by local matrices k1 k1 k3 & so on
end
bigmat;
G=zeros(2*N,2*N);% Initially define global stiffness matrix
% calculation of global stiffness matrix starts from here
% we will consider Elem_ass table for formation of global stiffness
matrix
for countelem=1:e
a=(4*countelem)-3;
b=4*countelem;
localmtrx= bigmat([a:b],[1:4]); % for given element extract
appropriate matrix from bigmat Matrix
i=1; % set counters
j=1;
p=1;
q=1;
temp1=Elem_ass(countelem,1);
temp2=Elem_ass(countelem,2);
temp3=(temp1*2)-1;
temp4=temp1*2;
temp5=(temp2*2)-1;
temp6=(temp2*2);
temp10=[temp3 temp4 temp5 temp6]; % load appropriate degrees of freedom
while i<=4
for j=1:4
c=temp10(1,i);
d=temp10(1,j);
G(c,d)= G(c,d)+localmtrx(p,q); % build matrix with appropriate
element associativity
j=j+1;
q=q+1;
end
q=1;
i=i+1;
p=p+1;
end
end
G;
% Entry of load and constraints begins here
fprintf('\nPlease enter nodal forces\n')
F=zeros(2*N,1);
for i=1:2*N
fprintf('\nPlease enter the value of force corrosponding to DOF
%d\n',i)
val=input(''); % Scan global load vector
F(i,1)=val;
end
F;
fprintf('\nPlease specify the DOF to be fully constrained\n')
disp('ENTER 1 IF YOUR ANSWER IS YES ELSE ENTER 2 IF YOUR ANSWER IS
NO')
Q=ones(2*N,1);
count=1;
for i=1:2*N
fprintf('\nDO YOU WANT TO CONSTRAINT DOF NO %d \n',i)
A=input('');
if A==1
Q(i,1)=0;
G(count,:)=[]; % Elimination of rows approach
G(:,count)=[];
F(count,:)=[];
count= count;
else
count=count+1;
end
end
G;
F;
nodsol= inv(G)*F;
nodsol;
Q;
j=1;
for i=1:2*N
if Q(i,1)==0
j=j;
else
Q(i,1)=nodsol(j,1);
j=j+1;
end
end
clc
fprintf('\n the Global stiffness matrix is\n')
G fprintf('\nThe nodal solution is\n')
Q
OUTPUT:
MATLAB DOF SOLUTION
DOF Values
1X 0
1Y 0
2X -0.0007
2Y 0
3X 0
3Y 0
4X -0.0039
4Y 0.0008
5X -0.0022
5Y -0.0008
%% Beam Analysis
% This Matlab code help you to calculate the displacements and reactions of
% Beam you may need to change the boundary conditions for different cases of
Beam
%% Initialization
clc; clear; close all;
%% Input Data
Ne = 2; % Number of Elements
L = 1; % m
E = 2.1*10^8; % KN/m2
I = 2120/100^4; % m4
w = 1000; % kN/m
wp = 1000; % kN
BC = [1 2 5]; % Boundary Condition
del_udl = 5*w*L^4/(384*E*I);
del_p = wp*L^3/(48*E*I);
dof = [ 1 2 3 4 5 6];
dof1 = [1 2 3 4];
dof2 = [3 4 5 6];
ndof = length(dof);
KG = zeros(ndof,ndof);
%% Assembly of Elements
le = L/Ne;
eqn = 1; % Belegundu 1, J.N. Reddy 2
if eqn == 1
KE = zeros(4,4);
KE(1,1) = 6;
KE(1,2) = 3*le;
KE(1,3) = -6;
KE(1,4) = 3*le;
KE(2,2) = 2*le^2;
KE(2,3) = -3*le;
KE(2,4) = le^2;
KE(3,3) = 6;
KE(3,4) = -3*le;
KE(4,4) = 2*le^2;
else
KE = zeros(4,4);
KE(1,1) = 6;
KE(1,2) = -3*le;
KE(1,3) = -6;
KE(1,4) = -3*le;
KE(2,2) = 2*le^2;
KE(2,3) = 3*le;
KE(2,4) = le^2;
KE(3,3) = 6;
KE(3,4) = 3*le;
KE(4,4) = 2*le^2;
end
for ii = 2:4,
for jj = 1:ii,
KE(ii,jj) = KE(jj,ii);
end
end
KG(1:4,1:4) = KG(1:4,1:4)+(2*E*I/(le^3)).*KE;
KG(3:6,3:6) = KG(3:6,3:6)+(2*E*I/(le^3)).*KE;
disp('Stiffness Matrix')
disp(KG)
%% Boundary Conditions
K_temp = KG;
for jj = 1:length(BC)
K_temp(:,BC(jj)) = 0;
K_temp(BC(jj),:) = 0;
End
K_temp(~any(K_temp,2),:) = [];
K_temp(:,~any(K_temp,1)) = [];
KR = K_temp;
disp('Stiffness Matrix after eliminations')
disp(KR)
FG = zeros(ndof,1);
% FR = [-2000;(-0.05*wp+w*(L/2)^2*(1/12-1/8));0];
if eqn == 1
F_udl = w*[-L/2;(L^2)/48-(L^2/48);(L^2/48)];
F_pl = wp*[-1;-0.05;0];
else
F_udl = w*[L/2;(L^2)/48-(L^2/48);(L^2/48)];
F_pl = wp*[1;-0.05;0];
end
FR = F_udl+F_pl;
disp('Force Vector')
disp(FR)
%% Solution
UR = KR\FR;
%% Results
UG = [0 0 UR(1) UR(2) 0 UR(3)]';
Rc = KG*(UG);
disp('Displacement in mm & rad')
UR = [UR(1)*1000;UR(2);UR(3)];
disp(UR)
disp('Reaction in kN')
if eqn == 1
Reaction = [Rc(1)+w*L/4; Rc(2)+w*(L/2)^2/12; Rc(5)+w*L/4];
else
Reaction = [Rc(1)-w*L/4; Rc(2)+w*(L/2)^2/12; Rc(5)-w*L/4];
end
disp(Reaction)
OUTPUT:
Stiffness Matrix
427392 106848 -427392 106848 0 0
106848 35616 -106848 17808 0 0
-427392 -106848 854784 0 -427392 106848
106848 17808 0 71232 -106848 17808
0 0 -427392 -106848 427392 -106848
0 0 106848 17808 -106848 35616
Force Vector
1.0e+03 *
-1.5000
-0.0500
0.0208
Reaction in kN
1.0e+03 *
1.2563
0.3063
0.7438
ANSYS SOLUTION
Displacement
-3.4034
-0.0036
0.0214
Reaction in kN
1.240e3
1.4536
0.3234
0.6538
PROGRAM:
clc;
fprintf('Following program demonstrate the calculation of nodal
temperatures\nin a pin fin with insulated tip and other end maintained at
uniform temperature')
fprintf('\nPress any number key to start the program ')
A=input(''); % The user enters the number and program starts
% The Program asks user to input the data
printf('\nPlease enter the thermal conductivity of fin material in W/mC')
kt=input(''); % kt is thermal conductivity
fprintf('\nPlease enter the film heat transfer h in W/m2C ')
h=input(''); % h is film heat transfer co efficient
fprintf('\nEnter the value of bulk fluid temprature in C ')
Tbulk=input(''); % Tbulk is bulk fluid temp in which fin is mounted
fprintf('\nEnter the uniform temperature of one end of fin in C ')
Tunif=input(''); %Tunif is that end of fin which is maintained at uniform
temp
fprintf('\nEnter the diameter of fin in meters ')
d=input('');
fprintf('\nPlease enter the length of fin in meters ')
L=input('');
fprintf('\nEnter the internal heat generated per unit volume in W/cu-meter')
Qgen=input('');
% Calculations of some basic entities is done in this section
pi=3.14;
Ac= 0.25*pi*d*d;
P=pi*d;
% Mesh size specification is here
fprintf('\nEnter the no of elements in which you want to mesh the problem')
N=input('');
le=L/N; %le is length of each element
% calculation of element conductance matrix (LHS) starts here
ke1=(kt/le)*[1 -1;-1 1]; % Firts part of conductance matrix see problem
4.10 P Seshu
ke2=((P*h*le)/(6*Ac))*[2 1;1 2]; % second part of conductance matrix see
problem 4.10 P Seshu
ke= ke1+ke2; % ke = elemental conductance matrix
%Calculation of RHS starts here
%First term of RHS is as follows
RHS1=((P*h)/Ac)*Tbulk*[le/2;le/2];
%Assembly of global stiffness matrix starts here
nods=N+1; % Total no of nodes in problem
Global=zeros(nods,nods);
for i=1:N
Global(i,i)=Global(i,i)+ke(1,1);
Global(i,i+1)=Global(i,i+1)+ke(1,2);
Global(i+1,i)=Global(i+1)+ke(2,1);
Global(i+1,i+1)=Global(i+1,i+1)+ke(2,2);
i=i+1;
end
clc;
fprintf('\nGlobal conductance matrix is as follows\n')
Global
% Assembly of first term in RHS starts here
Load1=zeros(nods,1);
for i=1:N
Load1(i,1)=Load1(i,1)+RHS1(1,1);
Load1(i+1,1)=Load1(i+1,1)+RHS1(2,1); % Load1 is Nodal heat flux
corrosponding to distributed heat source sink
i=i+1;
end
fprintf('\nPlease enter the nodal heat flux at the end which is maintained
at uniform temperature ')
Qwall=input(''); % Qwall is heat flux at the end of fin which is maintened
at uniform temp
Load2=zeros(nods,1);
Load2(1,1)=Load2(1,1)-Qwall;
fprintf('\nPlease enter the heat leakage at insulated tip if any in watt/sq-
meter if not then enter zero value ')
Qtip=input('');
Load2(nods,1)=Load2(nods,1)+Qtip; % Qtip is leakage heat flux at insulated tip
Load=Load1+Load2;
Global(1,:)=[];
% Storing the first column of this matrix for Elimination Approach.
Column=Global(:,1); % To be later used for elimination approach
Global(:,1)=[]; % Applying boundary condition at node of uniform temperature
Load(1,:)=[];
Load=Load-(Tunif*Column); % Elimination approach % Solving the problem
Tnodal=inv(Global)*Load; % Tnodal is nodal load vector calculated
Nodal1=Tnodal'; % converting nodal load vector in row matrix
Nodal1=[Tunif;Tnodal];
fprintf('\nThe nodal temp vector is as follows\n')
Nodal1
Nodal1=Nodal1';
%Plot of nodal temperature begins here
X=zeros(nods,1); % Define dimensions of X Axis
X(1,1)=0;
for i=2:nods
X(i,1)=X(i,1)+(i-1)*le;
end
X=X';
Y=Nodal1;
hold on;
xlabel('Length of fin')
ylabel('Nodal temperature')
lim=[0 L Tbulk Tunif];
title('Variation of nodal temperature of Fin')
axis(lim)
plot(X,Y)
OUTPUT
The nodal temp vector is as follows
Nodal1 =
300.0000 T1
263.9270 T2
237.2738 T3
218.9672 T4
208.2701 T5
204.7516 T6