Вы находитесь на странице: 1из 10

ME 471 Problem Set 4

Solutions
Text 5.9) This problem deals with the two dimensional steady state heat conduction equation





u
L(u) + Q = 0, with L(u) =
kx
+
ky
.
x
x
y
y
We are required to find the element weak form using linear triangular elements. In order to do the
required integration by parts we need the following identity




u

u
v u
v u

+ ky
+ vL(u),
vkx
+
vky
= kx
x
x
y
y
x x
y y
or with ~q = (kx u/x, ky u/y), (v~q) = v ~q + vL(u). If u is a trial function, and r = L(u) + Q
is the residual, we multiply r by a test function v and integrate over the triangular element D to
obtain
Z
Z
Z
vr dA =
( (v~q) v ~q) dA +
vQ dA = 0
D
D
D
R
R
Using the divergence theorem we have D (v~q) dA = D v~q ~n ds, and we find the element weak
form

Z
Z
Z
Z 
v u
v u
+ ky
dA =
vQ dA +
v~q ~n ds
v ~q dA =
kx
x x
y y
D
D
D
D
P3
Using linear triangles u = j=1 Hj uj and v = Hi , i = 1, 2, 3. Thus, the weak form becomes
3
X
j=1

kij uj = fi ,

with fi =

Z
D

Hi Q dA +

Hi ~q ~n ds,

and


Z 
Hi Hj
Hi Hj
+ ky
dA.
kij =
kx
x x
y y
D
Since the Hi are linear functions of x, y their derivatives are constants and the integrals defining kij
can be evaluated explicitly in terms of the coordinates of the three points, Pi = (xi , yi ), i = 1, 2, 3
defining the triangle D. Using the expressions for these functions given on p.89 of our textbook, it
is easy to see that, for example, k11 = (kx (y2 y3 )2 + ky (x2 x3 )2 )/4A (where A is the area of
D). (See formulas 5.2.155.2.23 in the textbook, Rand make appropriate modifications.) Note that
the
R load vector contains the computable portion D Hi Q dA as well as the boundary integral term
Hi ~q ~n ds which can only be computed over those portions of D on which the flux ~q is given.
D
The remaining portions of this integral are eliminated when the elements are assembled.
Text 5.11) The Matlab file ex592.m provided with the text can easily be modified to solve this
problem. Rather than retype the nodal coordinate matrix, you can use the file gridgen.m provided
on the course web site to generate both this matrix and the connectivity matrix. In addition to the
changes in geometry, the boundary conditions must be modified to solve this problem since essential
boundary conditions are given over the entire boundary of the domain. The exact solution of this
problem can be expressed in the form u = 200u1 (x, y) + 100u1 (y, x), where u1 (x, y) satisfies
u1 = 0, (x, y) S, u1 = 1 on R, u1 = 0 on S R,
where S = {(x, y) : 0 < x, y < 1}, R = {(x, y) : x P
= 1, 0 y 1}. u1 (x, y) may be expressed as the

infinite series (use separation of variables) u1 = n=1 cn sin(ny) sinh(nx), where the coefficients
are determined by the condition u1 (1, y) = 1, i.e., cn = 4/(n sinh(n)), n odd, cn = 0, n even. A
complete solution is provided in the file below. Note that since the boundary data is discontinuous
at the corners (1, 0), (1, 1), (0, 1), average values have been used. This provides a slightly more
accurate FEM solution.

function laplace511()
% to solve the two-dimensional Laplaces equation given as
%
u,xx + u,yy =0, 0 < x < 1, 0 < y < 1
%
u(x,0) = 0, u(x,1) = 200
%
u(0,y) = 0, u(1,y) = 100
% 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
%-----------------------------------clear
nel=16;
nnel=4;
ndof=1;
nnode=25;
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([0,0],[1,1],4,4,1);
gridplot(4,4,gcoord, nodes, 1);
%------------------------------------% input data for boundary conditions
%------------------------------------% index
1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
% bdry nodes:
1 2 3 4 5 6 11 16 10 15 20 21 22 23 24 25
% value specified: 0 0 0 0 50 0 0 0 100 100 100 100 200 200 200 150
% Note: values at corner nodes 5,21,25 are averages
bcdof=[1:5,6,11,16,10,15,20,21:25]; lbc=length(bcdof);
bcval=zeros(1,lbc);
bcval(5)=50; bcval(9:11)=100; bcval(12)=100;
bcval(13:15)=200; bcval(16)=150;

%----------------------------------------% 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;
% Note: The exact solution was not required. It is provided here
% so you can compare with the fem solution.
uexac=zeros(sdof,1);
for ii=1:sdof
xx=gcoord(ii,1); yy=gcoord(ii,2);
uexac(ii)=200*uf(10,xx,yy)+100*uf(10,yy,xx);
end
fprintf(1,%10s%10s%10s%10s%10s\n,node,x,y,approx,exact);

num=1:sdof;
disp([num gcoord(:,1), gcoord(:,2),fsol, uexac]);
%--------------------------------------------------------------% end of main program
%--------------------------------------------------------------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 u=uf(n,r,s)
% Exact separation of variables solution to u,rr+u,ss=0 in the
% unit square S subject to conditions u(0,s)=u(1,s)=u(r,0)=0, u(r,1)=1
% n terms are used in the series.
nn=1:n;
cn=(1-cos(pi*nn))./(pi*nn.*sinh(pi*nn));
sn=2*sin(pi*r*nn); sh=sinh(pi*s*nn);
u=sum(cn.*(sn.*sh));
Text 5.12) We are required to solve the Laplace equation u = 0 in L-shaped domain with u
prescribed on the boundary of the entire domain (u takes constant values of each subinterval of the
boundary).
function laplace512()
% to solve the two-dimensional Laplaces equation
% in L shaped domain shown in Fig.P5.12
% Boundary conditions as given there.
% Use 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=36;
nnel=4;
ndof=1;
nnode=52;
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=zeros(52,2); nodes=zeros(36,4);
[gcoord(1:40,:), nodes(1:27,:)]=gridgen([0,0],[30,10],9,3,1);
[gc,nd]=gridgen([0,10],[10,20],3,3,1);
gcoord(41:52,:)=gc(5:16,:);
nodes(31:36,:)=nd(4:9,:)+36*ones(6,4);
nodes(28,:)=[31,32,42,41];
nodes(29,:)=[32,33,43,42];
nodes(30,:)=[33,34,44,43];
%------------------------------------% input data for boundary conditions
%------------------------------------bcdof=[1:10,11,21,31,41,45,49,44,48,52,34:40]; nbc=length(bcdof);
bcval=zeros(1,nbc);
bcval(1)=30; bcval(2:10)=40; bcval(11:16)=20; bcval(17:nbc)=300;
%bcdof
%bcval
%return;
%----------------------------------------% 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;
%-----------------------------------% print both exact and fem solutions
%-----------------------------------fprintf(1,%10s%10s%10s%10s\n,node,x,y,approx);
num=1:1:sdof;
disp([num gcoord(:,1), gcoord(:,2),fsol]);
%--------------------------------------------------------------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);

Вам также может понравиться