me471_hwk05ans
DESCRIPTION
Finite ElementTRANSCRIPT
ME 471 Problem Set 5Solutions
5.8) The weak form of the given initial boundary value problem is
∫ 3
0
(
v∂u
∂t+ 0.1
∂v
∂x
∂u
∂x
)
dx =
∫ 3
010v dx+ 0.1v(3, t)
∂u
∂x(3, t)
where u(0, t) = 100, ∂u/∂x(3, t) = 100 exp(−t), and u(x, 0) = 50. An m-file to solve this problem isgiven below
function prob5_8(xvec, nstep, dt)
% 1D transient heat conduction problem:
% INPUTS:
% xvec = grid points in [0,3], xvec=[x(1)=0,x(2),...,x(n+1)=3]
% nstep = Number of time steps
% dt = time increment
%
sdof=length(xvec); % number of nodes
n=sdof-1; % number of elements
nodes=[[1:n]’ [2:n+1]’]; % configuration matrix
mm=zeros(sdof,sdof); kk=zeros(sdof,sdof); ff=zeros(sdof,1);
for iel=1:n
gn=nodes(iel,:);
h=xvec(gn(2))-xvec(gn(1));
ke=elstif(h);
me=elmass(h);
fe=elload(h);
for ii=1:2 % assemble mass and stiffness
gn1=gn(ii);
ff(gn1)=ff(gn1)+fe(ii);
for jj=1:2
gn2=gn(jj);
mm(gn1,gn2)=mm(gn1,gn2)+me(ii,jj);
kk(gn1,gn2)=kk(gn1,gn2)+ke(ii,jj);
end
end
end
uold=init_val(xvec’); % set inital conditions
effrc=zeros(sdof,1); fnew=zeros(sdof,1);
t=0;
close all
hold on
plot(xvec, uold); % plot initial condition
for k=1:nstep % use implicit (backward difference) method
fnew=ff; fnew(sdof)=ff(sdof)+10*exp(-t-dt); % load vector at t+dt
effrc=mm*uold+dt*fnew; % effective forcing
aa=mm+dt*kk;
aa(1,:)=0; aa(1,1)=1; effrc(1)=100; % impose bc at x=0 and time t
fsol=aa\effrc;
uold=fsol;
if(rem(k,50)==0) plot(xvec,uold); end
t=t+dt;
end
esol=exac(xvec);
plot(xvec, uold,’r’, xvec, esol, ’o’);
hold off
%============================================
% end of main program
%============================================
function me=elmass(h)
% element mass matrix
me=(h/6)*[2 1; 1 2];
%============================================
function ke=elstif(h)
% element stiffness matrix
ke=0.1*(1/h)*[1 -1; -1 1];
%============================================
function fe=elload(h)
% element load vector
fe=5*h*[1;1];
%============================================
function h=init_val(x)
h=50*ones(size(x));
%============================================
function u=exac(x)
% steady state solution
u=100+300*x-50*x.^2;
5.13) This and Problem 5.15 require the solution of the initial boundary value problem
∂u
∂t−∆u = 0, 0 < x, y < 1
with u given on the entire boundary of the unit square. Using forward time differencing leads to them-file (based on Ex5.11.1 of the texbook)
function prob5_13()
nx=4; ny=2;
nx1=nx+1; ny1=ny+1;
nel=nx*ny; % number of elements
nnel=4; % number of nodes per element
ndof=1; % number of dofs per node
nnode=nx1*ny1; % total number of nodes in system
sdof=nnode*ndof; % total system dofs
deltt=0.01; % time step size for transient analysis
stime=0.0; % initial time
ftime=1; % termination time
ntime=fix((ftime-stime)/deltt); % number of time increment
%---------------------------------------------
% input data for nodal coordinate values
% gcoord(i,j) where i->node no. and j->x or y
% nodes(i,j) where i-> element no. and j-> connected nodes
%---------------------------------------------
[gcoord, nodes]=gridgen([0,0],[1,1],nx,ny,1);
gridplot(nx,ny,gcoord,nodes,1);
%-------------------------------------
% input data for boundary conditions
%-------------------------------------
bcdof=[1:nx1, nx1+1:nx1:ny*nx1+1, ny*nx1+2:nnode-1, 2*nx1:nx1:nnode];
lendof=length(bcdof);
bcval=zeros(1,lendof);
bcval(lendof-ny+1:lendof)=200; % last ny nodes at 200
bcval(lendof-ny-nx+2:lendof-ny)=100; %next to last nx-1 nodes at 100
[(1:lendof)’,bcdof’, bcval’]
%-----------------------------------------
% initialization of matrices and vectors
%-----------------------------------------
ff=zeros(sdof,1); % initialization of system vector
fn=zeros(sdof,1); % initialization of effective system vector
fsol=zeros(sdof,1); % solution vector
sol=zeros(2,ntime+1); % vector containing time history solution
kk=zeros(sdof,sdof); % initialization of system matrix
mm=zeros(sdof,sdof); % initialization of system matrix
index=zeros(nnel*ndof,1); % initialization of index vector
%-----------------------------------------------------------------
% computation of element matrices and vectors and their assembly
%-----------------------------------------------------------------
for iel=1:nel % loop for the total number of elements
nd(1)=nodes(iel,1); % 1st connected node for (iel)-th element
nd(2)=nodes(iel,2); % 2nd connected node for (iel)-th element
nd(3)=nodes(iel,3); % 3rd connected node for (iel)-th element
nd(4)=nodes(iel,3); % 4th connected node for (iel)-th element
x1=gcoord(nd(1),1); y1=gcoord(nd(1),2);% coord values of 1st node
x2=gcoord(nd(2),1); y2=gcoord(nd(2),2);% coord values of 2nd node
x3=gcoord(nd(3),1); y3=gcoord(nd(3),2);% coord values of 3rd node
x4=gcoord(nd(4),1); y4=gcoord(nd(4),2);% coord values of 4th node
xleng=x2-x1; % element size in x-axis
yleng=y4-y1; % element size in y-axis
index=feeldof(nd,nnel,ndof);% extract system dofs associated with element
k=felp2dr4(xleng,yleng); % compute element matrix (time-independent term)
m=felpt2r4(xleng,yleng); % compute element matrix (time-dependent term)
kk=feasmbl1(kk,k,index); % assemble element matrices
mm=feasmbl1(mm,m,index); % assemble element matrices
end
%-----------------------------
% loop for time integration
%-----------------------------
for in=1:sdof
fsol(in)=0.0; % initial condition
end
sol(1,1)=fsol(8); % store time history solution for node no. 8
sol(2,1)=fsol(9); % store time history solution for node no. 9
for it=1:ntime % start loop for time integration
fn=deltt*ff+(mm-deltt*kk)*fsol; % compute effective column vector
[mm,fn]=feaplyc2(mm,fn,bcdof,bcval); % apply boundary condition
fsol=mm\fn; % solve the matrix equation
sol(1,it+1)=fsol(8); % store time history solution for node no. 8
sol(2,it+1)=fsol(9); % store time history solution for node no. 9
end
%-----------------------------------
% analytical solution at node 8
%-----------------------------------
%------------------------------------
% plot the solution at nodes 8 and 9
%------------------------------------
time=0:deltt:ntime*deltt;
plot(time,sol(1,:),’*’);
xlabel(’Time’)
ylabel(’Solution at nodes’)
%---------------------------------------------------------------
function [k]=felp2dr4(xleng,yleng)
%-------------------------------------------------------------------
% Purpose:
% element matrix for two-dimensional Laplace’s equation
% using four-node bilinear rectangular element
%
% Synopsis:
% [k]=felp2dr4(xleng,yleng)
%
% Variable Description:
% k - element stiffness matrix (size of 4x4)
% xleng - element size in the x-axis
% yleng - element size in the y-axis
%-------------------------------------------------------------------
% element matrix
k(1,1)=(xleng*xleng+yleng*yleng)/(3*xleng*yleng);
k(1,2)=(xleng*xleng-2*yleng*yleng)/(6*xleng*yleng);
k(1,3)=-0.5*k(1,1);
k(1,4)=(yleng*yleng-2*xleng*xleng)/(6*xleng*yleng);
k(2,1)=k(1,2); k(2,2)=k(1,1); k(2,3)=k(1,4); k(2,4)=k(1,3);
k(3,1)=k(1,3); k(3,2)=k(2,3); k(3,3)=k(1,1); k(3,4)=k(1,2);
k(4,1)=k(1,4); k(4,2)=k(2,4); k(4,3)=k(3,4); k(4,4)=k(1,1);
function [m]=felpt2r4(xleng,yleng)
%-------------------------------------------------------------------
% Purpose:
% element matrix of transient term for two-dimensional Laplace’s
% equation using four-node bilinear rectangular element
%
% Synopsis:
% [m]=felpt2r4(xleng,yleng)
%
% Variable Description:
% m - element stiffness matrix (size of 4x4)
% xleng - element size in the x-axis
% yleng - element size in the y-axis
%-------------------------------------------------------------------
% element matrix
m=(xleng*yleng/36)*[4 2 1 2;
2 4 2 1;
1 2 4 2;
2 1 2 4];
function [kk]=feasmbl1(kk,k,index)
%----------------------------------------------------------
% Purpose:
% Assembly of element matrices into the system matrix
%
% Synopsis:
% [kk]=feasmbl1(kk,k,index)
%
% Variable Description:
% kk - system matrix
% k - element matri
% index - d.o.f. vector associated with an element
%-----------------------------------------------------------
edof = length(index);
for i=1:edof
ii=index(i);
for j=1:edof
jj=index(j);
kk(ii,jj)=kk(ii,jj)+k(i,j);
end
end
function [kk,ff]=feaplyc2(kk,ff,bcdof,bcval)
%----------------------------------------------------------
% Purpose:
% Apply constraints to matrix equation [kk]{x}={ff}
%
% Synopsis:
% [kk,ff]=feaplybc(kk,ff,bcdof,bcval)
%
% Variable Description:
% kk - system matrix before applying constraints
% ff - system vector before applying constraints
% bcdof - a vector containging constrained d.o.f
% bcval - a vector containing contained value
%
% For example, there are constraints at d.o.f=2 and 10
% and their constrained values are 0.0 and 2.5,
% respectively. Then, bcdof(1)=2 and bcdof(2)=10; and
% bcval(1)=1.0 and bcval(2)=2.5.
%-----------------------------------------------------------
n=length(bcdof);
sdof=size(kk);
for i=1:n
c=bcdof(i);
for j=1:sdof
kk(c,j)=0;
end
kk(c,c)=1;
ff(c)=bcval(i);
end
function [index]=feeldof(nd,nnel,ndof)
%----------------------------------------------------------
% Purpose:
% Compute system dofs associated with each element
%
% Synopsis:
% [index]=feeldof(nd,nnel,ndof)
%
% Variable Description:
% index - system dof vector associated with element "iel"
% iel - element number whose system dofs are to be determined
% nnel - number of nodes per element
% ndof - number of dofs per node
%-----------------------------------------------------------
edof = nnel*ndof;
k=0;
for i=1:nnel
start = (nd(i)-1)*ndof;
for j=1:ndof
k=k+1;
index(k)=start+j;
end
end
5.15) Solving the same problem using Crank-Nicolson differencing leads to the m-file (based onEx5.11.5 of the texbook)
function prob5_15()
nx=4; ny=4;
nx1=nx+1; ny1=ny+1;
nel=nx*ny; % number of elements
nnel=4; % number of nodes per element
ndof=1; % number of dofs per node
nnode=nx1*ny1; % total number of nodes in system
sdof=nnode*ndof; % total system dofs
deltt=0.01; % time step size for transient analysis
stime=0.0; % initial time
ftime=1; % termination time
ntime=fix((ftime-stime)/deltt); % number of time increment
%---------------------------------------------
% input data for nodal coordinate values
% gcoord(i,j) where i->node no. and j->x or y
% nodes(i,j) where i-> element no. and j-> connected nodes
%---------------------------------------------
[gcoord, nodes]=gridgen([0,0],[1,1],nx,ny,1);
gridplot(nx,ny,gcoord,nodes,1);
input(’press any key to continue’);
%-------------------------------------
% input data for boundary conditions
%-------------------------------------
bcdof=[1:nx1, nx1+1:nx1:ny*nx1+1, ny*nx1+2:nnode-1, 2*nx1:nx1:nnode];
lendof=length(bcdof);
bcval=zeros(1,lendof);
bcval(lendof-ny+1:lendof)=200; % last ny nodes at 200
bcval(lendof-ny-nx+2:lendof-ny)=100; %next to last nx-1 nodes at 100
bcval(lendof)=150; bcval(nx1)=100; bcval(lendof-ny-nx+1)=50; % average values at corners
[(1:lendof)’,bcdof’, bcval’]
%-----------------------------------------
% initialization of matrices and vectors
%-----------------------------------------
ff=zeros(sdof,1); % initialization of system vector
fn=zeros(sdof,1); % initialization of effective system vector
fsol=zeros(sdof,1); % solution vector
sol=zeros(2,ntime+1); % vector containing time history solution
kk=zeros(sdof,sdof); % initialization of system matrix
mm=zeros(sdof,sdof); % initialization of system matrix
index=zeros(nnel*ndof,1); % initialization of index vector
%-----------------------------------------------------------------
% computation of element matrices and vectors and their assembly
%-----------------------------------------------------------------
for iel=1:nel % loop for the total number of elements
nd(1)=nodes(iel,1); % 1st connected node for (iel)-th element
nd(2)=nodes(iel,2); % 2nd connected node for (iel)-th element
nd(3)=nodes(iel,3); % 3rd connected node for (iel)-th element
nd(4)=nodes(iel,3); % 4th connected node for (iel)-th element
x1=gcoord(nd(1),1); y1=gcoord(nd(1),2);% coord values of 1st node
x2=gcoord(nd(2),1); y2=gcoord(nd(2),2);% coord values of 2nd node
x3=gcoord(nd(3),1); y3=gcoord(nd(3),2);% coord values of 3rd node
x4=gcoord(nd(4),1); y4=gcoord(nd(4),2);% coord values of 4th node
xleng=x2-x1; % element size in x-axis
yleng=y4-y1; % element size in y-axis
index=feeldof(nd,nnel,ndof);% extract system dofs associated with element
k=felp2dr4(xleng,yleng); % compute element matrix (time-independent term)
m=felpt2r4(xleng,yleng); % compute element matrix (time-dependent term)
kk=feasmbl1(kk,k,index); % assemble element matrices
mm=feasmbl1(mm,m,index); % assemble element matrices
end
%-----------------------------
% loop for time integration
%-----------------------------
for in=1:sdof
fsol(in)=0.0; % initial condition
end
sol(1,1)=fsol((nnode+1)/2); % store time history solution for middle node (assumes nnode is odd)
kn=2*mm+deltt*kk; % compute effective system matrix
for it=1:ntime % start loop for time integration
fn=deltt*ff+(2*mm-deltt*kk)*fsol; % compute effective column vector
[kn,fn]=feaplyc2(kn,fn,bcdof,bcval); % apply boundary condition
fsol=kn\fn; % solve the matrix equation
sol(1,it+1)=fsol((nnode+1)/2); %
end
%-----------------------------------
% analytical solution at node 8
%-----------------------------------
%------------------------------------
% plot the solution at nodes 8 and 9
%------------------------------------
time=0:deltt:ntime*deltt;
plot(time,sol(1,:));
xlabel(’Time’)
ylabel(’Solution at nodes’)
%---------------------------------------------------------------
function [k]=felp2dr4(xleng,yleng)
%-------------------------------------------------------------------
% Purpose:
% element matrix for two-dimensional Laplace’s equation
% using four-node bilinear rectangular element
%
% Synopsis:
% [k]=felp2dr4(xleng,yleng)
%
% Variable Description:
% k - element stiffness matrix (size of 4x4)
% xleng - element size in the x-axis
% yleng - element size in the y-axis
%-------------------------------------------------------------------
% element matrix
k(1,1)=(xleng*xleng+yleng*yleng)/(3*xleng*yleng);
k(1,2)=(xleng*xleng-2*yleng*yleng)/(6*xleng*yleng);
k(1,3)=-0.5*k(1,1);
k(1,4)=(yleng*yleng-2*xleng*xleng)/(6*xleng*yleng);
k(2,1)=k(1,2); k(2,2)=k(1,1); k(2,3)=k(1,4); k(2,4)=k(1,3);
k(3,1)=k(1,3); k(3,2)=k(2,3); k(3,3)=k(1,1); k(3,4)=k(1,2);
k(4,1)=k(1,4); k(4,2)=k(2,4); k(4,3)=k(3,4); k(4,4)=k(1,1);
function [m]=felpt2r4(xleng,yleng)
%-------------------------------------------------------------------
% Purpose:
% element matrix of transient term for two-dimensional Laplace’s
% equation using four-node bilinear rectangular element
%
% Synopsis:
% [m]=felpt2r4(xleng,yleng)
%
% Variable Description:
% m - element stiffness matrix (size of 4x4)
% xleng - element size in the x-axis
% yleng - element size in the y-axis
%-------------------------------------------------------------------
% element matrix
m=(xleng*yleng/36)*[4 2 1 2;
2 4 2 1;
1 2 4 2;
2 1 2 4];
function [kk]=feasmbl1(kk,k,index)
%----------------------------------------------------------
% Purpose:
% Assembly of element matrices into the system matrix
%
% Synopsis:
% [kk]=feasmbl1(kk,k,index)
%
% Variable Description:
% kk - system matrix
% k - element matri
% index - d.o.f. vector associated with an element
%-----------------------------------------------------------
edof = length(index);
for i=1:edof
ii=index(i);
for j=1:edof
jj=index(j);
kk(ii,jj)=kk(ii,jj)+k(i,j);
end
end
function [kk,ff]=feaplyc2(kk,ff,bcdof,bcval)
%----------------------------------------------------------
% Purpose:
% Apply constraints to matrix equation [kk]{x}={ff}
%
% Synopsis:
% [kk,ff]=feaplybc(kk,ff,bcdof,bcval)
%
% Variable Description:
% kk - system matrix before applying constraints
% ff - system vector before applying constraints
% bcdof - a vector containging constrained d.o.f
% bcval - a vector containing contained value
%
% For example, there are constraints at d.o.f=2 and 10
% and their constrained values are 0.0 and 2.5,
% respectively. Then, bcdof(1)=2 and bcdof(2)=10; and
% bcval(1)=1.0 and bcval(2)=2.5.
%-----------------------------------------------------------
n=length(bcdof);
sdof=size(kk);
for i=1:n
c=bcdof(i);
for j=1:sdof
kk(c,j)=0;
end
kk(c,c)=1;
ff(c)=bcval(i);
end
function [index]=feeldof(nd,nnel,ndof)
%----------------------------------------------------------
% Purpose:
% Compute system dofs associated with each element
%
% Synopsis:
% [index]=feeldof(nd,nnel,ndof)
%
% Variable Description:
% index - system dof vector associated with element "iel"
% iel - element number whose system dofs are to be determined
% nnel - number of nodes per element
% ndof - number of dofs per node
%-----------------------------------------------------------
edof = nnel*ndof;
k=0;
for i=1:nnel
start = (nd(i)-1)*ndof;
for j=1:ndof
k=k+1;
index(k)=start+j;
end
end