The finite element method is a very flexible approach for solving partial differential equations. Its two most attractive features are the ease of handling domains of complex shape in two and three dimensions and the variety of polynomials (with different properties and orders) that are available. The latter feature typically leads to errors proportional to hd+1, where h is the element length and d is the polynomial degree. When the solution is sufficiently smooth, the ability to use larger d creates methods that are much more computationally efficient than standard finite difference methods (and equally efficient finite difference methods are technically much harder to construct).
However, before we attack finite element methods, with localized basis functions, it can be easier from a pedagogical point of view to study approximations by global functions because the mathematics in this case gets simpler.
The finite element method is usually applied for discretization in space, and therefore spatial problems will be our focus in the coming sections. Extensions to time-dependent problems usually employs finite difference approximations in time.
The coming sections address at how global basis functions and the least squares, Galerkin, and collocation principles can be used to solve differential equations.
Let us consider an abstract differential equation for a function u(x) of one variable, written as
Here are a few examples on possible choices of L(u), of increasing complexity:
Both α(x) and f(x) are considered as specified functions, while a is a prescribed parameter. Differential equations corresponding to (2)-(3) arise in diffusion phenomena, such as stationary (time-independent) transport of heat in solids and flow of viscous fluids between flat plates. The form (4) arises when transient diffusion or wave phenomena are discretized in time by finite differences. The equation (5) appears in chemical models when diffusion of a substance is combined with chemical reactions. Also in biology, (5) plays an important role, both for spreading of species and in models involving generation and propagation of electrical signals.
Let Ω=[0,L] be the domain in one space dimension. In addition to the differential equation, u must fulfill boundary conditions at the boundaries of the domain, x=0 and x=L. When L contains up to second-order derivatives, as in the examples above, we need one boundary condition at each of the (two) boundary points, here abstractly specified as
There are three common choices of boundary conditions:
Here, g and H are specified quantities.
From now on we shall use ue(x) as symbol for the exact solution, fulfilling
while u(x) is our notation for an approximate solution of the differential equation.
Remark on notation.
In the literature about the finite element method,
it is common to use u as the exact solution and uh as the
approximate solution, where h is a discretization parameter. However,
the vast part of the present text is about the approximate solutions,
and having a subscript h attached all the time
is cumbersome. Of equal importance is the close correspondence between
implementation and mathematics that we strive to achieve in this text:
when it is natural to use u
and not u_h
in
code, we let the mathematical notation be dictated by the code's
preferred notation. In the relatively few cases where we need to work
with the exact solution of the PDE problem we call it ue in
mathematics and u_e
in the code (the function for computing
u_e
is named u_exact
).
A common model problem used much in the forthcoming examples is
A closely related problem with a different boundary condition at x=0 reads
A third variant has a variable coefficient,
The solution u to the model problem (11) can be determined as
where c0 and c1 are determined by the boundary conditions such that u′(0)=C and u(L)=D.
Computing the solution is easily done
using sympy
. Some common code is defined first:
import sympy as sym
x, L, C, D, c_0, c_1, = sym.symbols('x L C D c_0 c_1')
The following function computes the solution symbolically for the model problem (11):
def model1(f, L, D):
"""Solve -u'' = f(x), u(0)=0, u(L)=D."""
# Integrate twice
u_x = - sym.integrate(f, (x, 0, x)) + c_0
u = sym.integrate(u_x, (x, 0, x)) + c_1
# Set up 2 equations from the 2 boundary conditions and solve
# with respect to the integration constants c_0, c_1
r = sym.solve([u.subs(x, 0)-0, # x=0 condition
u.subs(x,L)-D], # x=L condition
[c_0, c_1]) # unknowns
# Substitute the integration constants in the solution
u = u.subs(c_0, r[c_0]).subs(c_1, r[c_1])
u = sym.simplify(sym.expand(u))
return u
Calling model1(2, L, D)
results in the solution
The model problem (12) can be solved by
def model2(f, L, C, D):
"""Solve -u'' = f(x), u'(0)=C, u(L)=D."""
u_x = - sym.integrate(f, (x, 0, x)) + c_0
u = sym.integrate(u_x, (x, 0, x)) + c_1
r = sym.solve([sym.diff(u,x).subs(x, 0)-C, # x=0 cond.
u.subs(x,L)-D], # x=L cond.
[c_0, c_1])
u = u.subs(c_0, r[c_0]).subs(c_1, r[c_1])
u = sym.simplify(sym.expand(u))
return u
to yield
if f(x)=2. Model (13) requires a bit more involved code,
def model3(f, a, L, C, D):
"""Solve -(a*u')' = f(x), u(0)=C, u(L)=D."""
au_x = - sym.integrate(f, (x, 0, x)) + c_0
u = sym.integrate(au_x/a, (x, 0, x)) + c_1
r = sym.solve([u.subs(x, 0)-C,
u.subs(x,L)-D],
[c_0, c_1])
u = u.subs(c_0, r[c_0]).subs(c_1, r[c_1])
u = sym.simplify(sym.expand(u))
return u
def demo():
f = 2
u = model1(f, L, D)
print(('model1:', u, u.subs(x, 0), u.subs(x, L)))
print((sym.latex(u, mode='plain')))
u = model2(f, L, C, D)
#f = x
#u = model2(f, L, C, D)
print(('model2:', u, sym.diff(u, x).subs(x, 0), u.subs(x, L)))
print((sym.latex(u, mode='plain')))
u = model3(0, 1+x**2, L, C, D)
print(('model3:', u, u.subs(x, 0), u.subs(x, L)))
print((sym.latex(u, mode='plain')))
if __name__ == '__main__':
demo()
('model1:', x*(D + L*(L - x))/L, 0, D) \frac{x \left(D + L \left(L - x\right)\right)}{L} ('model2:', -C*L + C*x + D + L**2 - x**2, C, D) - C L + C x + D + L^{2} - x^{2} ('model3:', (C*atan(L) - C*atan(x) + D*atan(x))/atan(L), C, D) \frac{C \operatorname{atan}{\left(L \right)} - C \operatorname{atan}{\left(x \right)} + D \operatorname{atan}{\left(x \right)}}{\operatorname{atan}{\left(L \right)}}
With f(x)=0 and α(x)=1+x2 we get
The fundamental idea is to seek an approximate solution u in some space V,
which means that u can always be expressed as a linear combination of the basis functions {ψj}j∈Is, with Is as the index set {0,…,N}:
The coefficients {cj}j∈Is are unknowns to be computed.
(Later, we will see that if we specify boundary values of u different from zero, we must look for an approximate solution u(x)=B(x)+∑jcjψj(x), where ∑jcjψj∈V and B(x) is some function for incorporating the right boundary values. Because of B(x), u will not necessarily lie in V. This modification does not imply any difficulties.)
We need principles for deriving N+1 equations to determine the N+1 unknowns {ci}i∈Is. When approximating a given function f by u=∑jcjφj, a key idea is to minimize the square norm of the approximation error e=u−f or (equivalently) demand that e is orthogonal to V. Working with e is not so useful here since the approximation error in our case is e=ue−u and ue is unknown. The only general indicator we have on the quality of the approximate solution is to what degree u fulfills the differential equation. Inserting u=∑jcjψj into L(u) reveals that the result is not zero, because u in general is an approximation and not identical to ue. The nonzero result,
is called the residual and measures the error in fulfilling the governing equation.
Various principles for determining {cj}j∈Is try to minimize R in some sense. Note that R varies with x and the {cj}j∈Is parameters. We may write this dependence explicitly as
Below, we present three principles for making R small: a least squares method, a projection or Galerkin method, and a collocation or interpolation method.
The least-squares method aims to find {ci}i∈Is such that the square norm of the residual
is minimized. By introducing an inner product of two functions f and g on Ω as
the least-squares method can be defined as
Differentiating with respect to the free parameters \left\{ {c}_i \right\}_{i\in{\mathcal{I}_s}} gives the N+1 equations
The least-squares principle is equivalent to demanding the error to be orthogonal to the space V when approximating a function f by u\in V. With a differential equation we do not know the true error so we must instead require the residual R to be orthogonal to V. This idea implies seeking \left\{ {c}_i \right\}_{i\in{\mathcal{I}_s}} such that
This is the Galerkin method for differential equations.
The above abstract statement can be made concrete by choosing a concrete basis. For example, the statement is equivalent to R being orthogonal to the N+1 basis functions \{{\psi}_i\} spanning V (and this is the most convenient way to express (22):
resulting in N+1 equations for determining \left\{ {c}_i \right\}_{i\in{\mathcal{I}_s}}.
A generalization of the Galerkin method is to demand that R is orthogonal to some space W, but not necessarily the same space as V where we seek the unknown function. This generalization is called the method of weighted residuals:
If \{w_0,\ldots,w_N\} is a basis for W, we can equivalently express the method of weighted residuals as
The result is N+1 equations for \left\{ {c}_i \right\}_{i\in{\mathcal{I}_s}}.
The least-squares method can also be viewed as a weighted residual method with w_i = \partial R/\partial c_i.
Variational formulation of the continuous problem.
Statements like (22), (23), (24), or (25)) are known as weak formulations or variational formulations. These equations are in this text primarily used for a numerical approximation u\in V, where V is a finite-dimensional space with dimension N+1. However, we may also let the exact solution {u_{\small\mbox{e}}} fulfill a variational formulation (\mathcal{L}({u_{\small\mbox{e}}}),v)=0 \forall v\in V, but the exact solution lies in general in a space with infinite dimensions (because an infinite number of parameters are needed to specify the solution). The variational formulation for {u_{\small\mbox{e}}} in an infinite-dimensional space V is a mathematical way of stating the problem and acts as an alternative to the usual (strong) formulation of a differential equation with initial and/or boundary conditions.
Much of the literature on finite element methods takes a differential equation problem and first transforms it to a variational formulation in an infinite-dimensional space V, before searching for an approximate solution in a finite-dimensional subspace of V. However, we prefer the more intuitive approach with an approximate solution u in a finite-dimensional space V inserted in the differential equation, and then the resulting residual is demanded to be orthogonal to V.
Remark on terminology.
The terms weak or variational formulations often refer to a statement like (22) or (24) after integration by parts has been performed (the integration by parts technique is explained in the section Integration by parts). The result after integration by parts is what is obtained after taking the first variation of a minimization problem (see the section Variational problems and minimization of functionals). However, in this text we use variational formulation as a common term for formulations which, in contrast to the differential equation R=0, instead demand that an average of R is zero: (R,v)=0 for all v in some space.
In the context of the Galerkin method and the method of weighted residuals it is common to use the name trial function for the approximate u = \sum_j c_j {\psi}_j.
The space containing the trial function is known as the trial space. The function v entering the orthogonality requirement in the Galerkin method and the method of weighted residuals is called test function, and so are the {\psi}_i or w_i functions that are used as weights in the inner products with the residual. The space where the test functions comes from is naturally called the test space.
We see that in the method of weighted residuals the test and trial spaces are different and so are the test and trial functions. In the Galerkin method the test and trial spaces are the same (so far).
The idea of the collocation method is to demand that R vanishes at N+1 selected points x_{0},\ldots,x_{N} in \Omega:
The collocation method can also be viewed as a method of weighted residuals with Dirac delta functions as weighting functions. Let \delta (x-x_{i}) be the Dirac delta function centered around x=x_{i} with the properties that \delta (x-x_{i})=0 for x\neq x_{i} and
Intuitively, we may think of \delta (x-x_{i}) as a very peak-shaped function around x=x_{i} with an integral \int_{-\infty}^\infty \delta(x-x_{i})dx that evaluates to unity. Mathematically, it can be shown that \delta (x-x_{i}) is the limit of a Gaussian function centered at x=x_{i} with a standard deviation that approaches zero. Using this latter model, we can roughly visualize delta functions as done in Figure. Because of (27), we can let w_i=\delta(x-x_{i}) be weighting functions in the method of weighted residuals, and (25) becomes equivalent to (26).
Approximation of delta functions by narrow Gaussian functions.
The idea of this approach is to demand the integral of R to vanish over N+1 subdomains \Omega_i of \Omega:
This statement can also be expressed as a weighted residual method
Our choice of basis functions {\psi}_i for V is
An important property of these functions is that {\psi}_i(0)={\psi}_i(L)=0, which means that the boundary conditions on u are fulfilled:
Another nice property is that the chosen sine functions are orthogonal on \Omega:
provided i and j are integers.
We can readily calculate the following explicit expression for the residual:
The governing equations for the unknown parameters \left\{ {c}_j \right\}_{j\in{\mathcal{I}_s}} are then
which can be rearranged as
This is nothing but a linear system
The entries in the coefficient matrix are given by
The orthogonality of the sine functions simplify the coefficient matrix:
The right-hand side reads
Since the coefficient matrix is diagonal we can easily solve for
With the special choice of f(x)=2, the coefficients
can be calculated in sympy
by
import sympy as sym
i, j = sym.symbols('i j', integer=True)
x, L = sym.symbols('x L')
f = 2
a = 2*L/(sym.pi**2*(i+1)**2)
c_i = a*sym.integrate(f*sym.sin((i+1)*sym.pi*x/L), (x, 0, L))
c_i = sym.simplify(c_i)
print(c_i)
Piecewise((4*L**2*((-1)**i + 1)/(pi**3*(i + 1)**3), Ne(pi*(i + 1)/L, 0)), (0, True))
The answer becomes
Now, 1+(-1)^i=0 for i odd, so only the coefficients with even index are nonzero. Introducing i=2k for k=0,\ldots,N/2 to count the relevant indices (for N odd, k goes to (N-1)/2), we get the solution
The coefficients decay very fast: c_2 = c_0/27, c_4=c_0/125. The solution will therefore be dominated by the first term,
or
This equation can be rearranged to a form that explicitly shows that we get a linear system for the unknowns \left\{ {c}_j \right\}_{j\in{\mathcal{I}_s}}:
For the particular choice of the basis functions (31) we get in fact the same linear system as in the least squares method because {\psi}''= -(i+1)^2\pi^2L^{-2}{\psi}. Consequently, the solution u(x) becomes identical to the one produced by the least squares method.
For the collocation method (26) we need to decide upon a set of N+1 collocation points in \Omega. A simple choice is to use uniformly spaced points: x_{i}=i\Delta x, where \Delta x = L/N in our case (N\geq 1). However, these points lead to at least two rows in the matrix consisting of zeros (since {\psi}_i(x_{0})=0 and {\psi}_i(x_{N})=0), thereby making the matrix singular and non-invertible. This forces us to choose some other collocation points, e.g., random points or points uniformly distributed in the interior of \Omega. Demanding the residual to vanish at these points leads, in our model problem (30), to the equations
which is seen to be a linear system with entries
in the coefficient matrix and entries b_i=2 for the right-hand side (when f(x)=2).
The special case of N=0 can sometimes be of interest. A natural choice is then the midpoint x_{0}=L/2 of the domain, resulting in A_{0,0} = -{\psi}_0''(x_{0}) = \pi^2L^{-2}, f(x_0)=2, and hence c_0=2L^2/\pi^2.
In the present model problem, with f(x)=2, the exact solution is
u(x)=x(L-x), while for N=0 the Galerkin and least squares method
result in u(x)=8L^2\pi^{-3}\sin (\pi x/L) and the
collocation method leads to u(x)=2L^2\pi^{-2}\sin (\pi x/L).
We can quickly use sympy
to verify that the maximum error
occurs at the midpoint x=L/2 and find what the errors are.
First we set up the error expressions:
If the derivative of the errors vanish at x=L/2, the errors reach their maximum values here (the errors vanish at the boundary points).
x, L = sym.symbols('x L')
e_Galerkin = x*(L-x) - 8*L**2*sym.pi**(-3)*sym.sin(sym.pi*x/L)
dedx_Galerkin = sym.diff(e_Galerkin, x)
dedx_Galerkin.subs(x, L/2)
e_colloc = x*(L-x) - 2*L**2*sym.pi**(-2)*sym.sin(sym.pi*x/L)
dedx_colloc = sym.diff(e_colloc, x)
dedx_colloc.subs(x, L/2)
Finally, we can compute the maximum error at x=L/2 and evaluate the expressions numerically with three decimals:
sym.simplify(e_Galerkin.subs(x, L/2).evalf(n=3))
sym.simplify(e_colloc.subs(x, L/2).evalf(n=3))
The error in the collocation method is about 6 times larger than the error in the Galerkin or least squares method.
A problem arises if we want to apply popular finite element functions to solve our model problem (30) by the standard least squares, Galerkin, or collocation methods: the piecewise polynomials {\psi}_i(x) have discontinuous derivatives at the cell boundaries which makes it problematic to compute the second-order derivative. This fact actually makes the least squares and collocation methods less suitable for finite element approximation of the unknown function. (By rewriting the equation -u''=f as a system of two first-order equations, u'=v and -v'=f, the least squares method can be applied. Also, differentiating discontinuous functions can actually be handled by distribution theory in mathematics.) The Galerkin method and the method of weighted residuals can, however, be applied together with finite element basis functions if we use integration by parts as a means for transforming a second-order derivative to a first-order one.
Consider the model problem (30) and its Galerkin formulation
Using integration by parts in the Galerkin method, we can "move" a derivative of u onto v:
Usually, one integrates the problem at the stage where the u and v functions enter the formulation. Alternatively, but less common, we can integrate by parts in the expressions for the matrix entries:
Integration by parts serves to reduce the order of the derivatives and to make the coefficient matrix symmetric since ({\psi}_i',{\psi}_j') = ({\psi}_j',{\psi}_i'). The symmetry property depends on the type of terms that enter the differential equation. As will be seen later, integration by parts also provides a method for implementing boundary conditions involving u'.
With the choice (31) of basis functions we see that the "boundary terms" {\psi}_i(L){\psi}_j'(L) and {\psi}_i(0){\psi}_j'(0) vanish since {\psi}_i(0)={\psi}_i(L)=0.
We therefore end up with the following alternative Galerkin formulation:
Since the variational formulation after integration by parts make weaker demands on the differentiability of u and the basis functions {\psi}_i, the resulting integral formulation is referred to as a weak form of the differential equation problem. The original variational formulation with second-order derivatives, or the differential equation problem with second-order derivative, is then the strong form, with stronger requirements on the differentiability of the functions.
For differential equations with second-order derivatives, expressed as variational formulations and solved by finite element methods, we will always perform integration by parts to arrive at expressions involving only first-order derivatives.
So far we have assumed zero Dirichlet boundary conditions, typically u(0)=u(L)=0, and we have demanded that {\psi}_i(0)={\psi}_i(L)=0 for i\in{\mathcal{I}_s}. What about a boundary condition like u(L)=D\neq0? This condition immediately faces a problem: u = \sum_j c_j{\varphi}_j(L) = 0 since all {\varphi}_i(L)=0.
We remark that we faced exactly the same problem where we considered Fourier series approximations of functions that where non-zero at the boundaries. We will use the same trick as we did earlier to get around this problem.
A boundary condition of the form u(L)=D can be implemented by demanding that all {\psi}_i(L)=0, but adding a boundary function B(x) with the right boundary value, B(L)=D, to the expansion for u:
This u gets the right value at x=L:
The idea is that for any boundary where u is known we demand {\psi}_i to vanish and construct a function B(x) to attain the boundary value of u. There are no restrictions on how B(x) varies with x in the interior of the domain, so this variation needs to be constructed in some way. Exactly how we decide the variation to be, is not important.
For example, with u(0)=0 and u(L)=D, we can choose B(x)=x D/L, since this form ensures that B(x) fulfills the boundary conditions: B(0)=0 and B(L)=D. The unknown function is then sought on the form
with {\psi}_i(0)={\psi}_i(L)=0.
The particular shape of the B(x) function is not important as long as its boundary values are correct. For example, B(x)=D(x/L)^p for any power p will work fine in the above example. Another choice could be B(x)=D\sin (\pi x/(2L)).
As a more general example, consider a domain \Omega = [a,b] where the boundary conditions are u(a)=U_a and u(b)=U_b. A class of possible B(x) functions is
Real applications will most likely use the simplest version, p=1, but here such a p parameter was included to demonstrate that there are many choices of B(x) in a problem. Fortunately, there is a general, unique technique for constructing B(x) when we use finite element basis functions for V.
How to deal with nonzero Dirichlet conditions.
The general procedure of incorporating Dirichlet boundary conditions goes as follows. Let \partial\Omega_E be the part(s) of the boundary \partial\Omega of the domain \Omega where u is specified. Set {\psi}_i=0 at the points in \partial\Omega_E and seek u as
where B(x) equals the boundary conditions on u at \partial\Omega_E.
Remark. With the B(x) term, u does not in general lie in V=\hbox{span}\, \{{\psi}_0,\ldots,{\psi}_N\} anymore. Moreover, when a prescribed value of u at the boundary, say u(a)=U_a is different from zero, it does not make sense to say that u lies in a vector space, because this space does not obey the requirements of addition and scalar multiplication. For example, 2u does not lie in the space since its boundary value is 2U_a, which is incorrect. It only makes sense to split u in two parts, as done above, and have the unknown part \sum_j c_j {\psi}_j in a proper function space.
The next example uses global polynomials and shows that if our solution, modulo boundary conditions, lies in the space spanned by these polynomials, then the Galerkin method recovers the exact solution.
Let us perform the necessary calculations to solve
using a global polynomial basis {\psi}_i\sim x^i. The requirements on {\psi}_i is that {\psi}_i(1)=0, because u is specified at x=1, so a proper set of polynomial basis functions can be
A suitable B(x) function to handle the boundary condition u(1)=D is B(x)=Dx. The variational formulation becomes
From inserting u=B + \sum_{j}c_j{\psi}_j and choosing v={\psi}_i we get
The entries in the linear system are then
Relevant sympy
commands to help calculate these expressions are
from sympy import *
x, C, D = symbols('x C D')
i, j = symbols('i j', integer=True, positive=True)
psi_i = (1-x)**(i+1)
psi_j = psi_i.subs(i, j)
integrand = diff(psi_i, x)*diff(psi_j, x)
integrand = simplify(integrand)
A_ij = integrate(integrand, (x, 0, 1))
A_ij = simplify(A_ij)
print(('A_ij:', A_ij))
f = 2
b_i = integrate(f*psi_i, (x, 0, 1)) - \
integrate(diff(D*x, x)*diff(psi_i, x), (x, 0, 1)) - \
C*psi_i.subs(x, 0)
b_i = simplify(b_i)
print(('b_i:', b_i))
('A_ij:', (i + 1)*(j + 1)/(i + j + 1)) ('b_i:', ((-C + D)*(i + 2) + 2)/(i + 2))
The output becomes
A_ij: (i + 1)*(j + 1)/(i + j + 1)
b_i: ((-C + D)*(i + 2) + 2)/(i + 2)
We can now choose some N and form the linear system, say for N=1:
N = 1
A = zeros(N+1, N+1)
b = zeros(N+1)
print(('fresh b:', b))
for r in range(N+1):
for s in range(N+1):
A[r,s] = A_ij.subs(i, r).subs(j, s)
b[r,0] = b_i.subs(i, r)
('fresh b:', Matrix([ [0, 0], [0, 0]]))
The system becomes
The solution (c = A.LUsolve(b)
)
becomes c_0=2 -C+D and c_1=-1, resulting in
We can form this u in sympy
and check that the differential equation
and the boundary conditions are satisfied:
c = A.LUsolve(b)
u = sum(c[r,0]*psi_i.subs(i, r) for r in range(N+1)) + D*x
print(('u:', simplify(u)))
print(("u'':", simplify(diff(u, x, x))))
print(('BC x=0:', simplify(diff(u, x).subs(x, 0))))
print(('BC x=1:', simplify(u.subs(x, 1))))
('u:', C*x - C + D - x**2 + 1) ("u'':", -2) ('BC x=0:', C) ('BC x=1:', D)
The output becomes
u: C*x - C + D - x**2 + 1
u'': -2
BC x=0: C
BC x=1: D
The complete sympy
code is found in u_xx_2_CD.py
.
The exact solution is found by integrating twice and applying the
boundary conditions, either by hand or using sympy
as shown in
the section Simple model problems and their solutions. It appears that the numerical
solution coincides with the exact one.
We have seen that variational formulations end up with a formula involving u and v, such as (u',v') and a formula involving v and known functions, such as (f,v). A widely used notation is to introduce an abstract variational statement written as
where a(u,v) is a so-called bilinear form involving all the terms that contain both the test and trial function, while L(v) is a linear form containing all the terms without the trial function. For example, the statement
can be written in abstract form: find u such that
where we have the definitions
The term linear means that
for two test functions v_1 and v_2, and scalar parameters \alpha_1 and \alpha_2. Similarly, the term bilinear means that a(u,v) is linear in both its arguments:
In nonlinear problems these linearity properties do not hold in general and the abstract notation is then
The matrix system associated with a(u,v)=L(v) can also be written in an abstract form by inserting v={\psi}_i and u=\sum_j c_j{\psi}_j in a(u,v)=L(v). Using the linear properties, we get
which is a linear system
where
In many problems, a(u,v) is symmetric such that a({\psi}_j,{\psi}_i) = a({\psi}_i,{\psi}_j). In those cases the coefficient matrix becomes symmetric, A_{i,j}=A_{j,i}, a property that can simplify solution algorithms for linear systems and make them more stable. The property also reduces memory requirements and the computational work.
The abstract notation a(u,v)=L(v) for linear differential equation problems is much used in the literature and in description of finite element software (in particular the FEniCS documentation). We shall frequently summarize variational forms using this notation.
The following sections derive variational formulations for some prototype differential equations in 1D, and demonstrate how we with ease can handle variable coefficients, mixed Dirichlet and Neumann boundary conditions, first-order derivatives, and nonlinearities.
Consider the problem
There are two new features of this problem compared with previous examples: a variable coefficient {\alpha} (x) and nonzero Dirichlet conditions at both boundary points.
Let us first deal with the boundary conditions. We seek
Since the Dirichlet conditions demand
the function B(x) must fulfill B(0)=C and B(L)=D. The we are guaranteed that u(0)=C and u(L)=D. How B varies in between x=0 and x=L is not of importance. One possible choice is
which follows from (48) with p=1.
We seek (u-B)\in V. As usual,
Note that any v\in V has the property v(0)=v(L)=0.
The residual arises by inserting our u in the differential equation:
Galerkin's method is
or written with explicit integrals,
We proceed with integration by parts to lower the derivative from second to first order:
The boundary term vanishes since v(0)=v(L)=0. The variational formulation is then
The variational formulation can alternatively be written in a more compact form:
The corresponding abstract notation reads
with
We may insert u=B + \sum_jc_j{\psi}_j and v={\psi}_i to derive the linear system:
Isolating everything with the c_j coefficients on the left-hand side and all known terms on the right-hand side gives
This is nothing but a linear system \sum_j A_{i,j}c_j=b_i with
The next problem to formulate in terms of a variational form reads
The new features are a first-order derivative u' in the equation and the boundary condition involving the derivative: u'(L)=E. Since we have a Dirichlet condition at x=0, we must force {\psi}_i(0)=0 and use a boundary function to take care of the condition u(0)=C. Because there is no Dirichlet condition on x=L we do not make any requirements to {\psi}_i(L). The simplest possible choice of B(x) is B(x)=C.
The expansion for u becomes
The variational formulation arises from multiplying the equation by a test function v\in V and integrating over \Omega:
We apply integration by parts to the u''v term only. Although we could also integrate u' v by parts, this is not common. The result becomes
Now, v(0)=0 so
because u'(L)=E. Thus, integration by parts allows us to take care of the Neumann condition in the boundary term.
Natural and essential boundary conditions.
A common mistake is to forget a boundary term like [u'v]_0^L in the integration by parts. Such a mistake implies that we actually impose the condition u'=0 unless there is a Dirichlet condition (i.e., v=0) at that point! This fact has great practical consequences, because it is easy to forget the boundary term, and that implicitly set a boundary condition!
Since homogeneous Neumann conditions can be incorporated without "doing anything" (i.e., omitting the boundary term), and non-homogeneous Neumann conditions can just be inserted in the boundary term, such conditions are known as natural boundary conditions. Dirichlet conditions require more essential steps in the mathematical formulation, such as forcing all {\varphi}_i=0 on the boundary and constructing a B(x), and are therefore known as essential boundary conditions.
The final variational form reads
In the abstract notation we have
with the particular formulas
The associated linear system is derived by inserting u=B+\sum_jc_j{\psi}_j and replacing v by {\psi}_i for i\in{\mathcal{I}_s}. Some algebra results in
Observe that in this problem, the coefficient matrix is not symmetric, because of the term
Finally, we show that the techniques used above to derive variational forms apply to nonlinear differential equation problems as well. Here is a model problem with a nonlinear coefficient \alpha(u) and a nonlinear right-hand side f(u):
Our space V has basis \left\{ {{\psi}}_i \right\}_{i\in{\mathcal{I}_s}}, and because of the condition u(0)=0, we must require {\psi}_i(0)=0, i\in{\mathcal{I}_s}.
Galerkin's method is about inserting the approximate u, multiplying the differential equation by v\in V, and integrate,
The integration by parts does not differ from the case where we have {\alpha}(x) instead of {\alpha}(u):
The term {\alpha}(u(0))v(0)u'(0)=0 since v(0). The other term, {\alpha}(u(L))v(L)u'(L), is used to impose the other boundary condition u'(L)=E, resulting in
or alternatively written more compactly as
Since the problem is nonlinear, we cannot identify a bilinear form a(u,v) and a linear form L(v). An abstract formulation is typically find u such that
with
By inserting u=\sum_j c_j{\psi}_j and v={\psi}_i in F(u;v), we get a nonlinear system of algebraic equations for the unknowns c_i, i\in{\mathcal{I}_s}. Such systems must be solved by constructing a sequence of linear systems whose solutions hopefully converge to the solution of the nonlinear system. Frequently applied methods are Picard iteration and Newton's method.
Our hand calculations can benefit greatly by symbolic computing, as shown
earlier, so it is natural to extend our approximation programs based on
sympy
to the problem domain of variational formulations.
The user must prepare a function integrand_lhs(psi, i, j)
for
returning the integrand of the integral that contributes to matrix
entry (i,j) on the left-hand side. The psi
variable is a Python dictionary holding the
basis functions and their derivatives in symbolic form. More
precisely, psi[q]
is a list of
Similarly, integrand_rhs(psi, i)
returns the integrand
for entry number i in the right-hand side vector.
Since we also have contributions to the right-hand side vector (and
potentially also the matrix) from boundary terms without any integral,
we introduce two additional functions, boundary_lhs(psi, i, j)
and
boundary_rhs(psi, i)
for returning terms in the variational
formulation that are not to be integrated over the domain \Omega.
Examples, to be shown later, will explain in more detail how these
user-supplied functions may look like.
The linear system can be computed and solved symbolically by the following function:
import sympy as sym
def solver(integrand_lhs, integrand_rhs, psi, Omega,
boundary_lhs=None, boundary_rhs=None):
N = len(psi[0]) - 1
A = sym.zeros(N+1, N+1)
b = sym.zeros(N+1, 1)
x = sym.Symbol('x')
for i in range(N+1):
for j in range(i, N+1):
integrand = integrand_lhs(psi, i, j)
I = sym.integrate(integrand, (x, Omega[0], Omega[1]))
if boundary_lhs is not None:
I += boundary_lhs(psi, i, j)
A[i,j] = A[j,i] = I # assume symmetry
integrand = integrand_rhs(psi, i)
I = sym.integrate(integrand, (x, Omega[0], Omega[1]))
if boundary_rhs is not None:
I += boundary_rhs(psi, i)
b[i,0] = I
c = A.LUsolve(b)
u = sum(c[i,0]*psi[0][i] for i in range(len(psi[0])))
return u, c
Not surprisingly, symbolic solution of differential
equations, discretized by a Galerkin or least squares method
with global basis functions,
is of limited interest beyond the simplest problems, because
symbolic integration might be very time consuming or impossible, not
only in sympy
but also in
WolframAlpha
(which applies the perhaps most powerful symbolic integration
software available today: Mathematica). Numerical integration
as an option is therefore desirable.
The extended solver
function below tries to combine symbolic and
numerical integration. The latter can be enforced by the user, or it
can be invoked after a non-successful symbolic integration (being
detected by an Integral
object as the result of the integration
in sympy
).
Note that for a
numerical integration, symbolic expressions must be converted to
Python functions (using lambdify
), and the expressions cannot contain
other symbols than x
. The real solver
routine in the
varform1D.py
file has error checking and meaningful error messages in such cases.
The solver
code below is a condensed version of the real one, with
the purpose of showing how to automate the Galerkin or least squares
method for solving differential equations in 1D with global basis functions:
def solver(integrand_lhs, integrand_rhs, psi, Omega,
boundary_lhs=None, boundary_rhs=None, symbolic=True):
N = len(psi[0]) - 1
A = sym.zeros(N+1, N+1)
b = sym.zeros(N+1, 1)
x = sym.Symbol('x')
for i in range(N+1):
for j in range(i, N+1):
integrand = integrand_lhs(psi, i, j)
if symbolic:
I = sym.integrate(integrand, (x, Omega[0], Omega[1]))
if isinstance(I, sym.Integral):
symbolic = False # force num.int. hereafter
if not symbolic:
integrand_ = sym.lambdify([x], integrand, 'mpmath')
I = mpmath.quad(integrand_, [Omega[0], Omega[1]])
if boundary_lhs is not None:
I += boundary_lhs(psi, i, j)
A[i,j] = A[j,i] = I
integrand = integrand_rhs(psi, i)
if symbolic:
I = sym.integrate(integrand, (x, Omega[0], Omega[1]))
if isinstance(I, sym.Integral):
symbolic = False
if not symbolic:
integrand_ = sym.lambdify([x], integrand, 'mpmath')
I = mpmath.quad(integrand_, [Omega[0], Omega[1]])
if boundary_rhs is not None:
I += boundary_rhs(psi, i)
b[i,0] = I
c = A.LUsolve(b)
u = sum(c[i,0]*psi[0][i] for i in range(len(psi[0])))
return u, c
To demonstrate the code above, we address
with b as a (symbolic) constant. A possible basis for the space V is {\psi}_i(x) = x^{i+1}(1-x), i\in{\mathcal{I}_s}. Note that {\psi}_i(0)={\psi}_i(1)=0 as required by the Dirichlet conditions. We need a B(x) function to take care of the known boundary values of u. Any function B(x)=1-x^p, p\in\mathbb{R}, is a candidate, and one arbitrary choice from this family is B(x)=1-x^3. The unknown function is then written as
Let us use the Galerkin method to derive the variational formulation. Multiplying the differential equation by v and integrating by parts yield
and with u=B + \sum_jc_j{\psi}_j we get the linear system
The application can be coded as follows with sympy
:
import sympy as sym
x, b = sym.symbols("x b")
f = b
B = 1 - x**3
dBdx = sym.diff(B, x)
# Compute basis functions and their derivatives
N = 3
psi = {0: [x**(i+1)*(1-x) for i in range(N+1)]}
psi[1] = [sym.diff(psi_i, x) for psi_i in psi[0]]
def integrand_lhs(psi, i, j):
return psi[1][i]*psi[1][j]
def integrand_rhs(psi, i):
return f*psi[0][i] - dBdx*psi[1][i]
Omega = [0, 1]
from src.varform1D import solver
u_bar, _ = solver(integrand_lhs, integrand_rhs, psi, Omega,
verbose=True, symbolic=True)
u = B + u_bar
print(("solution u:", sym.simplify(sym.expand(u))))
...evaluating matrix... (0,0): (1 - 2*x)**2 (0,1): (1 - 2*x)*(-x**2 + 2*x*(1 - x)) (0,2): (1 - 2*x)*(-x**3 + 3*x**2*(1 - x)) (0,3): (1 - 2*x)*(-x**4 + 4*x**3*(1 - x)) rhs: b*x*(1 - x) + 3*x**2*(1 - 2*x) (1,1): (-x**2 + 2*x*(1 - x))**2 (1,2): (-x**2 + 2*x*(1 - x))*(-x**3 + 3*x**2*(1 - x)) (1,3): (-x**2 + 2*x*(1 - x))*(-x**4 + 4*x**3*(1 - x)) rhs: b*x**2*(1 - x) + 3*x**2*(-x**2 + 2*x*(1 - x)) (2,2): (-x**3 + 3*x**2*(1 - x))**2 (2,3): (-x**3 + 3*x**2*(1 - x))*(-x**4 + 4*x**3*(1 - x)) rhs: b*x**3*(1 - x) + 3*x**2*(-x**3 + 3*x**2*(1 - x)) (3,3): (-x**4 + 4*x**3*(1 - x))**2 rhs: b*x**4*(1 - x) + 3*x**2*(-x**4 + 4*x**3*(1 - x)) A: Matrix([[1/3, 1/6, 1/10, 1/15], [1/6, 2/15, 1/10, 8/105], [1/10, 1/10, 3/35, 1/14], [1/15, 8/105, 1/14, 4/63]]) b: Matrix([[b/6 - 1/2], [b/12 - 3/10], [b/20 - 1/5], [b/30 - 1/7]]) coeff: [b/2 - 1, -1, 0, 0] approximation: -x**2*(1 - x) + x*(1 - x)*(b/2 - 1) ('solution u:', -b*x**2/2 + b*x/2 - x + 1)
The printout of u
reads -b*x**2/2 + b*x/2 - x + 1
. Note that
expanding u
, before simplifying, is necessary in the present case to
get a compact, final expression with sympy
. Doing expand
before
simplify
is a common strategy for simplifying expressions in
sympy
. However, a non-expanded u
might be preferable in other
cases - this depends on the problem in question.
The exact solution {u_{\small\mbox{e}}}(x) can be derived by some sympy
code that
closely follows the examples in the section Simple model problems and their solutions. The idea is to integrate -u''=b twice
and determine the integration constants from the boundary conditions:
# DO NOT RUN THIS CELL
C1, C2 = sym.symbols('C1 C2') # integration constants
f1 = sym.integrate(f, x) + C1
f2 = sym.integrate(f1, x) + C2
# Find C1 and C2 from the boundary conditions u(0)=0, u(1)=1
s = sym.solve([u_e.subs(x,0) - 1, u_e.subs(x,1) - 0], [C1, C2])
# Form the exact solution
u_e = -f2 + s[C1]*x + s[C2]
print(('analytical solution:', u_e))
print(('error:', sym.simplify(sym.expand(u - u_e))))
The last line prints 0
, which is not surprising when
{u_{\small\mbox{e}}}(x) is a parabola and our approximate u contains polynomials up to
degree 4. It suffices to have N=1, i.e., polynomials of degree
2, to recover the exact solution.
We can play around with the code and test that with f=Kx^p, for some constants K and p, the solution is a polynomial of degree p+2, and N=p+1 guarantees that the approximate solution is exact.
Although the symbolic code is capable of integrating many choices of f(x), the symbolic expressions for u quickly become lengthy and non-informative, so numerical integration in the code, and hence numerical answers, have the greatest application potential.