You are on page 1of 9

# Problems in Two Spacial Dimensions

## Bilinear rectangular elements.

We need to discuss briefly an alternate method of finite element interpolation: continuous, bilinear
interpolation on rectangles. Suppose a rectangular grid of lines x = xi , y = yj , i Nn , j Nm
(where Nk = {1, . . . , k}) is drawn in the plane forming a system of rectangles Rij = {(x, y) : xi
x xi+1 , yj y yj+1 }. Assume further that the domain D can be approximated by I Rij ,
where I Nn Nm . For example, D might be a rectangle D = {(x, y) : a x b, c y d}
and the grid lines xi = a + b(i 1)/n, yj = c + b(j 1)/m, i Nn , j Nm . Although it would
be possible the subdivide each Rij into two triangles, it is natural to attempt to use the rectangular
grid itself. Let Re be a rectangle in the grid, and consider interpolating a function u defined on
Re using the values of u = uj at the corners zje , j = 1, 2, 3, 4 of this rectangle. Lets label the
corners of Re so that z1e = (ae , be ) is the lower left, z2e = (ce , be ) the lower right, z3e = (ce , de ) the
upper right, and z4e = (ae , de ) the upper left (so ae < ce , be < de ). The interpolation is easily
accomplished if we define bilinear function Hje (x, y) such that Hie (zje ) = ij = 1, i = j, 0 otherwise.
If hex = (ce ae ), hey = (de be ) denote the side lengths of Re , then
e

## (c x)(de y)/(hex hey )

H1e
e
e
e e
e
H (x a )(d y)/(hx hy )
H e = 2e =

H3
(x ae )(y be )/(hex hey )
e
e
e
e e
H4
(c x)(y b )/(hx hy )
P4
Using these functions we can write u = j=1 Hje uj = H e T U, where U = [u1 , . . . , u4 ]T . It is easy to
see that this representation is unique. (If there were two representations then the difference would
be a bilinear function f vanishing at all zje , and hence would be identically zero. Since f restricted
to the line through z1e , z2e is a linear function of x vanishing at x = ae and f restricted to the line
through z2e , z3e is a linear function of y vanishing at y = be , we must have f = (x ae )(y be ).
Since f (z3e ) = 0, hex hey = 0 = 0.)

The assembly process using rectangular elements is basically the same as that using triangles. Of
course, the number of nodes per element is now 4, and the element stiffness and load matrices are
4 by 4 and 4 by 1 respectively.
Lets now consider an example from our textbook using bilinear elements.
function ex592a(nx,ny)
%---------------------------------------------------------------------------% Modified textbook file EX5.9.2.m
% to solve the two-dimensional Laplaces equation given as
%
u,xx + u,yy =0, 0 < x < 5, 0 < y < 10
%
u(x,0) = 0, u(x,10) = 100sin(pi*x/10),
%
u(0,y) = 0, u,x(5,y) = 0
% using bilinear rectangular elements
%(see Fig. 5.9.2 for the finite element mesh)
%
% Variable descriptions
%
k = element matrix
%
f = element vector
%
kk = system matrix
%
ff = system vector
1

%
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
%---------------------------------------------------------------------------%-----------------------------------% input data for control parameters
%-----------------------------------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
xl=0; xr=5;
yb=0; yt=10;
%--------------------------------------------% input data for nodal coordinate values
% gcoord(i,j) where i->node no. and j->x or y
% input data for nodal connectivity for each element
% nodes(i,j) where i-> element no. and j-> connected nodes
%--------------------------------------------------------[gcoord, nodes]=gridgen([0,0],[5,10],nx,ny,1)
gridplot(nx,ny,gcoord,nodes,1)
%------------------------------------% input data for boundary conditions
%------------------------------------bcdof=[1:nx1,... % base nodes
nx1+1:nx1:nx1*(ny1-2)+1,... % left side nodes
nx1*(ny1-1)+1:nx1*ny1]; % top nodes
sz=size(bcdof);
nbn=sz(2);
bcval=zeros(1,nbn); xx=linspace(xl,xr,nx1);
bcval(nbn-nx1+1:nbn)=100*sin(pi*(xx-xl)/10); % along y=yt, u(x,yt)=sin(pi*x/xr)

## %----------------------------------------% initialization of matrices and vectors

%----------------------------------------ff=zeros(sdof,1);

## % initialization of system force vector

kk=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

for i=1:nnel
nd(i)=nodes(iel,i);
x(i)=gcoord(nd(i),1);
y(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

xleng = x(2)-x(1);
% length of the element in x-axis
yleng = y(4)-y(1);
% length of the element in y-axis
index=feeldof(nd,nnel,ndof);% extract system dofs associated with element
k=felp2dr4(xleng,yleng);

## % compute element matrix

kk=feasmbl1(kk,k,index);

## % assemble element matrices

end
%----------------------------%
apply boundary conditions
%----------------------------[kk,ff]=feaplyc2(kk,ff,bcdof,bcval);
%---------------------------% solve the matrix equation
%---------------------------fsol=kk\ff;
%--------------------% analytical solution
%--------------------for i=1:nnode
x=gcoord(i,1); y=gcoord(i,2);
esol(i)=100*sinh(0.31415927*y)*sin(0.31415927*x)/sinh(3.1415927);
end
%-----------------------------------% print both exact and fem solutions

%-----------------------------------num=1:1:sdof;
store=[num fsol esol]

%===============================================================================
function [k]=felp2dr4(xleng,yleng)
%------------------------------------------------------------------% Purpose:
%
element matrix for two-dimensional Laplaces 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 [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 [k]=felp2dr4(xleng,yleng)
%------------------------------------------------------------------% Purpose:
%
element matrix for two-dimensional Laplaces 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 [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 [grd, connec]=gridgen(low_left, up_right, nx, ny, ind)
%
% FUNCTION [GRD, CONNEC]=GRIDGEN(LOW_LEFT, UP_RIGHT, NX, NY, IND)
%
% Creates a uniform grid on the rectangle with lower left corner
% low_left=[a,b] and upper right corner up_right=[c,d] using nx
% intervals along the x-axis and ny intervals on the y-axis. If ind=1
% the coordinates of nx*ny rectangular elements are returned in grd, and
% the corresponding connectivity matrix is returned in connec. If ind=2
% each rectangle is divided into two triangles and connec gives the
% connectivity matrix.
%
if ~((ind==1)|(ind==2))
error( ind must be 1 (rectangles) or 2 (triangles));
return;
end
ny1=ny+1; nx1=nx+1;
x=linspace(low_left(1),up_right(1),nx1);
y=linspace(low_left(2),up_right(2),ny1);
[xx,yy]=meshgrid(x,y); % arrays xx, yy are ny1 by nx1, xx has rows x, yy columns y
grd=zeros(ny1*nx1,2);
for row=1:ny1 %
for col=1:nx1
node_num=(row-1)*nx1+col;
grd(node_num,:)=[xx(row,col),yy(row,col)];
end
end
conq=zeros(nx*ny,4);
for row=1:ny % nodes for rectangular elements
for col=1:nx
nel=nx*(row-1)+col;
n0=nx1*(row-1)+col; % node at lower left of element nel
conq(nel,1:4)=[n0,n0+1,n0+1+nx1,n0+nx1]; % counter clockwise numbering
end
end
if ind==1
connec=conq;
else
for kk=1:nx*ny % divide each of the nx*ny rectangles into 2 triangles
4 3
cont(2*kk-1,1:3)=[conq(kk,4),conq(kk,1),conq(kk,3)]; % local nodes 4,1,3 ----

## cont(2*kk,1:3)=[conq(kk,2), conq(kk,3), conq(kk,1)]; % local nodes 2,3,1

end
%
connec=cont;
end
%=========================================================================

function gridplot(nx,ny,grd,connec,ind)
close all
nx1=nx+1; ny1=ny+1;
x=grd(1:nx1,1); y=grd(1:nx1:nx1*ny1-nx1+1,2);
hx=x(2)-x(1); hy=y(2)-y(1); % grid spacings
hold on
axis([-hx+x(1), x(nx1)+hx, -2*hy+y(1), y(ny1)+hy]);
for kk=1:ny1 plot(x,y(kk)*ones(1,nx1)); end % plot horizontal grid lines
for kk=1:nx1 plot(x(kk)*ones(1,ny1),y); end % plot vertical grid lines
ofx=0.1*hx; ofy=0.2*hy; % offsets
for kk=1:nx1*ny1
str=sprintf(%d,kk);
text(grd(kk,1)+ofx,grd(kk,2)+ofy,str,Color,[1,0,0],FontSize,8);
end
if ind==2 % complete triangles
for kk=1:2:length(connec(:,1))
x1=[grd(connec(kk,2),1),grd(connec(kk,3),1)];
y1=[grd(connec(kk,2),2),grd(connec(kk,3),2)];
plot(x1,y1,b-);
end
end
if ind==1
for kk=1:length(connec(:,1))
xg=grd(connec(kk,1),1)+hx/2; yg=grd(connec(kk,1),2)+hy/2;
str=sprintf(%d,kk);
text(xg,yg,str);
end
else
for kk=1:2:length(connec(:,1))
xg=grd(connec(kk,1),1)+hx/3; yg=grd(connec(kk,1),2)-hy/3;
str=sprintf(%d,kk);
text(xg,yg,str,FontSize,8);
end
for kk=2:2:length(connec(:,1))
xg=grd(connec(kk,3),1)+2*hx/3; yg=grd(connec(kk,3),2)+hy/3;
str=sprintf(%d,kk);
text(xg,yg,str,FontSize,8);
end
end
text(x(1),y(1)-2*hy/3,element numbers,FontSize,8);
text(x(1),y(1)-4*hy/3,node numbers,Color,[1,0,0],FontSize,8);

---1 2

hold off