MATLAB Codes For Finite Element Analysis
MATLAB Codes For Finite Element Analysis
% problem9.m
% clear memory
clear all
% E; modulus of elasticity
% L: length of bar
nodeCoordinates=linspace(0,1,numberElements+1);
xx=nodeCoordinates;
L=max(nodeCoordinates);
numberNodes=size(nodeCoordinates,1);
xx=nodeCoordinates(:,1);
for i=1:numberElements;
elementNodes(i,1)=i;
elementNodes(i,2)=i+1;
end
% distributed load
P=-1;
% for structure:
GDof=2*numberNodes; U=zeros(GDof,1);
% clamped at x=0
prescribedDof=[fixedNodeU;fixedNodeV];
% solution
displacements=solution(GDof,prescribedDof,stiffness,force);
% output displacements/reactions
outputDisplacementsReactions(displacements,stiffness,... GDof,prescribedDof)
U=displacements(1:2:2*numberNodes);
plot(nodeCoordinates,U,.)
function [stiffness,force]=...
formStiffnessBernoulliBeam(GDof,numberElements,...
elementNodes,numberNodes,xx,EI,P);
force=zeros(GDof,1);
stiffness=zeros(GDof);
for e=1:numberElements;
indice=elementNodes(e,:) ;
2*(indice(2)-1)+1 2*(indice(2)-1)+2];
% ndof=length(indice);
% length of element
LElem=xx(indice(2))-xx(indice(1)) ;
ll=LElem;
-P*LElem*LElem/12];
force(elementDof)=force(elementDof)+f1;
% stiffness matrix
stiffness(elementDof,elementDof)=...
stiffness(elementDof,elementDof)+k1;
end
% beam.m % % Solves a linear elastic 2D beam problem ( plane stress or strain ) % with several
element types. % % ^ y % | % --------------------------------------------- % | | % | | % ---------> x | 2c % | |
% | L | % --------------------------------------------- % % with the boundary following conditions: % % u_x
= 0 at (0,0), (0,-c) and (0,c) % u_y = 0 at (0,0) % % t_x = y along the edge x=0 % t_y = P*(x^2-c^2)
along the edge x=L % %
****************************************************************************** %
% This file and the supporting matlab files can be found at %
http://www.tam.northwestern.edu/jfc795/Matlab % % by Jack Chessa % Northwestern University
%%
******************************************************************************
clear colordef black state = 0; %
****************************************************************************** %
*** I N P U T *** %
******************************************************************************
tic; disp(************************************************) disp(*** S T A R T I N G R U
N ***) disp(************************************************) disp([num2str(toc),
START]) % MATERIAL PROPERTIES E0 = 10e7; % Youngs modulus nu0 = 0.30; % Poissons ratio %
BEAM PROPERTIES L = 16; % length of the beam c = 2; % the distance of the outer fiber of the
beam from the mid-line % MESH PROPERTIES elemType = Q9; % the element type used in the
FEM simulation; T3 is for a % three node constant strain triangular element, Q4 is for % a four
node quadrilateral element, and Q9 is for a nine % node quadrilateral element. numy = 4; % the
number of elements in the x-direction (beam length) numx = 18; % and in the y-direciton.
plotMesh = 1; % A flag that if set to 1 plots the initial mesh (to make sure % that the mesh is
correct) % TIP LOAD P = -1; % the peak magnitude of the traction at the right edge % STRESS
ASSUMPTION stressState=PLANE_STRESS; % set to either PLANE_STRAIN or "PLANE_STRESS %
nuff said. %
****************************************************************************** %
*** P R E - P R O C E S S I N G *** %
******************************************************************************
% Here we gnerate the finte element mesh (using the approriate elements). % I wont go into too
much detail about how to use these functions. If % one is interested one can type - help function
name at the matlab comand % line to find out more about it. % % The folowing data structures
are used to describe the finite element % discretization: % % node - is a matrix of the node
coordinates, i.e. node(I,j) -> x_Ij % element - is a matrix of element connectivities, i.e. the
connectivity % of element e is given by > element(e,:) -> [n1 n2 n3 ...]; % % To apply boundary
conditions a description of the boundaries is needed. To % accomplish this we use a separate finite
element discretization for each % boundary. For a 2D problem the boundary discretization is a set
of 1D elements. % % rightEdge - a element connectivity matrix for the right edge % leftEdge - Ill
give you three guesses % % These connectivity matricies refer to the node numbers defined in the
% coordinate matrix node. disp([num2str(toc), GENERATING MESH]) switch elemType case Q4 %
here we generate the mesh of Q4 elements nnx=numx+1; nny=numy+1;
node=square_node_array([0 -c],[L -c],[L c],[0 c],nnx,nny); inc_u=1; inc_v=nnx; node_pattern=[ 1 2
nnx+2 nnx+1 ]; element=make_elem(node_pattern,numx,numy,inc_u,inc_v); case Q9 % here we
generate a mehs of Q9 elements nnx=2*numx+1; nny=2*numy+1; node=square_node_array([0 -
c],[L -c],[L c],[0 c],nnx,nny); inc_u=2; inc_v=2*nnx; node_pattern=[ 1 3 2*nnx+3 2*nnx+1 2 nnx+3
2*nnx+2 nnx+1 nnx+2 ]; element=make_elem(node_pattern,numx,numy,inc_u,inc_v); otherwise
%T3 % and last but not least T3 elements nnx=numx+1; nny=numy+1;
node=square_node_array([0 -c],[L -c],[L c],[0 c],nnx,nny); node_pattern1=[ 1 2 nnx+1 ];
node_pattern2=[ 2 nnx+2 nnx+1 ]
% Here we define the boundary discretizations. uln=nnx*(nny-1)+1; % upper left node number
urn=nnx*nny; % upper right node number lrn=nnx; % lower right node number lln=1; % lower left
node number cln=nnx*(nny-1)/2+1; % node number at (0,0) switch elemType case Q9
rightEdge=[ lrn:2*nnx:(uln-1); (lrn+2*nnx):2*nnx:urn; (lrn+nnx):2*nnx:urn ]; leftEdge =[ uln:-
2*nnx:(lrn+1); (uln-2*nnx):-2*nnx:1; (uln-nnx):-2*nnx:1 ]; edgeElemType=L3; otherwise % same
discretizations for Q4 and T3 meshes rightEdge=[ lrn:nnx:(uln-1); (lrn+nnx):nnx:urn ]; leftEdge =[
uln:-nnx:(lrn+1); (uln-nnx):-nnx:1 ]; edgeElemType=L2; end % GET NODES ON DISPLACEMENT
BOUNDARY % Here we get the nodes on the essential boundaries fixedNodeX=[uln lln cln]; % a
vector of the node numbers which are fixed in % the x direction fixedNodeY=[cln]; % a vector of
node numbers which are fixed in % the y-direction uFixed=zeros(size(fixedNodeX)); % a vector of
the x-displacement for the nodes % in fixedNodeX ( in this case just zeros )
vFixed=zeros(size(fixedNodeY)); % and the y-displacements for fixedNodeY
numnode=size(node,1); % number of nodes numelem=size(element,1); % number of elements %
PLOT MESH if ( plotMesh ) % if plotMesh==1 we will plot the mesh clf
plot_mesh(node,element,elemType,g.-); hold on plot_mesh(node,rightEdge,edgeElemType,bo-
); plot_mesh(node,leftEdge,edgeElemType,bo-);
plot(node(fixedNodeX,1),node(fixedNodeX,2),r>);
plot(node(fixedNodeY,1),node(fixedNodeY,2),r^); axis off axis([0 L -c c]) disp((paused)) pause
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%% % DEFINE SYSTEM DATA STRUCTURES % % Here we
define the system data structures % U - is vector of the nodal displacements it is of length
2*numnode. The % displacements in the x-direction are in the top half of U and the % y-
displacements are in the lower half of U, for example the displacement % in the y-direction for
node number I is at U(I+numnode) % f - is the nodal force vector. Its structure is the same as U, %
i.e. f(I+numnode) is the force in the y direction at node I % K - is the global stiffness matrix and is
structured the same as with U and f % so that K_IiJj is at K(I+(i-1)*numnode,J+(j-1)*numnode)
disp([num2str(toc), INITIALIZING DATA STRUCTURES]) U=zeros(2*numnode,1); % nodal
displacement vector
%******************************************************************************
%*** P O S T - P R O C E S S I N G ***
%******************************************************************************
% % Here we plot the stresses and displacements of the solution. As with the % mesh generation
section we dont go into too much detail - use help % function name to get more details.
disp([num2str(toc), POST-PROCESSING]) dispNorm=L/max(sqrt(U(xs).^2+U(ys).^2));
scaleFact=0.1*dispNorm; fn=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
PLOT DEFORMED DISPLACEMENT PLOT figure(fn) clf plot_field(node+scaleFact*[U(xs)
U(ys)],element,elemType,U(ys)); hold on plot_mesh(node+scaleFact*[U(xs)
U(ys)],element,elemType,g.-); plot_mesh(node,element,elemType,w--); colorbar fn=fn+1;
title(DEFORMED DISPLACEMENT IN Y-DIRECTION)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %
COMPUTE STRESS stress=zeros(numelem,size(element,2),3); switch elemType % define
quadrature rule case Q9 stressPoints=[-1 -1;1 -1;1 1;-1 1;0 -1;1 0;0 1;-1 0;0 0 ]; case Q4
stressPoints=[-1 -1;1 -1;1 1;-1 1]; otherwise
stressPoints=[0 0;1 0;0 1]; end for e=1:numelem % start of element loop sctr=element(e,:);
sctrB=[sctr sctr+numnode]; nn=length(sctr); for q=1:nn pt=stressPoints(q,:); % stress point
[N,dNdxi]=lagrange_basis(elemType,pt); % element shape functions J0=node(sctr,:)*dNdxi; %
element Jacobian matrix invJ0=inv(J0); dNdx=dNdxi*invJ0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% % COMPUTE B MATRIX B=zeros(3,2*nn); B(1,1:nn) = dNdx(:,1); B(2,nn+1:2*nn) = dNdx(:,2);
B(3,1:nn) = dNdx(:,2); B(3,nn+1:2*nn) = dNdx(:,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% % COMPUTE ELEMENT STRAIN AND STRESS AT STRESS POINT strain=B*U(sctrB);
stress(e,q,:)=C*strain; end end % of element loop stressComp=1; figure(fn) clf
plot_field(node+scaleFact*[U(xs) U(ys)],element,elemType,stress(:,:,stressComp)); hold on
plot_mesh(node+scaleFact*[U(xs) U(ys)],element,elemType,g.-);
plot_mesh(node,element,elemType,w--); colorbar fn=fn+1; title(DEFORMED STRESS PLOT,
BENDING COMPONENT) %print(fn,-
djpeg90,[beam_,elemType,_sigma,num2str(stressComp),.jpg]) disp([num2str(toc), RUN
FINISHED])