Documente Academic
Documente Profesional
Documente Cultură
0<x<1
u(0, t) = a(t),
and the initial condition
u(x, 0) = c(x),
where a and b are given functions on [0, ) and c is given on [0,1] (for consistency, we require a(0) = u(0, 0) =
c(0) and c0 (1) = u/x(1, 0) = b(0)). For the finite element solution we select n linear elements with global
nodes at xk = (k 1)/n, k = 1, . . . , n + 1 (element e is the interval [xe , xe+1 ]). We elect to obtain the
weak form for this problem using a modification of our established procedure. For this method (prefered
by the authors of our textbook), we focus on and individual element (rather then the entire domain on the
problem). We obtain the weak form for this element and then assemble these element weak forms to obtain
the global weak form. As usual, we ignore the requirement that u(0, t) = a(t) in the initial stages of the
derivation, and impose this condition later. The element weak form is found as follows: We form the residual
r=
u 2 u
g,
t
x2
multiply it by the test function w(x) which depends only on x (w must vanish at x = 0 where u is prescribed,
but we ignore this for the moment and impose it later). Then we integrate the product rw over an element
I = [a, b] and require the result to vanish (we
R omit an identifying subscript or superscript on the interval
all elements are treated in the same way): I r(x, t)w(x) dx = 0. Using the identity
w
u
u w
2u
=
(w )
x2
x x
x x
This leads to the element weak form of the problem. We require that the numerical trial function u satisfy
b
Z
Z
Z
u w
u
u
dx +
dx = wg dx + w
w
t
x a
I x x
I
I
for all test functions w. (We must still impose the conditions that the trial function satisfy u(0, t) = a(t)
and take the initial condition u = c at t = 0 and that all test functions must vanish at x = 0.) It is easy to
see that for smooth functions the weak form is equivalent to the original problem.
The weak form is our starting point for finding an approximate solution (which well call the trial function) to
the differential equation. After dividing the interval [0, 1] into elements and requiring that the trial function be
linear on each element and continuous throughout the interval, we see that in each element the trial function
can be expressed in term of the linear shape functions H1 (x) = (b x)/(b a), H2 (x) = (x a)/(b a)
as u = u1 (t)H1 (x) + u2 (t)H2 (x) We take the test function to be arbitrary linear combination of the Hj .
Vanishing of the weak form is then equivalent to the demand that the weak form be satisfied for w = Hj .
Substituting for u and w gives:
2
X
j=1
mij u j +
2
X
kij uj = fi ,
j=1
i = 1, 2
where fi =
b
gHi dx + Hi u
x a
Z
I
H1
H2
g(x, t) dx +
u/x(a, t)
u/x(b, t)
dH2
dx
) dx
,
e,ne
H2e
b(t)
Ie
that is, only in the final element is there a contribution from the flux terms. I think this is probably an
acceptable fiction as long as the true state of affairs is kept in mind.
For 1D linear shape functions (see pp. 38-39 of the text) we have:
h 2
1 1 1
M [mij ] =
K [kij ] =
h 1 1
6 1
1
2
It is worthwhile looking at the computation of the mass matrix. In integrating over element of the form
I = [a, b] of length h = b a, it is useful to transform the variable of integration setting r = (x a)/h, and
integrate over the unit interval [0, 1]. On this interval the basis functions become: H1 (r) = 1 r, H2 (r) = r,
so that the integral to be computed becomes
Z 1
(1 r)2 r(1 r)
M =h
dr
r(1 r)
r2
0
It is now easy to perform the required integration and verify the results.
The global equations which are assembled, as usual, from the element equations take the form
M U + KU = F.
This is essentially a system of ODEs for the values uj (t) of the solution at the nodes. I say essentially because
all given boundary value information has to be input in order to create a legitimate system. (In this case,
it is required that u1 (t) = a(t). This can be done simply by replacing the first equation with u 1 = a and
tacking on the initial condition u1 (0) = a(0).)
Generally, the numerical solution of the system of ODEs just derived in accomplished using finite difference
methods not finite element methods. For these methods, we pick a sequence of times tj , j = 1, . . . , where
t1 = 0, and attempt to predict U (t) at these times. Let U j = U (tj ), then by the mean value theorem for
derivatives asserts that U (tj + (tj+1 tj )) = (U j+1 U j )/(tj+1 tj ), for some , 0 < < 1. Letting
j = tj+1 tj we have
M (U j+1 U j ) + j KU (tj + j ) = j F (tj + j )
So far this equation is exact, but we dont actually know the value of . So we choose a fixed value, and
each such choice creates a new numerical method. (Once is picked, the U j will represent a numerical
approximation to the original U , but I will continue to ignore this sort of thing.) The standard ones are:
1 The explicit forward difference method is obtained by choosing = 0, and can be written as
M U j+1 = (M j K)U j + j F j
Note that this determines U j for all j = 1, 2, . . .. It is explicit because it determines U at each new time
level explicitly in terms of U at the previous level. It is a forward difference method since the derivative
at level j is represented using the difference between U at this level and U at the next (forward) level.
2 The backward difference or implicit method is obtained using = 1. This gives
(M + j K)U j+1 = M U j + j F j+1
Thus, U at the new level us obtained only by solving a system of equations, i.e., the difference formula
only defines U at the new level implicitly. The backward difference designation obviously refers to the
fact the derivative is approximated at the new level using U at the previous (backward) level. Since,
our problem is linear we can actually solve for U j+1 to obtain
U j+1 = (M + j K)1 (M U j + j F j+1 )
3 The Crank-Nicholson method is obtained using = 1/2, and using the additional approximations
1
1
U ( (tj + tj+1 )) = (U j+1 + U j ),
2
2
1
1
F ( (tj + tj+1 )) = (F j+1 + F j ).
2
2
% dt
= time increment
%
sdof=n+1;
xvec=linspace(0,1,n+1); % define the node points
h=1/n;
nodes=[[1:n] [2:n+1]]; % configuration matrix
mm=zeros(sdof,sdof); kk=zeros(sdof,sdof); ff=zeros(sdof,1);
ke=elstif(h);
me=elmass(h);
for iel=1:n % assembly loop all elements have same mass and stiffness
for ii=1:2 % assemble mass and stiffness
gn1=nodes(iel,ii);
for jj=1:2
gn2=nodes(iel,jj);
mm(gn1,gn2)=mm(gn1,gn2)+me(ii,jj);
kk(gn1,gn2)=kk(gn1,gn2)+ke(ii,jj);
end
end
end
uold=init_val(xvec); % set inital conditions
effrc=zeros(sdof,1);
t=0;
esol=exac(xvec,0);
close all
hold on
plot(xvec, uold, xvec,esol,o); % plot initial condition
for k=1:nstep % use implicit method
ff(sdof)=1.0; % forcing at time t
effrc=mm*uold+dt*ff; % effective forcing
aa=mm+dt*kk;
aa(1,:)=0; aa(1,1)=1; effrc(1)=0; % impose bc at x=0 and time t
fsol=aa\effrc;
esol=exac(xvec,t+dt);
plot(xvec, fsol, xvec, esol, o); % plot solution at new time, t=t+dt
uold=fsol; t=t+dt;
end
legend(fem soln, exact soln,4)
hold off
%============================================
% end of main program
%============================================
function me=elmass(h)
% element mass matrix
me=(h/6)*[2 1; 1 2];
%============================================
function ke=elstif(h)
% element stiffness matrix
ke=(1/h)*[1 -1; -1 1];
%============================================
function h=init_val(x)
h=sin(pi*x./2)+x;
%============================================
function u=exac(x,t)
u=x+exp(-pi^2*t/4)*sin(pi*x/2)
(x, y) D, t > 0.
u = a(x, y, t) on Du ,
(x, y) D,
where, again, c is given. (Again, the usual consistency requirements need to be imposed, e.g., c(x, y)
a(x0 , y0 , 0), as (x, y) (x0 , y0 ) Du .) The weak form is derived just as in the 1D case. We define the
residual
u 2 u 2 u
2 g(x, y, t),
r=
t
x2
y
multiply r by a test function w(x, y) which must vanish on Du (note that w is independent of time t), and
then integrate over D. Using the divergence theorem to integrate by parts just as in the stationary case, we
find
Z
Z
Z
u w u w u
u
wr dA =
(w
+
+
wg) dA
ds.
w
t
x x
y y
n
Dq
D
D
This leads to the weak form. That is, we require that u take the given values on Du , satisfy
Z
Z
Z
u w u w u
(w
+
+
) dA =
wg dA +
wb ds
t
x x
y y
D
D
Dq
for all test functions w vanishing on Du , and take the initial condition u = c at time 0. It is easy to see
that for smooth functions the weak form is equivalent to the original problem.
Now we introduce a division of D into finite elements linear triangles, bilinear rectangles, etc. Ill only
consider linear triangles here but other elements can be handled in a similar manner. We specify that the
trial function be linear on P
each triangular element and continuous throughout the domain. We can then
n+1
write it in the form u =
j=1 j (x, y)uj (t) where the global shape functions, j satisfy the conditions
i (xj , yj ) = ij . We require that the trial solution u satisfy the Dirichlet boundary condition on Du so
that uj (t) = a(xj , yj , t) for nodes on this boundary. We demand that the weak form be satisfied for any
test function w from the set {k , k = 1, . . . , n + 1} ( j corresponding to boundary nodes j on Du should
not be included since w must vanish there, but as usual we ignore this restriction and impose the proper
conditions at the end). Substituting for u and w gives a system of linear equations:
n+1
X
j=1
mij u j +
n+1
X
kij uj = fi ,
i = 1, . . . , n + 1
j=1
Rather than go into the details of these global matrices here, I will focus immediately on a single element
since the global matrices can be assembled from the lower dimensional element matrices. We assume the
element number e is fixed, and therefore to simplify notation do not explicitly indicate it in our formulas.
Letting the element shape functions be denoted by Hj , j = 1, 3 as usual, we can write for an element
Z
H1
M = H2 ( H1 H2 H3 ) dA,
I
H3
5
K=
Z
I
and
H1
x
H2
x
H3
x
( H1
H2
x
H3
x
H1
)+
y
H2
y
H3
y
H1
y
H2
y
H3
y
dA,
Z
H1
H1
H2 b ds
F = H2 g dA +
ID
I
q
H3
H3
Z
Z 1r
Z 1
2 1 1
1rs
(1 r s r s) = A 1 2 1.
r
ds
dr
M = 2A
12
0
0
1 1 2
s
The corresponding global equations are of the form
M U + KU = F.
This is essentially a system of ODEs for the values uj (t) of the solution at the nodes. As in the 1D case,
all given boundary value information has to be input in order to create a legitimate system. (In this case,
it is required that uj (t) = a(xj , yj , t) whenever node j is on Du . This can be imposed by substituting
u j = a(x
j , yj , t) for the jth equation, and adding the boundary condition uj (0) = a(xj , yj , 0) = c(xj , yj ).)
The remaining considerations involving integrating these equations forward in time are essentially the same
as in the 1D case. Lets look at a slightly altered version of the file ex5111.m from the text which illustrates
the 2D case. The explicit method of time integration is chosen in this file. The numerical solution could be
compared to the exact as computed using the separation of variables technique, but the authors have not
done this. It is easy to show that the exact solution at the center node is given by
!
X
2n
(1)n1
exp( 2 t) ,
u = 100 1 2
n
`
n=1
where n = (2n 1)/2, ` = 2.5. Thus I have included a computation of this value in the file below. Clearly
there are some inaccuracies at the early stages of the computation.
%---------------------------------------------------------------------------%---------------------------------------------------------------------------function ex5111_alt(nx,ny)
% to solve the transient two-dimensional Laplaces equation
%
u,t = u,xx + u,yy , 0 < x < 5, 0 < y < 2
6
% boundary conditions:
%
u(0,y,t) = 100, u(5,y,t) = 100,
%
u,y(x,0,t) = 0, u,y(x,2,t) = 0
% initial condition:
%
u(x,y,0) = 0 over the domain
% using linear triangular elements and forward difference method
%
% Variable descriptions
%
k = element matrix for time-independent term (u,xx + u,yy)
%
m = element matrix for time-dependent term (u,t)
%
f = element vector
%
kk = system matrix of k
%
mm = system matrix of m
%
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
%---------------------------------------------------------------------------nx1=nx+1; ny1=ny+1;
nel=2*nx*ny;
% number of elements
nnel=3;
% 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
deltt=0.1;
% time step size for transient analysis
stime=0.0;
% initial time
ftime=10;
% 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
%--------------------------------------------[gcoord, nodes]=gridgen([0,0],[5,2],nx,ny,2);
gridplot(nx,ny,gcoord,nodes,2)
input(Strike any key to continue solution);
bcdof=[1:nx1:nnode-nx, nx1:nx1:nnode ]
lbc=length(bcdof);
bcval=100*ones(1,lbc);
fn=zeros(sdof,1);
%
fsol=zeros(sdof,1);
%
sol=zeros(2,ntime+1);
%
kk=zeros(sdof,sdof);
%
mm=zeros(sdof,sdof);
%
index=zeros(nnel*ndof,1);
initialization of
solution vector
vector containing
initialization of
initialization of
% initialization
nd(1)=nodes(iel,1);
nd(2)=nodes(iel,2);
nd(3)=nodes(iel,3);
x1=gcoord(nd(1),1);
x2=gcoord(nd(2),1);
x3=gcoord(nd(3),1);
for (iel)-th
for (iel)-th
for (iel)-th
coord values
coord values
coord values
element
element
element
of 1st node
of 2nd node
of 3rd node
end
%----------------------------%
loop for time integration
%----------------------------for in=1:sdof
fsol(in)=0.0;
end
% initial condition
sol(1,1)=fsol(8);
sol(2,1)=fsol(9);
for it=1:ntime
fn=deltt*ff+(mm-deltt*kk)*fsol;
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]=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 [m]=felpt2t3(x1,y1,x2,y2,x3,y3)
%------------------------------------------------------------------% Purpose:
%
element matrix for transient term of two-dimensional
%
Laplaces equation using linear triangular element
%
% Synopsis:
%
[m]=felpt2t3(x1,y1,x2,y2,x3,y3)
%
% Variable Description:
%
m - 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 triangle
m = (A/12)* [ 2
1
1
1
2
1
1;
1;
2 ];
function [kk]=feasmbl1(kk,k,index)
%---------------------------------------------------------% Purpose:
%
Assembly of element matrices into the system matrix
%
% Synopsis:
10
%
[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 u=cntr(t,n,lx)
11
12