You are on page 1of 12

ME 471 Quiz 2 Solution

1.) Let D = {(x, y) : 0 < x < 2, 0 < y < 3} be a rectangle in the x, y-plane. (a) Derive the weak form of the
following initial-boundary value problem: Find u(x, y, t) such that
u
u = 0,
t

(x, y) D, t > 0,

where = 2 /x2 + 2 /y 2

with initial value u(x, y, 0) = 0, (x, y) D, and boundary conditions (for t > 0, (x, y) D): u/y(x, 0, t) = 3x2 ,
u(x, 3, t) = 9(x2 3), u/x(0, y, t) = 0, and u(2, y, t) = 12y y 3 .
The weak form is obtained by multiplying the equation by a test function v(x, y) and integrating over the domain
D.
Z
Z
u
dA
vu dA = 0
v
D
D t
where v is required to vanish on D1 = {(x, 3) : 0 x 2} {(2, y) : 0 y 3} (where essential boundary
conditions are imposed). Using the identity vu = vu + v u and the divergence theorem, we eliminate the
second derivative terms in the above expression to obtain
Z
Z
Z
u
u
v
dA +
v u dA =
ds,
v
t
n
D
D2
D
where D2 = D D1 . In the integral on the right above, u/n vanishes on the line x = 0, and on the line y = 0
u/n = u/x = 3x2 , that is
Z
Z 3
u
3x2 v dx.
ds =
v
n
0
D2
(b) Use the finite element method with bilinear isoparametric elements and numerical quadrature to obtain an
approximate solution of the weak form of the problem.
function heat_eq(nx,ny)
%--------------------------------------------------------------------% For a fixed number of elements replace heat_eq(nx,ny) by heat_eq()
% and uncomment the line below (creating 24 elements in this example)
% nx=4; ny=6;
%--------------------------------------------------------------------%
%
Solve the two-dimensional Heat equation
%
u,t-u,xx - u,yy =f(x,y,t), (x,y) in a given rectangle
%
using isoparametric four-node quadrilateral elements
%
% Variable descriptions
%
k = element matrix
%
f = element vector
%
kk = system matrix
%
ff = system vector
%
gcoord = coordinate values of each node
%
nodes = nodal connectivity of each element
%
index = a vector containing system dofs associated with each element
%
bcdof = a vector containing dofs associated with boundary conditions
%
bcval = a vector containing boundary condition values associated with
%
the dofs in bcdof
%
point2 - integration (or sampling) points
%
weight2 - weighting coefficients
%
nglx - number of integration points along x-axis
%
ngly - number of integration points along y-axis
%
xcoord - x coordinate values of nodes
%
ycoord - y coordinate values of nodes
%
jacob2 - jacobian matrix

%
shape - four-node quadrilateral shape functions
%
dhdr - derivatives of shape functions w.r.t. natural coord. r
%
dhds - derivatives of shape functions w.r.t. natural coord. s
%
dhdx - derivatives of shape functions w.r.t. physical coord. x
%
dhdy - derivatives of shape functions w.r.t. physical coord. y
%----------------------------------------------------------------------------%-----------------------------------% input data for control parameters
%-----------------------------------% Set the domain
bl=[0,0]; ur=[2,3];
nx1=nx+1; ny1=ny+1;
nel=nx*ny;
nnel=4;
ndof=1;
nnode=nx1*ny1;
nglx=2; ngly=2;
sdof=nnode*ndof;
edof=nnel*ndof;

% number of elements
% number of nodes per element
% number of dofs per node
% total number of nodes in system
% use 2x2 integration rule
% total system dofs
% dofs per element

deltt=0.04;
% time step size for transient analysis
stime=0.0;
% initial time
ftime=4;
% 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(bl,ur, nx, ny, 1);
gridplot(nx, ny, gcoord, nodes, 1);
input(press any key to continue);
close all
%------------------------------------% input data for boundary conditions
%------------------------------------bcdof=[nx1:nx1:nx1*ny1, nx1*ny1-nx:nx1*ny1-1];
nbcdof=length(bcdof);
bcval=zeros(1,nbcdof);
yvals=gcoord(bcdof(1:ny1),2);
bcval(1:ny1)=12*yvals-yvals.^3;
xvals=gcoord(bcdof(ny1+1:nbcdof),1);
bcval(ny1+1:nbcdof)=9*(xvals.^2-3);
%----------------------------------------% For a fixed number of elements in the domain (nx=4, ny=6 in this example)
% essential boundary conditions can also be input by hand in the
% manner shown below
%
% bcdof=[5
10
15
20
25
30
35
31
32
33
34];
% bcval=[0,5.8750,11.0,14.6250,16.0,14.3750,9.0,-27.0,-24.750,-18.0,-6.750];
%
% As a second alternative, the slightly more elegant approach below can be used
%
% bcdof=[5 10 15 20 25 30 35 31 32 33 34]; % length=11

% bcval=zeros(1,11);
% xvals=[0:0.5:1.5]; yvals=[0:0.5:3];
% bcval(1:7)=12*yvals-yvals.^3; bcval(8:11)=9*(xvals.^2-3);
%
%----------------------------------------% initialization of matrices and vectors
%----------------------------------------ff=zeros(sdof,1);
% initialization of system force vector
kk=zeros(sdof,sdof);
% initialization of system matrix
mm=zeros(sdof,sdof);
% initialization of system mass matrix
index=zeros(nnel*ndof,1); % initialization of index vector
% Note: The only contribution to ff in this problem is from the boundary fluxes
%----------------------------------------------------------% loop for computation and assembly of element matrices
%----------------------------------------------------------[point2,weight2]=feglqd2(nglx,ngly);
for iel=1:nel

% sampling points & weights

% loop for the total number of elements

for i=1:nnel
nd(i)=nodes(iel,i);
xcoord(i)=gcoord(nd(i),1);
ycoord(i)=gcoord(nd(i),2);
end

% extract connected node for (iel)-th element


% extract x value of the node
% extract y value of the node

k=zeros(edof,edof);
% initialization of element matrix to zero
m=zeros(edof,edof);
% element mass matrix
%-------------------------------% numerical integration
%-------------------------------for intx=1:nglx
x=point2(intx,1);
wtx=weight2(intx,1);
for inty=1:ngly
y=point2(inty,2);
wty=weight2(inty,2) ;

% sampling point in x-axis


% weight in x-axis
% sampling point in y-axis
% weight in y-axis

[shape,dhdr,dhds]=feisoq4(x,y); % compute shape functions and


% derivatives at sampling point
jacob2=fejacob2(nnel,dhdr,dhds,xcoord,ycoord);
detjacob=det(jacob2);
invjacob=inv(jacob2);

% compute Jacobian

% determinant of Jacobian
% inverse of Jacobian matrix

[dhdx,dhdy]=federiv2(nnel,dhdr,dhds,invjacob); % derivatives w.r.t.


% physical coordinate
%-----------------------------% compute element matrix
%-----------------------------for i=1:edof
for j=1:edof

k(i,j)=k(i,j)+(dhdx(i)*dhdx(j)+dhdy(i)*dhdy(j))*wtx*wty*detjacob;
m(i,j)=m(i,j)+shape(i)*shape(j)*wtx*wty*detjacob;
end
end
end % y loop
end % x loop
index=feeldof(nd,nnel,ndof);% extract system dofs associated with element
%---------------------------------% assemble element mass and stiffness matrices
%---------------------------------kk=feasmbl1(kk,k,index);
mm=feasmbl1(mm,m,index);
end

% end of element loops

%-----------------------------------% Now compute the flux contribution to the load vector.


% This is independent of time for this problem -- so does not
% have to be computed in the time integration loop.
% The loop below could really be moved into the main assembly loop
% and then the textbook file feasmbl2() could be used to
% assemble kk and ff at the same time.
%-----------------------------------[point1, weight1]=feglqd1(nglx);
for iel=1:nx % loop over elements with boundary on y=0, 0<=x<=2 (local nodes 1,2)
for i=1:nnel
nd(i)=nodes(iel,i);
% extract connected node for (iel)-th element
xcoord(i)=gcoord(nd(i),1); % x value of the node
ycoord(i)=gcoord(nd(i),2); % extract y value of the node
end
f=zeros(4,1);
for intr=1:nglx
r=point1(intr);
wtx=weight1(intr);
[shape,dhdr,dhds]=feisoq4(r,-1); % master element shape functions at integration pt (r,-1)
[shape1,dhdr1]=feisol2(r); % master element shape functions restricted to s=-1
xiso=shape(1:2)*xcoord(1:2); % physical element x values, x=sum H_j(r,-1)x_j
jacob1=fejacob1(2,dhdr1,xcoord(1:2));
for i=1:2
f(i)=f(i)+shape(i)*(-3*xiso.^2)*wtx*jacob1; % only shape(1),shape(2) nonzero
end
end
for i=1:2 ff(nd(i))=ff(nd(i))+f(i); end % assemble load vector
end
%compute steady state
us=zeros(sdof,1); ssol=zeros(sdof,1);
[kk,ff]=feaplyc2(kk,ff,bcdof,bcval);
us=kk\ff;
for i=1:nnode
x=gcoord(i,1); y=gcoord(i,2);
ssol(i)=3*x^2*y-y^3;

end
disp([(1:nnode), us, ssol]);
%----------------------------%
loop for time integration: backwards time difference method used
%----------------------------fsol=zeros(sdof,1); %initial value of solution
sol(1,1)=fsol((nnode+1)/2);

kn=mm+deltt*kk;
for it=1:ntime

% store time history solution for middle node (assumes nnode is odd)

% compute effective system matrix (time invariant)


% start loop for time integration

fn=deltt*ff+mm*fsol;

% compute effective column vector

[kn,fn]=feaplyc2(kn,fn,bcdof,bcval); % apply essential boundary conditions


fsol=kn\fn;

% solve the matrix equation

sol(1,it+1)=fsol((nnode+1)/2); %
end

%----------------------------------% analytical solution at node middle node


%----------------------------------xypt=gcoord((nnode+1)/2,:);
for it=1:ntime+1
tt=(it-1)*deltt;
esol(it)=3*xypt(1)^2*xypt(2)-xypt(2)^3+trans(xypt(1),xypt(2),tt, 10);
end
%-----------------------------------% plot the solution at nodes middle node
%-----------------------------------time=0:deltt:ntime*deltt;
plot(time,sol(1,:), time, esol, o);
xlabel(Time)
ylabel(Solution at nodes)
%===================================================================
% end of main routine
%===================================================================

function [point1,weight1]=feglqd1(ngl)
%------------------------------------------------------------------% Purpose:
%
determine the integration points and weighting coefficients
%
of Gauss-Legendre quadrature for one-dimensional integration

%
% Synopsis:
%
[point1,weight1]=feglqd1(ngl)
%
% Variable Description:
%
ngl - number of integration points
%
point1 - vector containing integration points
%
weight1 - vector containing weighting coefficients
%------------------------------------------------------------------%

initialization
point1=zeros(ngl,1);
weight1=zeros(ngl,1);

find corresponding integration points and weights


if ngl==1
point1(1)=0.0;
weight1(1)=2.0;

% 1-point quadrature rule

elseif ngl==2
% 2-point quadrature rule
point1(1)=-0.577350269189626;
point1(2)=-point1(1);
weight1(1)=1.0;
weight1(2)=weight1(1);
elseif ngl==3
% 3-point quadrature rule
point1(1)=-0.774596669241483;
point1(2)=0.0;
point1(3)=-point1(1);
weight1(1)=0.555555555555556;
weight1(2)=0.888888888888889;
weight1(3)=weight1(1);
elseif ngl==4
% 4-point quadrature rule
point1(1)=-0.861136311594053;
point1(2)=-0.339981043584856;
point1(3)=-point1(2);
point1(4)=-point1(1);
weight1(1)=0.347854845137454;
weight1(2)=0.652145154862546;
weight1(3)=weight1(2);
weight1(4)=weight1(1);
else

end

% 5-point quadrature rule


point1(1)=-0.906179845938664;
point1(2)=-0.538469310105683;
point1(3)=0.0;
point1(4)=-point1(2);
point1(5)=-point1(1);
weight1(1)=0.236926885056189;
weight1(2)=0.478628670499366;
weight1(3)=0.568888888888889;
weight1(4)=weight1(2);
weight1(5)=weight1(1);

%------------------------------------------------------------------function [point2,weight2]=feglqd2(nglx,ngly)
%------------------------------------------------------------------% Purpose:
%
determine the integration points and weighting coefficients
%
of Gauss-Legendre quadrature for two-dimensional integration
%
% Synopsis:
%
[point2,weight2]=feglqd2(nglx,ngly)
%
% Variable Description:
%
nglx - number of integration points in the x-axis
%
ngly - number of integration points in the y-axis
%
point2 - vector containing integration points
%
weight2 - vector containing weighting coefficients
%------------------------------------------------------------------%

determine the largest one between nglx and ngly


if nglx > ngly
ngl=nglx;
else
ngl=ngly;
end

initialization
point2=zeros(ngl,2);
weight2=zeros(ngl,2);

find corresponding integration points and weights


[pointx,weightx]=feglqd1(nglx);
[pointy,weighty]=feglqd1(ngly);

% quadrature rule for x-axis


% quadrature rule for y-axis

quadrature for two-dimension


for intx=1:nglx
point2(intx,1)=pointx(intx);
weight2(intx,1)=weightx(intx);
end

% quadrature in x-axis

for inty=1:ngly
point2(inty,2)=pointy(inty);
weight2(inty,2)=weighty(inty);
end

% quadrature in y-axis

%-------------------------------------------------------------------

function [shapeq4,dhdrq4,dhdsq4]=feisoq4(rvalue,svalue)
%-----------------------------------------------------------------------% Purpose:
%
compute isoparametric four-node quadilateral shape functions

%
and their derivatves at the selected (integration) point
%
in terms of the natural coordinate
%
% Synopsis:
%
[shapeq4,dhdrq4,dhdsq4]=feisoq4(rvalue,svalue)
%
% Variable Description:
%
shapeq4 - shape functions for four-node element
%
dhdrq4 - derivatives of the shape functions w.r.t. r
%
dhdsq4 - derivatives of the shape functions w.r.t. s
%
rvalue - r coordinate value of the selected point
%
svalue - s coordinate value of the selected point
%
% Notes:
%
1st node at (-1,-1), 2nd node at (1,-1)
%
3rd node at (1,1), 4th node at (-1,1)
%-----------------------------------------------------------------------% shape functions
shapeq4(1)=0.25*(1-rvalue)*(1-svalue);
shapeq4(2)=0.25*(1+rvalue)*(1-svalue);
shapeq4(3)=0.25*(1+rvalue)*(1+svalue);
shapeq4(4)=0.25*(1-rvalue)*(1+svalue);
% derivatives
dhdrq4(1)=-0.25*(1-svalue);
dhdrq4(2)=0.25*(1-svalue);
dhdrq4(3)=0.25*(1+svalue);
dhdrq4(4)=-0.25*(1+svalue);
dhdsq4(1)=-0.25*(1-rvalue);
dhdsq4(2)=-0.25*(1+rvalue);
dhdsq4(3)=0.25*(1+rvalue);
dhdsq4(4)=0.25*(1-rvalue);
%-------------------------------------------------------------------

function [dhdx,dhdy]=federiv2(nnel,dhdr,dhds,invjacob)
%-----------------------------------------------------------------------% Purpose:
%
determine derivatives of 2-D isoparametric shape functions with
%
respect to physical coordinate system
%
% Synopsis:
%
[dhdx,dhdy]=federiv2(nnel,dhdr,dhds,invjacob)
%
% Variable Description:
%
dhdx - derivative of shape function w.r.t. physical coordinate x
%
dhdy - derivative of shape function w.r.t. physical coordinate y
%
nnel - number of nodes per element
%
dhdr - derivative of shape functions w.r.t. natural coordinate r
%
dhds - derivative of shape functions w.r.t. natural coordinate s
%
invjacob - inverse of 2-D Jacobian matrix
%------------------------------------------------------------------------

for i=1:nnel
dhdx(i)=invjacob(1,1)*dhdr(i)+invjacob(1,2)*dhds(i);
dhdy(i)=invjacob(2,1)*dhdr(i)+invjacob(2,2)*dhds(i);
end
%-------------------------------------------------------------------

function [jacob2]=fejacob2(nnel,dhdr,dhds,xcoord,ycoord)
%-----------------------------------------------------------------------% Purpose:
%
determine the Jacobian for two-dimensional mapping
%
% Synopsis:
%
[jacob2]=fejacob2(nnel,dhdr,dhds,xcoord,ycoord)
%
% Variable Description:
%
jacob2 - Jacobian for one-dimension
%
nnel - number of nodes per element
%
dhdr - derivative of shape functions w.r.t. natural coordinate r
%
dhds - derivative of shape functions w.r.t. natural coordinate s
%
xcoord - x axis coordinate values of nodes
%
ycoord - y axis coordinate values of nodes
%-----------------------------------------------------------------------jacob2=zeros(2,2);
for i=1:nnel
jacob2(1,1)=jacob2(1,1)+dhdr(i)*xcoord(i);
jacob2(1,2)=jacob2(1,2)+dhdr(i)*ycoord(i);
jacob2(2,1)=jacob2(2,1)+dhds(i)*xcoord(i);
jacob2(2,2)=jacob2(2,2)+dhds(i)*ycoord(i);
end
%-------------------------------------------------------------------

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
%------------------------------------------------------------------function [jacob1]=fejacob1(nnel,dhdr,xcoord)
%------------------------------------------------------------------% Purpose:
%
determine the Jacobian for one-dimensional mapping
%
% Synopsis:
%
[jacob1]=fejacob1(nnel,dhdr,xcoord)
%
% Variable Description:
%
jacob1 - Jacobian for one-dimension
%
nnel - number of nodes per element
%
dhdr - derivative of shape functions w.r.t. natural coordinate
%
xcoord - x axis coordinate values of nodes
%------------------------------------------------------------------jacob1=0.0;
for i=1:nnel
jacob1=jacob1+dhdr(i)*xcoord(i);
end
%-------------------------------------------------------------------function [shape,dhdr]=feisol2(rvalue)
%-------------------------------------------------------%Purpose
%
Compute isoparametric 2-node shape functions
%
and their derivatives at the selected
%
point in terms of the natural coordinate.
%
%Synopsis:
%
[shape,dhdr]=kwisol2(rvalue)
%
%Variable Description:
%
shape - shape functions for the linear element
%
dhdr - derivatives of shape functions
%
rvalue - r coordinate valute of the selected point
%
%Notes:
%
1st node at rvalue=-1
%
2nd node at rvalue=1
%-------------------------------------------------------shape(1)=0.5*(1.0-rvalue);

% first shape function

shape(2)=0.5*(1.0+rvalue);

% second shape function

dhdr(1)=-0.5; % derivative of the first shape function


dhdr(2)=0.5; % derivative of the second shape function
%--------------------------------------------------------function t=trans(x,y,t,nterms)
sgn_m=-1; sgn_n=-1;
sum=0;
for m=1:nterms
lambda_m=(2*m-1)*pi/4;lamsq_m=lambda_m*lambda_m;
cosine_m=cos(lambda_m*x);
for n=1:nterms
lambda_n=(2*n-1)*pi/6;lamsq_n=lambda_n*lambda_n;
cosine_n=cos(lambda_n*y);
expfnc=exp((-lamsq_n-lamsq_m)*t);
fac=expfnc*(3*sgn_n+4/lambda_n+(6*sgn_n+2/lambda_n)*...
(1/lambda_n^2-1/lambda_m^2));
sum=sum+sgn_m*(fac/(lambda_m*lambda_n))*cosine_m*cosine_n ;
sgn_n=-sgn_n;
end
sgn_m=-sgn_m;
end
t=-2*sum;
%----------------------------------------------------------

You might also like