You are on page 1of 6

Sample 2D Potential Theory Problem

We look in detail at the solution of the following boundary value problem (Textbook ex5.9.1): Let D
its closure, and D = {(x, y) : x = 0, 5, 0
be the rectangle D = {(x, y) : 0 < x < 5, 0 < y < 10}, D
R, u V = C(D)
C 2 (D)
y = 10, y = 0, 10, 0 x 5} its boundary. Find the function u : D

(i.e., u continuous on D with continuous second partial derivatives in D) satisfying the differential
equation
2u 2u
+ 2 = 0 (x, y) D,
x2
y
and the following boundary conditions: On the portion D1 of D, u is required to satisfy: u(x, 0) =
0, u(x, 10) = 100 sin(x/10), 0 x 5, and on the portion D2 of the boundary u(0, y) =
0, u(5, y)/x = 0, 0 y 10. (Of course, D = D1 D2 . )
Let V = {u V : u satisfies the given boundary conditions on D1 }, and V0 = {u V : u =
0 on D1 }. The weak form of this problem is: Find the function u V such that
Z
(v u) dA = 0
D

PN
for all v V0 . Using (linear) interpolation on triangular elements, we take u = j=1 j uj , where
j is the tent function which takes value 1 at node j and drops to zero at all adjacent nodes in the
triangularization of D, uj is the value of the solution at node j (uj are given at nodes on D1 , and
must be determined at all other nodes), and N is the number of nodes in the triangularization of D.
The admissible test functions v are those tent functions i corresponding to nodes i which do not
lie on D1 . Thus, if N2 is the number of nodes not on D1 , there are N2 unknown nodal values uj ,
and N2 equations obtained by selecting the admissible i .
It is necessary
to use integration by parts in forming the weak formulation, because the trial function
P
u = j uj j and the various test functions i that we choose in our implementation of the finite
element method do not really belong to the required subspaces of V (V and V0 respectively). In order
that the weak form make sense for our chosen method of interpolation, we cannot allow derivatives
higher than the first order. Just as in the 1D case, the justification for relaxation of the original
smoothness requirements is beyond the scope of our course.
function ex591a(nx,ny)
%---------------------------------------------------------------------------% modified version of EX5.9.1.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, (essential bcs)
%
u,x(5,y) = 0 (natural bc -- can ignore since specified value is 0)
% using linear triangular 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
1

%
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=2*nx*ny;
% number of elements
nnel=3;
% number of nodes per element
ndof=1;
% number of dofs per node
nx1=nx+1; ny1=ny+1;
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
% and data for nodal connectivity for each element
% nodes(i,j) where i-> element no. and j-> connected nodes
%--------------------------------------------[gcoord, nodes]= gridgen([xl,yb], [xr,yt],nx,ny,2);
gridplot(nx,ny,gcoord,nodes,2);
%------------------------------------% input data for boundary conditions
%------------------------------------bcdof=[1:nx1,...
nx1+1:nx1:nx1*(ny1-2)+1,...
nx1*(ny1-1)+1:nx1*ny1];
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);
kk=zeros(sdof,sdof);

% initialization of system force vector


% 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=nodes(iel,:); % 1 by 3 matrix of global node numbers
ndpts=gcoord(nd,:); % coordinates of element nodes
x=ndpts(:,1); y=ndpts(:,2);
index=feeldof(nd,nnel,ndof);% extract system dofs associated with element
k=felp2dt3(x(1),y(1),x(2),y(2),x(3),y(3)); % 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
%--------------------dx=xr-xl; dy=yt-yb;
for i=1:nnode
x=gcoord(i,1); y=gcoord(i,2);
esol(i)=100*sinh(pi*(y-yb)/dy)*sin(pi*(x-xl)/(2*dx))/sinh(pi);
end
%-----------------------------------% print both exact and fem solutions
%-----------------------------------num=1:sdof;
fprintf(%10s %10s %10s\n,node, fem soln, exact);
fprintf(%10.0f %10.3f %10.3f\n, [num; fsol; esol]);
%================================================================
% Textbook supplied functions
%================================================================
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]=felp2dt3(x1,y1,x2,y2,x3,y3)
%------------------------------------------------------------------% Purpose:
%
element matrix for two-dimensional Laplaces equation
%
using three-node linear triangular element
%
% Synopsis:
%
[k]=felp2dt3(x1,y1,x2,y2,x3,y3)
%
% Variable Description:
%
k - element stiffness matrix (size of 3x3)
%
x1, y1 - x and y coordinate values of the first node of element
%
x2, y2 - x and y coordinate values of the second node of element
%
x3, y3 - x and y coordinate values of the third node of element
%------------------------------------------------------------------% element matrix
A=0.5*(x2*y3+x1*y2+x3*y1-x2*y1-x1*y3-x3*y2); % area of the triangule
k(1,1)=((x3-x2)^2+(y2-y3)^2)/(4*A);
k(1,2)=((x3-x2)*(x1-x3)+(y2-y3)*(y3-y1))/(4*A);
k(1,3)=((x3-x2)*(x2-x1)+(y2-y3)*(y1-y2))/(4*A);

k(2,1)=k(1,2);
k(2,2)=((x1-x3)^2+(y3-y1)^2)/(4*A);
k(2,3)=((x1-x3)*(x2-x1)+(y3-y1)*(y1-y2))/(4*A);
k(3,1)=k(1,3);
k(3,2)=k(2,3);
k(3,3)=((x2-x1)^2+(y1-y2)^2)/(4*A);
%======================================================================
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
%=============================================================