.. !split Exercises (1) ============== .. --- begin exercise --- .. _fem:approx:exer:linalg1: Exercise 1: Linear algebra refresher I -------------------------------------- Look up the topic of *vector space* in your favorite linear algebra book or search for the term at Wikipedia. Prove that vectors in the plane :math:`(a,b)` form a vector space by showing that all the axioms of a vector space are satisfied. Similarly, prove that all linear functions of the form :math:`ax+b` constitute a vector space, :math:`a,b\in\mathbb{R}`. On the contrary, show that all quadratic functions of the form :math:`1 + ax^2 + bx` *do not* constitute a vector space. Filename: ``linalg1.pdf``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:linalg2: Exercise 2: Linear algebra refresher II --------------------------------------- As an extension of :ref:`fem:approx:exer:linalg1`, check out the topic of *inner product spaces*. Suggest a possible inner product for the space of all linear functions of the form :math:`ax+b`, :math:`a,b\in\mathbb{R}`. Show that this inner product satisfies the general requirements of an inner product in a vector space. Filename: ``linalg2.pdf``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:vec:3Dby2D: Exercise 3: Approximate a three-dimensional vector in a plane ------------------------------------------------------------- Given :math:`\boldsymbol{f} = (1,1,1)` in :math:`\mathbb{R}^3`, find the best approximation vector :math:`\boldsymbol{u}` in the plane spanned by the unit vectors :math:`(1,0)` and :math:`(0,1)`. Repeat the calculations using the vectors :math:`(2,1)` and :math:`(1,2)`. Filename: ``vec111_approx.pdf``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:exp:powers: Exercise 4: Approximate the exponential function by power functions ------------------------------------------------------------------- Let :math:`V` be a function space with basis functions :math:`x^i`, :math:`i=0,1,\ldots,N`. Find the best approximation to :math:`f(x)=\exp(-x)` on :math:`\Omega =[0,8]` among all functions in :math:`V` for :math:`N=2,4,6`. Illustrate the three approximations in three separate plots. .. --- begin hint in exercise --- **Hint.** The exercise is easy to solve if you apply the ``lest_squares`` and ``comparison_plot`` functions in the ``approx1D.py`` module. .. --- end hint in exercise --- Filename: ``exp_powers.py``. .. good idea to add them. .. Taylor: these polynomials go so far off on [0,8] that it is not a .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:sin:powers: Exercise 5: Approximate the sine function by power functions ------------------------------------------------------------ In this exercise we want to approximate the sine function by polynomials of order :math:`N+1`. Consider two bases: .. math:: V_1 &= \{x, x^3, x^5, \ldots, x^{N-2}, x^N \}, \\ V_2 &= \{1,x,x^2,x^3,\ldots, x^N\}{\thinspace .} The basis :math:`V_1` is motivated by the fact that the Taylor polynomial approximation to the sine function has only odd powers, while :math:`V_2` is motivated by the assumption that also the even powers could improve the approximation in a least-squares setting. Compute the best approximation to :math:`f(x)=\sin(x)` among all functions in :math:`V_1` and :math:`V_2` on two domains of increasing sizes: :math:`\Omega_{1,k} = [0, k\pi]`, :math:`k=2,3\ldots,6` and :math:`\Omega_{2,k} = [-k\pi /2, k\pi/2]`, :math:`k=2,3\ldots,6`. Make plots for all combinations of :math:`V_1`, :math:`V_2`, :math:`\Omega_1`, :math:`\Omega_2`, :math:`k=2,3,\ldots,6`. Add a plot of the :math:`N`-th degree Taylor polynomial approximation of :math:`\sin(x)` around :math:`x=0`. .. --- begin hint in exercise --- **Hint.** You can make a loop over :math:`V_1` and :math:`V_2`, a loop over :math:`\Omega_1` and :math:`\Omega_2`, and a loop over :math:`k`. Inside the loops, call the functions ``least_squares`` and ``comparison_plot`` from the ``approx1D`` module. :math:`N=9` is a suggested value. .. --- end hint in exercise --- Filename: ``sin_powers.py``. .. Solveig explanations based on f-B approx .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:tanh:sine1: Exercise 6: Approximate a steep function by sines ------------------------------------------------- Find the best approximation of :math:`f(x) = \tanh (s(x-\pi))` on :math:`[0, 2\pi]` in the space :math:`V` with basis :math:`{\psi}_i(x) = \sin((2i+1)x)`, :math:`i\in{\mathcal{I}_s} = \{0,\ldots,N\}`. Make a movie showing how :math:`u=\sum_{j\in{\mathcal{I}_s}}c_j{\psi}_j(x)` approximates :math:`f(x)` as :math:`N` grows. Choose :math:`s` such that :math:`f` is steep (:math:`s=20` may be appropriate). .. --- begin hint in exercise --- **Hint.** One may naively call the ``least_squares_orth`` and ``comparison_plot`` from the ``approx1D`` module in a loop and extend the basis with one new element in each pass. This approach implies a lot of recomputations. A more efficient strategy is to let ``least_squares_orth`` compute with only one basis function at a time and accumulate the corresponding ``u`` in the total solution. .. --- end hint in exercise --- Filename: ``tanh_sines_approx.py``. .. Closing remarks for this Exercise Remarks ~~~~~~~ Approximation of a discontinuous (or steep) :math:`f(x)` by sines, results in slow convergence and oscillatory behavior of the approximation close to the abrupt changes in :math:`f`. This is known as the `Gibb's phenomenon `__. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:tanh:sine2: Exercise 7: Animate the approximation of a steep function by sines ------------------------------------------------------------------ Make a movie that shows how the approximation in :ref:`fem:approx:exer:tanh:sine1` is improved as :math:`N` grows. Illustrate a smooth case where :math:`s=0.5` and a steep case where :math:`s=20` in the :math:`\tanh (s(x-\pi))` function. Filename: ``tanh_sines_approx_movie.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:Fourier: Exercise 8: Fourier series as a least squares approximation ----------------------------------------------------------- Given a function :math:`f(x)` on an interval :math:`[0,L]`, look up the formula for the coefficients :math:`a_j` and :math:`b_j` in the Fourier series of :math:`f`: .. math:: f(x) = a_0 + \sum_{j=1}^\infty a_j\cos \left(j\frac{\pi x}{L}\right) + \sum_{j=1}^\infty b_j\sin \left(j\frac{\pi x}{L}\right){\thinspace .} Let an infinite-dimensional vector space :math:`V` have the basis functions :math:`\cos j\frac{\pi x}{L}` for :math:`j=0,1,\dots,\infty` and :math:`\sin j\frac{\pi x}{L}` for :math:`j=1,\dots,\infty`. Show that the least squares approximation method from the section :ref:`fem:approx:global` leads to a linear system whose solution coincides with the standard formulas for the coefficients in a Fourier series of :math:`f(x)` (see also the section :ref:`fem:approx:global:Fourier`). You may choose .. math:: {\psi}_{2i} = \cos\left( i\frac{\pi}{L}x\right),\quad {\psi}_{2i+1} = \sin\left( i\frac{\pi}{L}x\right), for :math:`i=0,1,\ldots,N\rightarrow\infty`. Choose :math:`f(x) = \tanh(s(x-\frac{1}{2}))` on :math:`\Omega=[0,1]`, which is a smooth function, but with considerable steepness around :math:`x=1/2` as :math:`s` grows in size. Calculate the coefficients in the Fourier expansion by solving the linear system, arising from the least squares or Galerkin methods, by hand. Plot some truncated versions of the series together with :math:`f(x)` to show how the series expansion converges for :math:`s=10` and :math:`s=100`. Filename: ``Fourier_approx.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:tanh:Lagrange: Exercise 9: Approximate a steep function by Lagrange polynomials ---------------------------------------------------------------- Use interpolation/collocation with uniformly distributed points and Chebychev nodes to approximate .. math:: f(x) = -\tanh(s(x-\frac{1}{2})),\quad x\in [0,1], by Lagrange polynomials for :math:`s=10` and :math:`s=100`, and :math:`N=3,6,9,11`. Make separate plots of the approximation for each combination of :math:`s`, point type (Chebyshev or uniform), and :math:`N`. Filename: ``tanh_Lagrange.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:mesh1: Exercise 10: Define nodes and elements -------------------------------------- Consider a domain :math:`\Omega =[0,2]` divided into the three P2 elements :math:`[0,1]`, :math:`[1,1.2]`, and :math:`[1.2,2]`. For P1 and P2 elements, set up the list of coordinates and nodes (``nodes``) and the numbers of the nodes that belong to each element (``elements``) in two cases: 1) nodes and elements numbered from left to right, and 2) nodes and elements numbered from right to left. Filename: ``fe_numberings1.py.``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:mesh2: Exercise 11: Define vertices, cells, and dof maps ------------------------------------------------- Repeat :ref:`fem:approx:fe:exer:mesh1`, but define the data structures ``vertices``, ``cells``, and ``dof_map`` instead of ``nodes`` and ``elements``. Filename: ``fe_numberings2.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:defmesh:sparsity: Exercise 12: Construct matrix sparsity patterns ----------------------------------------------- :ref:`fem:approx:fe:exer:mesh1` describes a element mesh with a total of five elements, but with two different element and node orderings. For each of the two orderings, make a :math:`5\times 5` matrix and fill in the entries that will be nonzero. .. --- begin hint in exercise --- **Hint.** A matrix entry :math:`(i,j)` is nonzero if :math:`i` and :math:`j` are nodes in the same element. .. --- end hint in exercise --- Filename: ``fe_sparsity_pattern.pdf``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:Asinwt:symbolic: Exercise 13: Perform symbolic finite element computations --------------------------------------------------------- Perform symbolic calculations to find formulas for the coefficient matrix and right-hand side when approximating :math:`f(x) = \sin (x)` on :math:`\Omega=[0, \pi]` by two P1 elements of size :math:`\pi/2`. Solve the system and compare :math:`u(\pi/2)` with the exact value 1. Filename: ``sin_approx_P1.py``. .. print sp.simplify(c[1,0].subs('h', sp.pi/2)) .. Omega=[0,sp.pi]) .. c = approximate(sp.sin(x), symbolic=True, d=1, N_e=2, numint=None, .. from fe_approx1D_numint import * .. solution: .. that arises on the right-hand side. .. which is the integral .. Hint: wolframalpha or sympy can help with (1-x)*sin(a*x+b), .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:tanh:P1P2: Exercise 14: Approximate a steep function by P1 and P2 elements --------------------------------------------------------------- Given .. math:: f(x) = \tanh(s(x-\frac{1}{2})) use the Galerkin or least squares method with finite elements to find an approximate function :math:`u(x)`. Choose :math:`s=40` and try :math:`N_e=4,8,16` P1 elements and :math:`N_e=2,4,8` P2 elements. Integrate :math:`f{\varphi}_i` numerically. Filename: ``tanh_fe_P1P2_approx.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:exer:tanh:P3P4: Exercise 15: Approximate a steep function by P3 and P4 elements --------------------------------------------------------------- Solve :ref:`fem:approx:exer:tanh:P1P2` using :math:`N_e=1,2,4` P3 and P4 elements. How will a collocation/interpolation method work in this case with the same number of nodes? Filename: ``tanh_fe_P3P4_approx.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:Asinwt:interpol:error: Exercise 16: Investigate the approximation error in finite elements ------------------------------------------------------------------- The theory :ref:`(8.1) ` from the section :ref:`fem:approx:fe:error` predicts that the error in the Pd approximation of a function should behave as :math:`h^{d+1}`, where :math:`h` is the length of the element. Use experiments to verify this asymptotic behavior (i.e., for small enough :math:`h`). Choose three examples: :math:`f(x)=Ae^{-\omega x}` on :math:`[0,3/\omega]`, :math:`f(x) = A\sin (\omega x)` on :math:`\Omega=[0, 2\pi/\omega]` for constant :math:`A` and :math:`\omega`, and :math:`f(x)=\sqrt{x}` on :math:`[0,1]`. .. --- begin hint in exercise --- **Hint.** Run a series of experiments: :math:`(h_i,E_i)`, :math:`i=0,\ldots,m`, where :math:`E_i` is the :math:`L^2` norm of the error corresponding to element length :math:`h_i`. Assume an error model :math:`E=Ch^r` and compute :math:`r` from two successive experiments: .. math:: r_i = \ln (E_{i+1}/E_i)/\ln (h_{i+1}/h_i),\quad i=0,\ldots,m-1{\thinspace .} Hopefully, the sequence :math:`r_0,\ldots,r_{m-1}` converges to the true :math:`r`, and :math:`r_{m-1}` can be taken as an approximation to :math:`r`. Run such experiments for different :math:`d` for the different :math:`f(x)` functions. .. --- end hint in exercise --- Filename: ``Pd_approx_error.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:Heaviside: Exercise 17: Approximate a step function by finite elements ----------------------------------------------------------- Approximate the step function .. math:: f(x) = \left\lbrace\begin{array}{ll} 1 & 0\leq x < {1/2},\\ 2 & {1/2} \leq x \geq {1/2} \end{array}\right. by 2, 4, and 8 P1 and P2 elements. Compare approximations visually. .. --- begin hint in exercise --- **Hint.** This :math:`f` can also be expressed in terms of the Heaviside function :math:`H(x)`: :math:`f(x) = H(x-{1/2})`. Therefore, :math:`f` can be defined by .. code-block:: python f = sp.Heaviside(x - sp.Rational(1,2)) making the ``approximate`` function in the ``fe_approx1D.py`` module an obvious candidate to solve the problem. However, ``sympy`` does not handle symbolic integration with this particular integrand, and the ``approximate`` function faces a problem when converting ``f`` to a Python function (for plotting) since ``Heaviside`` is not an available function in ``numpy``. It is better to make special-purpose code for this case or perform all calculations by hand. .. --- end hint in exercise --- Filename: ``Heaviside_approx_P1P2.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:2Dsines:symbolic: Exercise 18: 2D approximation with orthogonal functions ------------------------------------------------------- Assume we have basis functions :math:`{\varphi}_i(x,y)` in 2D that are orthogonal such that :math:`({\varphi}_i,{\varphi}_j)=0` when :math:`i\neq j`. The function ``least_squares`` in the file `approx2D.py `__ will then spend much time on computing off-diagonal terms in the coefficient matrix that we know are zero. To speed up the computations, make a version ``least_squares_orth`` that utilizes the orthogonality among the basis functions. Apply the function to approximate .. math:: f(x,y) = x(1-x)y(1-y)e^{-x-y} on :math:`\Omega = [0,1]\times [0,1]` via basis functions .. math:: {\varphi}_i(x,y) = \sin ((p+1)\pi x)\sin((q+1)\pi y),\quad i=q(N_x+1) + p, where :math:`p=0,\ldots,N_x` and :math:`q=0,\ldots,N_y`. .. --- begin hint in exercise --- **Hint.** Get ideas from the function ``least_squares_orth`` in the section :ref:`fem:approx:global:orth` and file `approx1D.py `__. .. --- end hint in exercise --- Filename: ``approx2D_least_squares_orth.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:1D:trapez: Exercise 19: Use the Trapezoidal rule and P1 elements ----------------------------------------------------- Consider approximation of some :math:`f(x)` on an interval :math:`\Omega` using the least squares or Galerkin methods with P1 elements. Derive the element matrix and vector using the Trapezoidal rule :ref:`(9.1) ` for calculating integrals on the reference element. Assemble the contributions, assuming a uniform cell partitioning, and show that the resulting linear system has the form :math:`c_i=f(x_{i})` for :math:`i\in{\mathcal{I}_s}`. Filename: ``fe_P1_trapez.pdf``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:1D:P1:vs:interp: Problem 20: Compare P1 elements and interpolation ------------------------------------------------- We shall approximate the function .. math:: f(x) = 1 + \epsilon\sin (2\pi nx),\quad x\in \Omega = [0,1], where :math:`n\in\mathbb{Z}` and :math:`\epsilon \geq 0`. **a)** Plot :math:`f(x)` for :math:`n=1,2,3` and find the wave length of the function. **b)** We want to use :math:`N_P` elements per wave length. Show that the number of elements is then :math:`nN_P`. **c)** The critical quantity for accuracy is the number of elements per wave length, not the element size in itself. It therefore suffices to study an :math:`f` with just one wave length in :math:`\Omega = [0,1]`. Set :math:`\epsilon = 0.5`. Run the least squares or projection/Galerkin method for :math:`N_P=2,4,8,16,32`. Compute the error :math:`E=||u-f||_{L^2}`. .. --- begin hint in exercise --- **Hint.** Use the ``fe_approx1D_numint`` module to compute :math:`u` and use the technique from the section :ref:`fem:approx:fe:error` to compute the norm of the error. .. --- end hint in exercise --- **d)** Repeat the set of experiments in the above point, but use interpolation/collocation based on the node points to compute :math:`u(x)` (recall that :math:`c_i` is now simply :math:`f(x_{i})`). Compute the error :math:`E=||u-f||_{L^2}`. Which method seems to be most accurate? Filename: ``P1_vs_interp.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:3D:approx3D: Exercise 21: Implement 3D computations with global basis functions ------------------------------------------------------------------ Extend the `approx2D.py `__ code to 3D applying ideas from the section :ref:`fem:approx:3D:global`. Use a 3D generalization of the test problem in the section :ref:`fem:approx:2D:global:code` to test the implementation. Filename: ``approx3D.py``. .. --- end exercise --- .. --- begin exercise --- .. _fem:approx:fe:exer:1D:simpson: Exercise 22: Use Simpson's rule and P2 elements ----------------------------------------------- Redo :ref:`fem:approx:fe:exer:1D:trapez`, but use P2 elements and Simpson's rule based on sampling the integrands at the nodes in the reference cell. Filename: ``fe_P2_simpson.pdf``. .. --- end exercise ---