Professional Documents
Culture Documents
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
for i=1:nnel
nd(i)=nodes(iel,i);
xcoord(i)=gcoord(nd(i),1);
ycoord(i)=gcoord(nd(i),2);
end
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) ;
% compute Jacobian
% determinant of Jacobian
% inverse of Jacobian matrix
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
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)
fn=deltt*ff+mm*fsol;
sol(1,it+1)=fsol((nnode+1)/2); %
end
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);
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
%------------------------------------------------------------------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
%------------------------------------------------------------------%
initialization
point2=zeros(ngl,2);
weight2=zeros(ngl,2);
% 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);
shape(2)=0.5*(1.0+rvalue);