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
$$ \begin{equation} -\frac{d}{dx}\left( \dfc(x)\frac{du}{dx}\right) = f(x),\quad x\in\Omega =[0,L],\ u(0)=C,\ u(L)=D\tp \end{equation} $$ There are two new features of this problem compared with previous examples: a variable coefficient \( a(x) \) and nonzero Dirichlet conditions at both boundary points.
Let us first deal with the boundary conditions. We seek
$$ u(x) = B(x) + \sum_{j\in\If} c_j\baspsi_i(x),$$ with \( \baspsi_i(0)=\baspsi_i(L)=0 \) for \( i\in\If \). The function \( B(x) \) must then fulfill \( B(0)=C \) and \( B(L)=D \). How \( B \) varies in between \( x=0 \) and \( x=L \) is not of importance. One possible choice is
$$ B(x) = C + \frac{1}{L}(D-C)x,$$ which follows from (70) with \( p=1 \).
We seek \( (u-B)\in V \). As usual,
$$ V = \hbox{span}\{\baspsi_0,\ldots,\baspsi_N\},$$ but the two Dirichlet boundary conditions demand that
$$ \baspsi_i(0)=\baspsi_i(L)=0, \quad i\in\If\tp$$ 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:
$$ R = -\frac{d}{dx}\left( \dfc\frac{du}{dx}\right) -f\tp $$ Galerkin's method is
$$ (R, v) = 0,\quad \forall v\in V, $$ or written with explicit integrals,
$$ \int_{\Omega} \left(\frac{d}{dx}\left( \dfc\frac{du}{dx}\right) -f\right)v \dx = 0,\quad \forall v\in V \tp $$ We proceed with integration by parts to lower the derivative from second to first order:
$$ -\int_{\Omega} \frac{d}{dx}\left( \dfc(x)\frac{du}{dx}\right) v \dx = \int_{\Omega} \dfc(x)\frac{du}{dx}\frac{dv}{dx}\dx - \left[\dfc\frac{du}{dx}v\right]_0^L \tp $$
The boundary term vanishes since \( v(0)=v(L)=0 \). The variational formulation is then
$$ \int_{\Omega} \dfc(x)\frac{du}{dx}\frac{dv}{dx}\dx = \int_{\Omega} f(x)v\dx,\quad \forall v\in V\tp $$ The variational formulation can alternatively be written in a more compact form:
$$ (\dfc u',v') = (f,v),\quad \forall v\in V \tp $$ The corresponding abstract notation reads
$$ a(u,v)=L(v)\quad\forall v\in V,$$ with $$ a(u,v)= (\dfc u',v'),\quad L(v)=(f,v) \tp $$ Note that the \( a \) in the notation \( a(\cdot,\cdot) \) is not to be mixed with the variable coefficient \( a(x) \) in the differential equation.
We may insert \( u=B + \sum_jc_j\baspsi_j \) and \( v=\baspsi_i \) to derive the linear system:
$$ (\dfc B' + \dfc \sum_{j\in\If} c_j \baspsi_j', \baspsi_i') = (f,\baspsi_i), \quad i\in\If \tp $$ Isolating everything with the \( c_j \) coefficients on the left-hand side and all known terms on the right-hand side gives
$$ \sum_{j\in\If} (\dfc\baspsi_j', \baspsi_i')c_j = (f,\baspsi_i) + (a(D-C)L^{-1}, \baspsi_i'), \quad i\in\If \tp $$ This is nothing but a linear system \( \sum_j A_{i,j}c_j=b_i \) with
$$ \begin{align*} A_{i,j} &= (a\baspsi_j', \baspsi_i') = \int_{\Omega} \dfc(x)\baspsi_j'(x), \baspsi_i'(x)\dx,\\ b_i &= (f,\baspsi_i) + (a(D-C)L^{-1},\baspsi_i')= \int_{\Omega} \left(f(x)\baspsi_i(x) + \dfc(x)\frac{D-C}{L}\baspsi_i'(x)\right) \dx \tp \end{align*} $$
The next problem to formulate in variational form reads
$$ \begin{equation} -u''(x) + bu'(x) = f(x),\quad x\in\Omega =[0,L],\ u(0)=C,\ u'(L)=E\tp \end{equation} $$ 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 \( \baspsi_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 \( \baspsi_i(L) \). The simplest possible choice of \( B(x) \) is \( B(x)=C \).
The expansion for \( u \) becomes
$$ u = C + \sum_{j\in\If} c_j \baspsi_i(x) \tp $$
The variational formulation arises from multiplying the equation by a test function \( v\in V \) and integrating over \( \Omega \):
$$ (-u'' + bu' - f, v) = 0,\quad\forall v\in V$$ 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
$$ (u' + bu',v') = (f,v) + [u' v]_0^L, \quad\forall v\in V \tp $$ Now, \( v(0)=0 \) so
$$ [u' v]_0^L = u'(L)v(L) = E v(L),$$ because \( u'(L)=E \). Integration by parts allows us to take care of the Neumann condition in the boundary term.
The final variational form reads
$$ (u',v') + (bu',v) = (f,v) + E v(L), \quad\forall v\in V \tp $$ In the abstract notation we have
$$ a(u,v)=L(v)\quad\forall v\in V,$$ with the particular formulas $$ a(u,v)=(u',v') + (bu',v),\quad L(v)= (f,v) + E v(L)\tp $$
The associated linear system is derived by inserting \( u=B+\sum_jc_j\baspsi_j \) and replacing \( v \) by \( \baspsi_i \) for \( i\in\If \). Some algebra results in
$$ \sum_{j\in\If} \underbrace{((\baspsi_j',\baspsi_i') + (b\baspsi_j',\baspsi_i))}_{A_{i,j}} c_j = \underbrace{(f,\baspsi_i) + E \baspsi_i(L)}_{b_i} \tp $$ Observe that in this problem, the coefficient matrix is not symmetric, because of the term
$$ (b\baspsi_j',\baspsi_i)=\int_{\Omega} b\baspsi_j'\baspsi_i \dx \neq \int_{\Omega} b \baspsi_i' \baspsi_j \dx = (\baspsi_i',b\baspsi_j) \tp $$
Finally, we show that the techniques used above to derive variational forms also apply to nonlinear differential equation problems as well. Here is a model problem with a nonlinear coefficient and right-hand side:
$$ \begin{equation} -(\dfc(u)u')' = f(u),\quad x\in [0,L],\ u(0)=0,\ u'(L)=E \tp \end{equation} $$ Our space \( V \) has basis \( \sequencei{\baspsi} \), and because of the condition \( u(0)=0 \), we must require \( \baspsi_i(0)=0 \), \( i\in\If \).
Galerkin's method is about inserting the approximate \( u \), multiplying the differential equation by \( v\in V \), and integrate,
$$ -\int_0^L \frac{d}{dx}\left(\dfc(u)\frac{du}{dx}\right)v \dx = \int_0^L f(u)v \dx\quad\forall v\in V \tp $$ The integration by parts does not differ from the case where we have \( \dfc(x) \) instead of \( \dfc(u) \):
$$ \int_0^L \dfc(u)\frac{du}{dx}\frac{dv}{dx}\dx = \int_0^L f(u)v\dx + [\dfc(u)vu']_0^L\quad\forall v\in V \tp $$ The term \( \dfc(u(0))v(0)u'(0)=0 \) since \( v(0) \). The other term, \( \dfc(u(L))v(L)u'(L) \), is used to impose the other boundary condition \( u'(L)=E \), resulting in
$$ \int_0^L \dfc(u)\frac{du}{dx}\frac{dv}{dx}v\dx = \int_0^L f(u)v\dx + \dfc(u(L))v(L)E\quad\forall v\in V, $$ or alternatively written more compactly as
$$ (\dfc(u)u', v') = (f(u),v) + \dfc(L)v(L)E\quad\forall v\in V \tp $$ Since the problem is nonlinear, we cannot identify a bilinear form \( a(u,v) \) and a linear form \( L(v) \). An abstract notation is typically find \( u \) such that
$$ F(u;v) = 0\quad\forall v\in V,$$ with $$ F(u;v) = (a(u)u', v') - (f(u),v) - a(L)v(L)E \tp $$
By inserting \( u=\sum_j c_j\baspsi_j \) we get a nonlinear system of algebraic equations for the unknowns \( c_i \), \( i\in\If \). 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.
Let us perform the necessary calculations to solve
$$ \begin{equation*} -u''(x)=2,\quad x\in \Omega=[0,1],\quad u'(0)=C,\ u(1)=D, \end{equation*} $$ using a global polynomial basis \( \baspsi_i\sim x^i \). The requirements on \( \baspsi_i \) is that \( \baspsi_i(1)=0 \), because \( u \) is specified at \( x=1 \), so a proper set of polynomial basis functions can be
$$ \baspsi_i(x)=(1-x)^{i+1}, \quad i\in\If\tp$$ A suitable \( B(x) \) function to handle the boundary condition \( u(1)=D \) is \( B(x)=Dx \). The variational formulation becomes
$$ (u',v') = (2,v) - Cv(0)\quad\forall v\in V\tp $$ The entries in the linear system are then
$$ \begin{align*} A_{i,j} &= (\baspsi_j,\baspsi_i) = \int_{0}^1 \baspsi_i'(x)\baspsi_j'(x)\dx = \int_0^1 (i+1)(j+1)(1-x)^{i+j}\dx = \frac{ij + i + j + 1}{i + j + 1},\\ b_i &= (2,\baspsi_i) - (D,\baspsi_i') -C\baspsi_i(0)\\ &= \int_0^1\left( 2\baspsi_i(x) - D\baspsi_i'(x)\right)\dx -C\baspsi_i(0)\\ &= \int_0^1 \left( 2(1-x)^{i+1} - D(i+1)(1-x)^i\right)\dx -C\baspsi_i(0)\\ &= \frac{2 - (2+i)(D+C)}{i+2} \tp \end{align*} $$
With \( N=1 \) the global matrix system is
$$ \begin{equation*} \left(\begin{array}{cc} 1 & 1\\ 1 & 4/3 \end{array}\right) \left(\begin{array}{c} c_0\\ c_1 \end{array}\right) = \left(\begin{array}{c} -C+D+1\\ 2/3 -C + D \end{array}\right) \end{equation*} $$ The solution becomes \( c_0=-C+D+2 \) and \( c_1=-1 \), resulting in
$$ \begin{equation} u(x) = 1 -x^2 + D + C(x-1), \end{equation} $$
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.
It appears that the numerical solution coincides with the exact one.
This result is to be expected because if \( (\uex - B)\in V \), \( u = \uex \),
as proved next.
We have some variational formulation: find \( (u-B)\in V \) such that \( a(u,v)=L(u)\ \forall V \). The exact solution also fulfills \( a(\uex,v)=L(v) \), but normally \( (\uex -B) \) lies in a much larger (infinite-dimensional) space. Suppose, nevertheless, that \( \uex = B + E \), where \( E\in V \). That is, apart from Dirichlet conditions, \( \uex \) lines in our finite-dimensional space \( V \) we use to compute \( u \). Writing also \( u \) on the same form \( u=B+F \), we have
$$ \begin{align*} a(B+E,v) &= L(v)\quad\forall v\in V,\\ a(B+F,v) &= L(v)\quad\forall v\in V\tp \end{align*} $$ Subtracting the equations show that \( a(E-F,v)=0 \) for all \( v\in V \), and therefore \( E-F=0 \) and \( u=\uex \).
The case treated in the section Computing with Dirichlet and Neumann conditions is of the type where \( \uex - B \) is a quadratic function that is 0 at \( x=1 \), and therefore \( (\uex -B)\in V \), and the method finds the exact solution.