You are on page 1of 7

Laplace Equation Problem with Boundary Flux Prescribed

In this lecture we study an example which illustrates the application of both essential and natural
boundary conditions. We let D be a rectangular domain and consider the boundary value problems
of the general form
u = f (x, y) D,

u = g1 , (x, y) D1 ,

u
= g2 (x, y) D2 ,
n

where D = D1 D2 , and the functions f : D R, and gj : Dj R, j = 1, 2 are given. It is


very important to note that the weak form for this equation is
Z
Z
Z
u
d`
w u dA =
wf dA +
w
n
D
D
D2
where w is the test function and u is the trial function. The weak form reveals that if u/n =
g2 (`) = 0 on D2 , then the natural boundary condition can be ignored. However, if g2 6= 0 on
RD2 then the element and global load vectors must be modified to account for the contribution of
wu/n d`. This is in complete analogy with the one dimensional case (in which D2 reduces to
D2
a single point). The weak form also reveals the form of the element stiffness and load matrices. Let
D be divided into elements, D = ne el Ee , so that the integral over D becomes the sum of integrals
over elements Ee , and the boundary integral becomes a sum of integrals over D2 Ee .
Z

w u dA =

e=1

and
Z

wf dA +

nel Z
X

D2

el
X
u
d` =
n
e=1

w u dA

Ee

wf dA +

D2 Ee

Ee

u
d`
n

Pnn

Recall that if we represent u using global shape functions u = j=1 j uj (nn =number of nodes)
Pnn
and take w = i , then w u = j=1
i j uj . Thus, we have for example
Z
Ee

w u dA =

nn Z
X
j=1

Ee

i |Ee j |Ee dA uj =

nn
X
j=1

K e [u1 , . . . , unn ]T

where the K e are nn nn matrices which have only n2 nonzero element, where n is the number of
nodes per element. By establishing a mapping between the local node numbers on each element and
the global node numbers in the problem we are able to focus on single elements and interpret each
K e as (a much smaller) n n element matrix. Similar remarks apply to the integrals appearing in
the load vector. In the discussion below, we assume the required mapping has been established and
focus on the element matrices. (In our programs, the array nodes gives the mapping when there is
only one degree of freedom (dof) per node. If there are several dof per node, then the matrix index
gives the required map.)
Let E denote any element with boundary E (the element number e is fixed forPthis discussion and
n
will not be indicated). Then on E the trial function can be written as u|E = j=1 Hj uj = H T U ,
with n the number of nodes per element (not the total number of nodes!), H = [H1 , . . . , Hn ]T ,
the element shape functions, and U = [u1 , . . . , un ]T the nodal values of the trial function on E (of

course, the numbering is in terms of local node numbers). Since w can be any of the n components
of H, we have can arrange the possible values of the integral over E into an n component vector as
follows:
Z


T
T
Hx Hx + Hy Hy dA U = KU
E

where K is the nn element stiffness matrix, Hx = [H1 /x, . . . , Hn /x]T , and Hy is the analogous
matrix of derivatives with respect to y. The element load vector can be treated in a similar way to
yield the element load vector
Z
Z
Hf dA +
Hg2 d` = F.
D2 E

The problem to be considered is adopted from our textbook Ex 5.9.1 but with the natural boundary
condition no longer homogeneous. Keeping the same notation for the geometry (so that D = {(x, y) :
R
Rd
a < x < c, b < y < d}), we must account for the term D2 wu/n d` = b wu/x(c, y) dy where
w is a test function. We need to see what happens when a test function w (which if we neglect the
constraints imposed by the essential boundary conditions, can take any of the values Hj , j = 1, . . . , 4
in an element) is evaluated on the x = c edge (the right hand edge) of the rectangle. If E is an
element such that E D2 = E2 6= , then it is easy to see that

0,
for j = 1, 4
Hj |E2 =
hj (y), for j = 2, 3
where h2 (y) = (y3 y)/y3 y2 , h3 (y) = (y y2 )/y3 y2 . Of course, these functions are just the
linear 1D shape functions that weve studied when considering ODEs. To evaluate the contribution
to the load vector from these elements, we only need to find the values of y2 , y3 for the element and
integrate the given function g2 (y) multiplied by hj (y) between these limits. You will see this done
in our example.
A bit of caution must be exercised when computing the contribution of the element flux to the load
vector. Namely, if an essential boundary condition is prescribed at one of the nodal points, say k, on
E2 , then the test function w must vanish there so that a 0 must be substituted for the component
Hk of H, or the component Fk must be set equal to 0. This can only happen if node k is an end
point of E2 because we know that u is not prescribed in the interior of the boundary element. (At
this point we have not looked at elements with interior points so k has to be an end point.)
Ry
We need to decide how to compute the integral I = y23 hj (y)g(y) dy. At this point, we havent
studied numerical integration so well adopt what seems like a reasonable method and worry about
accuracy later. We assume that g can
P3be accurately represented for y2 y y3 by its linear
interpolation g g2 h2 (y) + g3 h3 (y) = j=2 gj hj (y), where gj = g(yj ), j = 2, 3. In this case,
T

[I2 , I3 ] =

Z

y3

[h2 , h3 ] [h2 , h3 ] dy [g2 , g3 ] = B

y2

and the matrix B is easily found to be


B=

(y3 y2 )
6

function laplace_ex02(bl, ur, nx, ny)

2
1

1
2

g2
g3

% Input Parameters:
% bl=[a,b], ur=[c,d],
% nx=number of segments in [a,c], ny=number of segments in [b,d]
% Solve the two-dimensional Laplaces equation given as
%
u,xx + u,yy =0, a<x<c, b<y<d
%
u(x,b) = 0, u(x,d) = 0,
%
u(a,y) = 0, u,x(c,y) = 100sin(pi (y-b)/hy), hy=d-b
% using bilinear rectangular 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
%---------------------------------------------------------------------------%-----------------------------------% input data for control parameters
%-----------------------------------nel=nx*ny;
nnel=4;
ndof=1;
nx1=nx+1; ny1=ny+1;
nnode=nx1*ny1;
sdof=nnode*ndof;

% number of elements
% number of nodes per element
% number of dofs per node
% total number of nodes in system
% total system dofs

%--------------------------------------------% input data for nodal coordinate values


% gcoord(i,j) where i->node no. and j->x or y
%--------------------------------------------[gcoord, nodes]=gridgen(bl, ur, nx, ny, 1);
gridplot(nx, ny,gcoord, nodes, 1)
%------------------------------------% input data for boundary conditions
%------------------------------------nxtop=nnode-nx1+1; % node on top left corner
% list prescribed dofs (=bounndary nodes) in order: bottom, left, top
bcdof=[1:nx1, nx1+1:nx1:nxtop-nx1, nxtop:nnode];
bcval=zeros(1,length(bcdof));

%----------------------------------------% 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
%----------------------------% compute boundary contribution to load vector
%----------------------------% loop over elements on edge x=ur(1), bl(2)<=y<=ur(2)
% to compute contributions to load vector from flux
for elno=nx:nx:nx*ny
nd=nodes(elno,:); % global nodes of element number elno
y1=gcoord(nd(2),2); y2=gcoord(nd(3),2); % y coordinates of left edge
gv=bdryfnc([y1,y2],[bl(2),ur(2)]);
eflx=elflux(y1,y2,gv); % compute element contribution to load vector
ff(nd(2))=ff(nd(2))+eflx(1);
ff(nd(3))=ff(nd(3))+eflx(2);
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)-bl(1); y=gcoord(i,2)-bl(2);
hx=ur(1)-bl(1); hy=ur(2)-bl(2);
esol(i)=100*hy*sinh(pi*x/hy)*sin(pi*y/hy)/(pi*cosh(pi*hx/hy));
end
%-----------------------------------% print both exact and fem solutions
%-----------------------------------fprintf(1,%10s%10s%10s%10s%10s\n,node,x,y,approx,exact);
num=1:1:sdof;
fprintf(%10d%10.4f%10.4f%10.4f%10.4f\n, [num gcoord(:,1), gcoord(:,2),fsol esol]);

%--------------------------------------------------------------function [index]=feeldof(nd,nnel,ndof)
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 [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 [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 eflx=elflux(y1,y2, gv)
% Compute int_y1^y2 [h2,h3]g(y)dy, where h_j are the 1D linear
% shape functions, using linear interpolation. gv=[g(y1),g(y2)]^T
eflx=((y2-y1)/6)*[2 1;1 2]*gv;
%------------------------------------------------------------------function g=bdryfnc(y,dy)
hy=dy(2)-dy(1);
z=[(y(1)-dy(1))/hy; (y(2)-dy(1))/hy]
g=100*sin(pi*z);