$$ \newcommand{\uex}{{u_{\small\mbox{e}}}} \newcommand{\Aex}{{A_{\small\mbox{e}}}} \newcommand{\half}{\frac{1}{2}} \newcommand{\halfi}{{1/2}} \newcommand{\tp}{\thinspace .} \newcommand{\normalvec}{\boldsymbol{n}} \newcommand{\x}{\boldsymbol{x}} \newcommand{\X}{\boldsymbol{X}} \renewcommand{\u}{\boldsymbol{u}} \renewcommand{\v}{\boldsymbol{v}} \newcommand{\w}{\boldsymbol{w}} \newcommand{\V}{\boldsymbol{V}} \newcommand{\e}{\boldsymbol{e}} \newcommand{\f}{\boldsymbol{f}} \newcommand{\dfc}{\alpha} % diffusion coefficient \newcommand{\Ix}{\mathcal{I}_x} \newcommand{\Iy}{\mathcal{I}_y} \newcommand{\Iz}{\mathcal{I}_z} \newcommand{\If}{\mathcal{I}_s} % for FEM \newcommand{\Ifd}{{I_d}} % for FEM \newcommand{\Ifb}{{I_b}} % for FEM \newcommand{\sequencei}[1]{\left\{ {#1}_i \right\}_{i\in\If}} \newcommand{\basphi}{\varphi} \newcommand{\baspsi}{\psi} \newcommand{\refphi}{\tilde\basphi} \newcommand{\psib}{\boldsymbol{\psi}} \newcommand{\sinL}[1]{\sin\left((#1+1)\pi\frac{x}{L}\right)} \newcommand{\xno}[1]{x_{#1}} \newcommand{\Xno}[1]{X_{(#1)}} \newcommand{\yno}[1]{y_{#1}} \newcommand{\xdno}[1]{\boldsymbol{x}_{#1}} \newcommand{\dX}{\, \mathrm{d}X} \newcommand{\dx}{\, \mathrm{d}x} \newcommand{\ds}{\, \mathrm{d}s} \newcommand{\Real}{\mathbb{R}} \newcommand{\Integer}{\mathbb{Z}} $$

Exercises

Exercise 23: Refactor functions into a more general class

The section Simple model problems displays three functions for computing the analytical solution of some simple model problems. There is quite some repetitive code, suggesting that the functions can benefit from being refactored into a class where the user can define the \( f(x) \), \( a(x) \), and the boundary conditions in particular methods in subclasses. Demonstrate how the new class can be used to solve the three particular problems in the section Simple model problems.

In the method that computes the solution, check that the solution found fulfills the differential equation and the boundary conditions. Filename: uxx_f_sympy_class.py.

Exercise 24: Compute the deflection of a cable with sine functions

A hanging cable of length \( L \) with significant tension \( T \) has a downward deflection \( w(x) \) governed by

Solve $$ T w''(x) = \ell(x), $$ when \( \ell(x) \) the vertical load per unit length. The cable is fixed at \( x=0 \) and \( x=L \) so the boundary conditions become \( w(0)=w(L)=0 \).

If we assume a constant load \( \ell(x)=\hbox{const} \), the solution is expected to be symmetric around \( x=L/2 \). For a function \( w(x) \) that is symmetric around some point \( x_0 \), it means that \( w(x_0-h) = w(x_0+h) \), and then \( w'(x_0)=\lim_{h\rightarrow 0}(w(x_0+h)- w(x_0-h))/(2h)=0 \). We can therefore halve the domain and seek \( w(x) \) in \( [0,L/2] \) with boundary conditions \( w(0)=0 \) and \( w'(L/2)=0 \).

The problem can be scaled by introducing a dimensionless coordinate (also called \( x \)) in \( [0,1] \) and a dimensionless vertical deflection \( u(x) \). The differential equation problem for \( u(x) \) becomes $$ u'' = 1,\quad x\in (0,1),\quad u(0)=0,\ u'(1)=0\tp $$

A possible function space is spanned by \( \baspsi_i=\sin ((2i+1)\pi x/2) \), \( i=0,\ldots,N \). Use a Galerkin and a least squares method to find the coefficients \( c_j \) in \( u(x)=\sum_j c_j\baspsi_j \). Find how fast the coefficients decrease in magnitude by looking at \( c_j/c_{j-1} \). Find the error in the maximum deflection at \( x=1 \) when only one basis function is used (\( N=0 \)).

What happens if we choose basis functions \( \baspsi_i=\sin ((i+1)\pi x) \)? These basis functions are appropriate if we do not utilize symmetry and solve the original problem on \( [0,L] \). A scaled version of this problem reads $$ u''=1,\quad x\in(0,1),\quad u(0)=u(1)=0\tp$$ Carry out the computations with \( N=0 \) and demonstrate that the maximum deflection \( u(1/2) \) is the same in the problem utilizing symmetry and the problem covering the whole cable. Filenames: cable_sin.pdf, cable_sin.py.

Exercise 25: Check integration by parts

Consider the Galerkin method for the problem involving \( u \) in Exercise 24: Compute the deflection of a cable with sine functions. Show that the formulas for \( c_j \) are independent of whether we perform integration by parts or not. Filename: cable_integr_by_parts.pdf.

Exercise 26: Compute the deflection of a cable with 2 P1 elements

Solve the problem for \( u \) in Exercise 24: Compute the deflection of a cable with sine functions using two P1 linear elements. Filename: cable_2P1.pdf.

Exercise 27: Compute the deflection of a cable with 1 P2 element

Solve the problem for \( u \) in Exercise 24: Compute the deflection of a cable with sine functions using one P2 element with quadratic basis functions. Filename: cable_1P2.pdf.

Exercise 28: Compute the deflection of a cable with a step load

We consider the deflection of a tension cable as described in Exercise 24: Compute the deflection of a cable with sine functions. Now the load is $$ \ell (x) =\left\lbrace\begin{array}{ll} \ell_1, & x < L/2,\\ \ell_2, & x \geq L/2 \end{array}\right.\quad x\in [0,L] \tp $$ This load is not symmetric with respect to the midpoint \( x=L/2 \) so the solution loses its symmetry and we must solve the scaled problem $$ u'' =\left\lbrace\begin{array}{ll} 1, & x < 1/2,\\ 0, & x \geq 1/2 \end{array}\right. \quad x\in (0,1),\quad u(0)=0,\ u(1)=0 \tp $$

a) Use \( \baspsi_i = \sin((i+1)\pi x) \), \( i=0,\ldots,N \) and the Galerkin method without integration by parts. Derive a formula for \( c_j \) in the solution expansion \( u=\sum_j c_j\baspsi_j \). Plot how fast the coefficients \( c_j \) tend to zero (on a log scale).

b) Solve the problem with P1 finite elements. Plot the solution for \( N_e=2,4,8 \) elements.

Filename: cable_discont_load.pdf.

Exercise 29: Show equivalence between linear systems

Incorporation of Dirichlet conditions at \( x=0 \) and \( x=L \) in a finite element mesh on \( \Omega=[0,L] \) can either be done by introducing an expansion \( u(x)=U_0\basphi_0 + U_{N_n}\basphi_{N_n} + \sum_{j=0}^{N} c_j\basphi_{\nu(j)} \), with \( N=N_n-2 \) and considering \( u \) values at the inner nodes as unknowns, or one can assemble the matrix system with \( u(x)=\sum_{j=0}^{N=N_n} c_j\basphi_j \) and afterwards replace the rows corresponding to known \( c_j \) values by the boundary conditions. Show that the two approaches are equivalent.

Exercise 30: Compute with a non-uniform mesh

Derive the linear system for the problem \( -u''=2 \) on \( [0,1] \), with \( u(0)=0 \) and \( u(1)=1 \), using P1 elements and a non-uniform mesh. The vertices have coordinates \( \xno{0}=0 < \xno{1} < \cdots < \xno{N}=1 \), and the length of cell number \( e \) is \( h_e = \xno{e+1} -\xno{e} \).

It is of interest to compare the discrete equations for the finite element method in a non-uniform mesh with the corresponding discrete equations arising from a finite difference method. Go through the derivation of the finite difference formula \( u''(x_i) \approx [D_x D_x u]_i \) and modify it to find a natural discretization of \( u''(x_i) \) on a non-uniform mesh. Filename: nonuniform_P1.pdf.

Problem 31: Solve a 1D finite element problem by hand

The following scaled 1D problem is a very simple, yet relevant, model for convective transport in fluids: $$ \begin{equation} u' = \epsilon u'' ,\quad u(0)=0,\ u(1)=1,\ x\in [0,1] \tp \end{equation} $$

a) Find the analytical solution to this problem. (Introduce \( w=u' \), solve the first-order differential equation for \( w(x) \), and integrate once more.)

b) Derive the variational form of this problem.

c) Introduce a finite element mesh with uniform partitioning. Use P1 elements and compute the element matrix and vector for a general element.

d) Incorporate the boundary conditions and assemble the element contributions.

e) Identify the resulting linear system as a finite difference discretization of the differential equation using $$ [D_{2x}u = \epsilon D_xD_x u]_i \tp $$

f) Compute the numerical solution and plot it together with the exact solution for a mesh with 20 elements and \( \epsilon=10, 1, 0.1, 0.01 \).

Filename: convdiff1D_P1.pdf.

Exercise 32: Compare finite elements and differences for a radially symmetric Poisson equation

We consider the Poisson problem in a disk with radius \( R \) with Dirichlet conditions at the boundary. Given that the solution is radially symmetric and hence dependent only on the radial coordinate (\( r=\sqrt{x^2+y^2} \)), we can reduce the problem to a 1D Poisson equation $$ \begin{equation} -\frac{1}{r}\frac{d}{dr}\left( r\frac{du}{dr}\right) = f(r),\quad r\in (0,R),\ u'(0)=0,\ u(R)=U_R \tp \tag{163} \end{equation} $$

a) Derive a variational form of (163) by integrating over the whole disk, or posed equivalently: use a weighting function \( 2\pi r v(r) \) and integrate \( r \) from \( 0 \) to \( R \).

b) Use a uniform mesh partition with P1 elements and show what the resulting set of equations becomes. Integrate the matrix entries exact by hand, but use a Trapezoidal rule to integrate the \( f \) term.

c) Explain that an intuitive finite difference method applied to (163) gives $$ \frac{1}{r_i}\frac{1}{h^2}\left( r_{i+\half}(u_{i+1}-u_i) - r_{i-\half}(u_{i}-u_{i-1})\right) = f_i,\quad i=rh \tp $$

For \( i=0 \) the factor \( 1/r_i \) seemingly becomes problematic. One must always have \( u'(0)=0 \), because of the radial symmetry, which implies \( u_{-1}=u_1 \), if we allow introduction of a fictitious value \( u_{-1} \). Using this \( u_{-1} \) in the difference equation for \( i=0 \) gives $$ \begin{align*} &\frac{1}{r_0}\frac{1}{h^2}\left( r_{\half}(u_{1}-u_0) - r_{-\half}(u_{0}-u_{1})\right) = \\ & \qquad \frac{1}{r_0}\frac{1}{2h^2}\left( (r_0 + r_1)(u_{1}-u_0) - (r_{-1} + r_0)(u_{0}-u_{1})\right) \approx 2(u_1-u_0), \end{align*} $$ if we use \( r_{-1}+r_1\approx 2r_0 \).

Set up the complete set of equations for the finite difference method and compare to the finite element method in case a Trapezoidal rule is used to integrate the \( f \) term in the latter method.

Filename: radial_Poisson1D_P1.pdf.

Exercise 33: Compute with variable coefficients and P1 elements by hand

Consider the problem $$ \begin{equation} -\frac{d}{dx}\left( a(x)\frac{du}{dx}\right) + \gamma u = f(x), \quad x\in\Omega=[0,L],\quad u(0)=\dfc,\ u'(L)=\beta\tp \tag{164} \end{equation} $$ We choose \( a(x)=1+x^2 \). Then $$ \begin{equation} u(x) = \dfc + \beta(1+L^2)\tan^{-1}(x), \end{equation} $$ is an exact solution if \( f(x) = \gamma u \).

Derive a variational formulation and compute general expressions for the element matrix and vector in an arbitrary element, using P1 elements and a uniform partitioning of \( [0,L] \). The right-hand side integral is challenging and can be computed by a numerical integration rule. The Trapezoidal rule (41) gives particularly simple expressions. Filename: atan1D_P1.pdf.

Exercise 34: Solve a 2D Poisson equation using polynomials and sines

The classical problem of applying a torque to the ends of a rod can be modeled by a Poisson equation defined in the cross section \( \Omega \): $$ -\nabla^2 u = 2,\quad (x,y)\in\Omega,$$ with \( u=0 \) on \( \partial\Omega \). Exactly the same problem arises for the deflection of a membrane with shape \( \Omega \) under a constant load.

For a circular cross section one can readily find an analytical solution. For a rectangular cross section the analytical approach ends up with a sine series. The idea in this exercise is to use a single basis function to obtain an approximate answer.

We assume for simplicity that the cross section is the unit square: \( \Omega = [0,1]\times [0,1] \).

a) We consider the basis \( \baspsi_{p,q}(x,y) = \sin((p+1)\pi x)\sin (q\pi y) \), \( p,q=0,\ldots,n \). These basis functions fulfill the Dirichlet condition. Use a Galerkin method and \( n=0 \).

b) The basis function involving sine functions are orthogonal. Use this property in the Galerkin method to derive the coefficients \( c_{p,q} \) in a formula \( u=\sum_p\sum_q c_{p,q}\baspsi_{p,q}(x,y) \).

c) Another possible basis is \( \baspsi_i(x,y) = (x(1-x)y(1-y))^{i+1} \), \( i=0,\ldots,N \). Use the Galerkin method to compute the solution for \( N=0 \). Which choice of a single basis function is best, \( u\sim x(1-x)y(1-y) \) or \( u\sim \sin(\pi x)\sin(\pi y) \)? In order to answer the question, it is necessary to search the web or the literature for an accurate estimate of the maximum \( u \) value at \( x=y=1/2 \).

Filename: torsion_sin_xy.pdf.

Exercise 35: Analyze a Crank-Nicolson scheme for the diffusion equation

Perform the analysis in the section Analysis of the discrete equations for a 1D diffusion equation \( u_t = \dfc u_{xx} \) discretized by the Crank-Nicolson scheme in time: $$ \frac{u^{n+1}- u^n}{\Delta t} = \dfc \half\left( \frac{\partial u^{n+1}}{\partial x^2} \frac{\partial u^{n}}{\partial x^2}\right),$$ or written compactly with finite difference operators, $$ [D_t u = \dfc D_xD_x \overline{u}^t]^{n+\half}\tp$$ (From a strict mathematical point of view, the \( u^n \) and \( u^{n+1} \) in these equations should be replaced by \( \uex^n \) and \( \uex^{n+1} \) to indicate that the unknown is the exact solution of the PDE discretized in time, but not yet in space, see the section Discretization in time by a Forward Euler scheme.) Make plots similar to those in the section Analysis of the discrete equations. Filename: fe_diffusion.pdf.