.. !split .. _fem:approx:global: Approximation of functions ========================== .. index:: single: approximation; of functions Let :math:`V` be a function space spanned by a set of *basis functions* :math:`{\psi}_0,\ldots,{\psi}_N`, .. math:: V = \hbox{span}\,\{{\psi}_0,\ldots,{\psi}_N\}, such that any function :math:`u\in V` can be written as a linear combination of the basis functions: .. _Eq:fem:approx:ufem: .. math:: :label: fem:approx:ufem u = \sum_{j\in{\mathcal{I}_s}} c_j{\psi}_j{\thinspace .} The index set :math:`{\mathcal{I}_s}` is defined as :math:`{\mathcal{I}_s} =\{0,\ldots,N\}` and is used both for compact notation and for flexibility in the numbering of elements in sequences. For now, in this introduction, we shall look at functions of a single variable :math:`x`: :math:`u=u(x)`, :math:`{\psi}_i={\psi}_i(x)`, :math:`i\in{\mathcal{I}_s}`. Later, we will almost trivially extend the mathematical details to functions of two- or three-dimensional physical spaces. The approximation :eq:`fem:approx:ufem` is typically used to discretize a problem in space. Other methods, most notably finite differences, are common for time discretization, although the form :eq:`fem:approx:ufem` can be used in time as well. .. _fem:approx:LS: The least squares method (3) ----------------------------- Given a function :math:`f(x)`, how can we determine its best approximation :math:`u(x)\in V`? A natural starting point is to apply the same reasoning as we did for vectors in the section :ref:`fem:approx:vec:Np1dim`. That is, we minimize the distance between :math:`u` and :math:`f`. However, this requires a norm for measuring distances, and a norm is most conveniently defined through an inner product. Viewing a function as a vector of infinitely many point values, one for each value of :math:`x`, the inner product could intuitively be defined as the usual summation of pairwise components, with summation replaced by integration: .. math:: (f,g) = \int f(x)g(x)\, {\, \mathrm{d}x} {\thinspace .} To fix the integration domain, we let :math:`f(x)` and :math:`{\psi}_i(x)` be defined for a domain :math:`\Omega\subset\mathbb{R}`. The inner product of two functions :math:`f(x)` and :math:`g(x)` is then .. _Eq:fem:approx:LS:innerprod: .. math:: :label: fem:approx:LS:innerprod (f,g) = \int_\Omega f(x)g(x)\, {\, \mathrm{d}x} {\thinspace .} The distance between :math:`f` and any function :math:`u\in V` is simply :math:`f-u`, and the squared norm of this distance is .. _Eq:fem:approx:LS:E: .. math:: :label: fem:approx:LS:E E = (f(x)-\sum_{j\in{\mathcal{I}_s}} c_j{\psi}_j(x), f(x)-\sum_{j\in{\mathcal{I}_s}} c_j{\psi}_j(x)){\thinspace .} Note the analogy with :ref:`(3.9) `: the given function :math:`f` plays the role of the given vector :math:`\boldsymbol{f}`, and the basis function :math:`{\psi}_i` plays the role of the basis vector :math:`\boldsymbol{\psi}_i`. We can rewrite :eq:`fem:approx:LS:E`, through similar steps as used for the result :ref:`(3.9) `, leading to .. math:: E(c_i, \ldots, c_N) = (f,f) -2\sum_{j\in{\mathcal{I}_s}} c_j(f,{\psi}_i) + \sum_{p\in{\mathcal{I}_s}}\sum_{q\in{\mathcal{I}_s}} c_pc_q({\psi}_p,{\psi}_q){\thinspace .} Minimizing this function of :math:`N+1` scalar variables :math:`\left\{ {c}_i \right\}_{i\in{\mathcal{I}_s}}`, requires differentiation with respect to :math:`c_i`, for all :math:`i\in{\mathcal{I}_s}`. The resulting equations are very similar to those we had in the vector case, and we hence end up with a linear system of the form :ref:`(3.10) `, with basically the same expressions: .. _Eq:fem:approx:Aij: .. math:: :label: fem:approx:Aij A_{i,j} = ({\psi}_i,{\psi}_j), .. _Eq:fem:approx:bi: .. math:: :label: fem:approx:bi b_i = (f,{\psi}_i){\thinspace .} The projection (or Galerkin) method ----------------------------------- .. index:: single: Galerkin method; functions .. index:: single: projection; functions As in the section :ref:`fem:approx:vec:Np1dim`, the minimization of :math:`(e,e)` is equivalent to .. _Eq:fem:approx:Galerkin: .. math:: :label: fem:approx:Galerkin (e,v)=0,\quad\forall v\in V{\thinspace .} This is known as a projection of a function :math:`f` onto the subspace :math:`V`. We may also call it a Galerkin method for approximating functions. Using the same reasoning as in :ref:`(3.11) `-:ref:`(3.12) `, it follows that :eq:`fem:approx:Galerkin` is equivalent to .. _Eq:fem:approx:Galerkin0: .. math:: :label: fem:approx:Galerkin0 (e,{\psi}_i)=0,\quad i\in{\mathcal{I}_s}{\thinspace .} Inserting :math:`e=f-u` in this equation and ordering terms, as in the multi-dimensional vector case, we end up with a linear system with a coefficient matrix :eq:`fem:approx:Aij` and right-hand side vector :eq:`fem:approx:bi`. Whether we work with vectors in the plane, general vectors, or functions in function spaces, the least squares principle and the projection or Galerkin method are equivalent. .. _fem:approx:global:linear: Example: linear approximation ----------------------------- Let us apply the theory in the previous section to a simple problem: given a parabola :math:`f(x)=10(x-1)^2-1` for :math:`x\in\Omega=[1,2]`, find the best approximation :math:`u(x)` in the space of all linear functions: .. math:: V = \hbox{span}\,\{1, x\}{\thinspace .} With our notation, :math:`{\psi}_0(x)=1`, :math:`{\psi}_1(x)=x`, and :math:`N=1`. We seek .. math:: u=c_0{\psi}_0(x) + c_1{\psi}_1(x) = c_0 + c_1x, where :math:`c_0` and :math:`c_1` are found by solving a :math:`2\times 2` the linear system. The coefficient matrix has elements .. math:: A_{0,0} = ({\psi}_0,{\psi}_0) = \int_1^21\cdot 1\, {\, \mathrm{d}x} = 1, .. math:: A_{0,1} = ({\psi}_0,{\psi}_1) = \int_1^2 1\cdot x\, {\, \mathrm{d}x} = 3/2, .. math:: A_{1,0} = A_{0,1} = 3/2, .. math:: A_{1,1} = ({\psi}_1,{\psi}_1) = \int_1^2 x\cdot x\,{\, \mathrm{d}x} = 7/3{\thinspace .} The corresponding right-hand side is .. math:: b_1 = (f,{\psi}_0) = \int_1^2 (10(x-1)^2 - 1)\cdot 1 \, {\, \mathrm{d}x} = 7/3, .. math:: b_2 = (f,{\psi}_1) = \int_1^2 (10(x-1)^2 - 1)\cdot x\, {\, \mathrm{d}x} = 13/3{\thinspace .} Solving the linear system results in .. math:: c_0 = -38/3,\quad c_1 = 10, and consequently .. math:: u(x) = 10x - \frac{38}{3}{\thinspace .} Figure :ref:`fem:approx:global:fig:parabola:linear` displays the parabola and its best approximation in the space of all linear functions. .. _fem:approx:global:fig:parabola:linear: .. figure:: parabola_ls_linear.png :width: 400 *Best approximation of a parabola by a straight line* .. _fem:approx:global:LS:code: Implementation of the least squares method ------------------------------------------ Symbolic integration ~~~~~~~~~~~~~~~~~~~~ The linear system can be computed either symbolically or numerically (a numerical integration rule is needed in the latter case). Here is a function for symbolic computation of the linear system, where :math:`f(x)` is given as a ``sympy`` expression ``f`` involving the symbol ``x``, ``psi`` is a list of expressions for :math:`\left\{ {{\psi}}_i \right\}_{i\in{\mathcal{I}_s}}`, and ``Omega`` is a 2-tuple/list holding the limits of the domain :math:`\Omega`: .. code-block:: python import sympy as sp def least_squares(f, psi, Omega): N = len(psi) - 1 A = sp.zeros((N+1, N+1)) b = sp.zeros((N+1, 1)) x = sp.Symbol('x') for i in range(N+1): for j in range(i, N+1): A[i,j] = sp.integrate(psi[i]*psi[j], (x, Omega[0], Omega[1])) A[j,i] = A[i,j] b[i,0] = sp.integrate(psi[i]*f, (x, Omega[0], Omega[1])) c = A.LUsolve(b) u = 0 for i in range(len(psi)): u += c[i,0]*psi[i] return u, c Observe that we exploit the symmetry of the coefficient matrix: only the upper triangular part is computed. Symbolic integration in ``sympy`` is often time consuming, and (roughly) halving the work has noticeable effect on the waiting time for the function to finish execution. Fallback on numerical integration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Obviously, ``sympy`` mail fail to successfully integrate :math:`\int_\Omega{\psi}_i{\psi}_jdx` and especially :math:`\int_\Omega f{\psi}_idx` symbolically. Therefore, we should extend the ``least_squares`` function such that it falls back on numerical integration if the symbolic integration is unsuccessful. In the latter case, the returned value from ``sympy``'s ``integrate`` function is an object of type ``Integral``. We can test on this type and utilize the ``mpmath`` module in ``sympy`` to perform numerical integration of high precision. Even when ``sympy`` manages to integrate symbolically, it can take an undesirable long time. We therefore include an argument ``symbolic`` that governs whether or not to try symbolic integration. Here is the complete code of the improved version of function ``least_squares``: .. code-block:: python def least_squares(f, psi, Omega, symbolic=True): N = len(psi) - 1 A = sp.zeros((N+1, N+1)) b = sp.zeros((N+1, 1)) x = sp.Symbol('x') for i in range(N+1): for j in range(i, N+1): integrand = psi[i]*psi[j] if symbolic: I = sp.integrate(integrand, (x, Omega[0], Omega[1])) if not symbolic or isinstance(I, sp.Integral): # Could not integrate symbolically, # fall back on numerical integration integrand = sp.lambdify([x], integrand) I = sp.mpmath.quad(integrand, [Omega[0], Omega[1]]) A[i,j] = A[j,i] = I integrand = psi[i]*f if symbolic: I = sp.integrate(integrand, (x, Omega[0], Omega[1])) if not symbolic or isinstance(I, sp.Integral): integrand = sp.lambdify([x], integrand) I = sp.mpmath.quad(integrand, [Omega[0], Omega[1]]) b[i,0] = I c = A.LUsolve(b) # symbolic solve # c is a sympy Matrix object, numbers are in c[i,0] u = sum(c[i,0]*psi[i] for i in range(len(psi))) return u, [c[i,0] for i in range(len(c))] The function is found in the file ``approx1D.py``. Plotting the approximation ~~~~~~~~~~~~~~~~~~~~~~~~~~ Comparing the given :math:`f(x)` and the approximate :math:`u(x)` visually is done by the following function, which with the aid of ``sympy``'s ``lambdify`` tool converts a ``sympy`` expression to a Python function for numerical computations: .. code-block:: python def comparison_plot(f, u, Omega, filename='tmp.pdf'): x = sp.Symbol('x') f = sp.lambdify([x], f, modules="numpy") u = sp.lambdify([x], u, modules="numpy") resolution = 401 # no of points in plot xcoor = linspace(Omega[0], Omega[1], resolution) exact = f(xcoor) approx = u(xcoor) plot(xcoor, approx) hold('on') plot(xcoor, exact) legend(['approximation', 'exact']) savefig(filename) The ``modules='numpy'`` argument to ``lambdify`` is important if there are mathematical functions, such as ``sin`` or ``exp`` in the symbolic expressions in ``f`` or ``u``, and these mathematical functions are to be used with vector arguments, like ``xcoor`` above. Both the ``least_squares`` and ``comparison_plot`` are found and coded in the file `approx1D.py `__. The forthcoming examples on their use appear in ``ex_approx1D.py``. .. _fem:approx:global:exact: Perfect approximation --------------------- Let us use the code above to recompute the problem from the section :ref:`fem:approx:global:linear` where we want to approximate a parabola. What happens if we add an element :math:`x^2` to the basis and test what the best approximation is if :math:`V` is the space of all parabolic functions? The answer is quickly found by running .. code-block:: python >>> from approx1D import * >>> x = sp.Symbol('x') >>> f = 10*(x-1)**2-1 >>> u, c = least_squares(f=f, psi=[1, x, x**2], Omega=[1, 2]) >>> print u 10*x**2 - 20*x + 9 >>> print sp.expand(f) 10*x**2 - 20*x + 9 Now, what if we use :math:`{\psi}_i(x)=x^i` for :math:`i=0,1,\ldots,N=40`? The output from ``least_squares`` gives :math:`c_i=0` for :math:`i>2`, which means that the method finds the perfect approximation. In fact, we have a general result that if :math:`f\in V`, the least squares and projection/Galerkin methods compute the exact solution :math:`u=f`. The proof is straightforward: if :math:`f\in V`, :math:`f` can be expanded in terms of the basis functions, :math:`f=\sum_{j\in{\mathcal{I}_s}} d_j{\psi}_j`, for some coefficients :math:`\left\{ {d}_i \right\}_{i\in{\mathcal{I}_s}}`, and the right-hand side then has entries .. math:: b_i = (f,{\psi}_i) = \sum_{j\in{\mathcal{I}_s}} d_j({\psi}_j, {\psi}_i) = \sum_{j\in{\mathcal{I}_s}} d_jA_{i,j} {\thinspace .} The linear system :math:`\sum_jA_{i,j}c_j = b_i`, :math:`i\in{\mathcal{I}_s}`, is then .. math:: \sum_{j\in{\mathcal{I}_s}} c_jA_{i,j} = \sum_{j\in{\mathcal{I}_s}}d_jA_{i,j}, \quad i\in{\mathcal{I}_s}, which implies that :math:`c_i=d_i` for :math:`i\in{\mathcal{I}_s}`. .. _fem:approx:global:illconditioning: Ill-conditioning ---------------- The computational example in the section :ref:`fem:approx:global:exact` applies the ``least_squares`` function which invokes symbolic methods to calculate and solve the linear system. The correct solution :math:`c_0=9, c_1=-20, c_2=10, c_i=0` for :math:`i\geq 3` is perfectly recovered. Suppose we convert the matrix and right-hand side to floating-point arrays and then solve the system using finite-precision arithmetics, which is what one will (almost) always do in real life. This time we get astonishing results! Up to about :math:`N=7` we get a solution that is reasonably close to the exact one. Increasing :math:`N` shows that seriously wrong coefficients are computed. Below is a table showing the solution of the linear system arising from approximating a parabola by functions on the form :math:`u(x)=c_0 + c_1x + c_2x^2 + \cdots + c_{10}x^{10}`. Analytically, we know that :math:`c_j=0` for :math:`j>2`, but numerically we may get :math:`c_j\neq 0` for :math:`j>2`. ===== ========= =========== =========== exact ``sympy`` ``numpy32`` ``numpy64`` ===== ========= =========== =========== 9 9.62 5.57 8.98 -20 -23.39 -7.65 -19.93 10 17.74 -4.50 9.96 0 -9.19 4.13 -0.26 0 5.25 2.99 0.72 0 0.18 -1.21 -0.93 0 -2.48 -0.41 0.73 0 1.81 -0.013 -0.36 0 -0.66 0.08 0.11 0 0.12 0.04 -0.02 0 -0.001 -0.02 0.002 ===== ========= =========== =========== The exact value of :math:`c_j`, :math:`j=0,1,\ldots,10`, appears in the first column while the other columns correspond to results obtained by three different methods: * Column 2: The matrix and vector are converted to the data structure ``sympy.mpmath.fp.matrix`` and the ``sympy.mpmath.fp.lu_solve`` function is used to solve the system. * Column 3: The matrix and vector are converted to ``numpy`` arrays with data type ``numpy.float32`` (single precision floating-point number) and solved by the ``numpy.linalg.solve`` function. * Column 4: As column 3, but the data type is ``numpy.float64`` (double precision floating-point number). We see from the numbers in the table that double precision performs much better than single precision. Nevertheless, when plotting all these solutions the curves cannot be visually distinguished (!). This means that the approximations look perfect, despite the partially very wrong values of the coefficients. Increasing :math:`N` to 12 makes the numerical solver in ``numpy`` abort with the message: "matrix is numerically singular". A matrix has to be non-singular to be invertible, which is a requirement when solving a linear system. Already when the matrix is close to singular, it is *ill-conditioned*, which here implies that the numerical solution algorithms are sensitive to round-off errors and may produce (very) inaccurate results. The reason why the coefficient matrix is nearly singular and ill-conditioned is that our basis functions :math:`{\psi}_i(x)=x^i` are nearly linearly dependent for large :math:`i`. That is, :math:`x^i` and :math:`x^{i+1}` are very close for :math:`i` not very small. This phenomenon is illustrated in Figure :ref:`fem:approx:global:fig:illconditioning`. There are 15 lines in this figure, but only half of them are visually distinguishable. Almost linearly dependent basis functions give rise to an ill-conditioned and almost singular matrix. This fact can be illustrated by computing the determinant, which is indeed very close to zero (recall that a zero determinant implies a singular and non-invertible matrix): :math:`10^{-65}` for :math:`N=10` and :math:`10^{-92}` for :math:`N=12`. Already for :math:`N=28` the numerical determinant computation returns a plain zero. .. _fem:approx:global:fig:illconditioning: .. figure:: ill_conditioning.png :width: 600 The 15 first basis functions :math:`x^i`, :math:`i=0,\ldots,14` On the other hand, the double precision ``numpy`` solver do run for :math:`N=100`, resulting in answers that are not significantly worse than those in the table above, and large powers are associated with small coefficients (e.g., :math:`c_j<10^{-2}` for :math:`10\leq j\leq 20` and :math:`c<10^{-5}` for :math:`j>20`). Even for :math:`N=100` the approximation still lies on top of the exact curve in a plot (!). The conclusion is that visual inspection of the quality of the approximation may not uncover fundamental numerical problems with the computations. However, numerical analysts have studied approximations and ill-conditioning for decades, and it is well known that the basis :math:`\{1,x,x^2,x^3,\ldots,\}` is a bad basis. The best basis from a matrix conditioning point of view is to have orthogonal functions such that :math:`(\psi_i,\psi_j)=0` for :math:`i\neq j`. There are many known sets of orthogonal polynomials and other functions. The functions used in the finite element methods are almost orthogonal, and this property helps to avoid problems with solving matrix systems. Almost orthogonal is helpful, but not enough when it comes to partial differential equations, and ill-conditioning of the coefficient matrix is a theme when solving large-scale matrix systems arising from finite element discretizations. .. _fem:approx:global:Fourier: Fourier series -------------- .. index:: single: approximation; by sines A set of sine functions is widely used for approximating functions (the sines are also orthogonal as explained more in the section :ref:`fem:approx:global:illconditioning`). Let us take .. math:: V = \hbox{span}\,\{ \sin \pi x, \sin 2\pi x,\ldots,\sin (N+1)\pi x\} {\thinspace .} That is, .. math:: {\psi}_i(x) = \sin ((i+1)\pi x),\quad i\in{\mathcal{I}_s}{\thinspace .} An approximation to the :math:`f(x)` function from the section :ref:`fem:approx:global:linear` can then be computed by the ``least_squares`` function from the section :ref:`fem:approx:global:LS:code`: .. code-block:: python N = 3 from sympy import sin, pi x = sp.Symbol('x') psi = [sin(pi*(i+1)*x) for i in range(N+1)] f = 10*(x-1)**2 - 1 Omega = [0, 1] u, c = least_squares(f, psi, Omega) comparison_plot(f, u, Omega) Figure :ref:`fem:approx:global:fig:parabola:sine1` (left) shows the oscillatory approximation of :math:`\sum_{j=0}^Nc_j\sin ((j+1)\pi x)` when :math:`N=3`. Changing :math:`N` to 11 improves the approximation considerably, see Figure :ref:`fem:approx:global:fig:parabola:sine1` (right). .. _fem:approx:global:fig:parabola:sine1: .. figure:: parabola_ls_sines4_12.png :width: 800 *Best approximation of a parabola by a sum of 3 (left) and 11 (right) sine functions* There is an error :math:`f(0)-u(0)=9` at :math:`x=0` in Figure :ref:`fem:approx:global:fig:parabola:sine1` regardless of how large :math:`N` is, because all :math:`{\psi}_i(0)=0` and hence :math:`u(0)=0`. We may help the approximation to be correct at :math:`x=0` by seeking .. math:: u(x) = f(0) + \sum_{j\in{\mathcal{I}_s}} c_j{\psi}_j(x) {\thinspace .} However, this adjustment introduces a new problem at :math:`x=1` since we now get an error :math:`f(1)-u(1)=f(1)-0=-1` at this point. A more clever adjustment is to replace the :math:`f(0)` term by a term that is :math:`f(0)` at :math:`x=0` and :math:`f(1)` at :math:`x=1`. A simple linear combination :math:`f(0)(1-x) + xf(1)` does the job: .. math:: u(x) = f(0)(1-x) + xf(1) + \sum_{j\in{\mathcal{I}_s}} c_j{\psi}_j(x) {\thinspace .} This adjustment of :math:`u` alters the linear system slightly. In the general case, we set .. math:: u(x) = B(x) + \sum_{j\in{\mathcal{I}_s}} c_j{\psi}_j(x), and the linear system becomes .. math:: \sum_{j\in{\mathcal{I}_s}}({\psi}_i,{\psi}_j)c_j = (f-B,{\psi}_i),\quad i\in{\mathcal{I}_s}{\thinspace .} The calculations can still utilize the ``least_squares`` or ``least_squares_orth`` functions, but solve for :math:`u-b`: .. code-block:: python f0 = 0; f1 = -1 B = f0*(1-x) + x*f1 u_sum, c = least_squares_orth(f-b, psi, Omega) u = B + u_sum Figure :ref:`fem:approx:global:fig:parabola:sine2` shows the result of the technique for ensuring right boundary values. Even 3 sines can now adjust the :math:`f(0)(1-x) + xf(1)` term such that :math:`u` approximates the parabola really well, at least visually. .. _fem:approx:global:fig:parabola:sine2: .. figure:: parabola_ls_sines4_12_wfterm.png :width: 800 *Best approximation of a parabola by a sum of 3 (left) and 11 (right) sine functions with a boundary term* .. _fem:approx:global:orth: Orthogonal basis functions -------------------------- The choice of sine functions :math:`{\psi}_i(x)=\sin ((i+1)\pi x)` has a great computational advantage: on :math:`\Omega=[0,1]` these basis functions are *orthogonal*, implying that :math:`A_{i,j}=0` if :math:`i\neq j`. This result is realized by trying .. code-block:: python integrate(sin(j*pi*x)*sin(k*pi*x), x, 0, 1) in `WolframAlpha `__ (avoid ``i`` in the integrand as this symbol means the imaginary unit :math:`\sqrt{-1}`). Also by asking WolframAlpha about :math:`\int_0^1\sin^2 (j\pi x) {\, \mathrm{d}x}`, we find it to equal 1/2. With a diagonal matrix we can easily solve for the coefficients by hand: .. math:: c_i = 2\int_0^1 f(x)\sin ((i+1)\pi x) {\, \mathrm{d}x},\quad i\in{\mathcal{I}_s}, which is nothing but the classical formula for the coefficients of the Fourier sine series of :math:`f(x)` on :math:`[0,1]`. In fact, when :math:`V` contains the basic functions used in a Fourier series expansion, the approximation method derived in the section :ref:`fem:approx:global` results in the classical Fourier series for :math:`f(x)` (see :ref:`fem:approx:exer:Fourier` for details). With orthogonal basis functions we can make the ``least_squares`` function (much) more efficient since we know that the matrix is diagonal and only the diagonal elements need to be computed: .. code-block:: python def least_squares_orth(f, psi, Omega): N = len(psi) - 1 A = [0]*(N+1) b = [0]*(N+1) x = sp.Symbol('x') for i in range(N+1): A[i] = sp.integrate(psi[i]**2, (x, Omega[0], Omega[1])) b[i] = sp.integrate(psi[i]*f, (x, Omega[0], Omega[1])) c = [b[i]/A[i] for i in range(len(b))] u = 0 for i in range(len(psi)): u += c[i]*psi[i] return u, c As mentioned in the section :ref:`fem:approx:global:LS:code`, symbolic integration may fail or take very long time. It is therefore natural to extend the implementation above with a version where we can choose between symbolic and numerical integration and fall back on the latter if the former fails: .. code-block:: python def least_squares_orth(f, psi, Omega, symbolic=True): N = len(psi) - 1 A = [0]*(N+1) # plain list to hold symbolic expressions b = [0]*(N+1) x = sp.Symbol('x') for i in range(N+1): # Diagonal matrix term A[i] = sp.integrate(psi[i]**2, (x, Omega[0], Omega[1])) # Right-hand side term integrand = psi[i]*f if symbolic: I = sp.integrate(integrand, (x, Omega[0], Omega[1])) if not symbolic or isinstance(I, sp.Integral): print 'numerical integration of', integrand integrand = sp.lambdify([x], integrand) I = sp.mpmath.quad(integrand, [Omega[0], Omega[1]]) b[i] = I c = [b[i]/A[i] for i in range(len(b))] u = 0 u = sum(c[i,0]*psi[i] for i in range(len(psi))) return u, c This function is found in the file ``approx1D.py``. Observe that we here assume that :math:`\int_\Omega{\varphi}_i^2dx` can always be symbolically computed, which is not an unreasonable assumption when the basis functions are orthogonal, but there is no guarantee, so an improved version of the function above would implement numerical integration also for the ``A[i,i]`` term. Numerical computations ---------------------- Sometimes the basis functions :math:`{\psi}_i` and/or the function :math:`f` have a nature that makes symbolic integration CPU-time consuming or impossible. Even though we implemented a fallback on numerical integration of :math:`\int f{\varphi}_i dx` considerable time might be required by ``sympy`` in the attempt to integrate symbolically. Therefore, it will be handy to have function for fast *numerical integration and numerical solution of the linear system*. Below is such a method. It requires Python functions ``f(x)`` and ``psi(x,i)`` for :math:`f(x)` and :math:`{\psi}_i(x)` as input. The output is a mesh function with values ``u`` on the mesh with points in the array ``x``. Three numerical integration methods are offered: ``scipy.integrate.quad`` (precision set to :math:`10^{-8}`), ``sympy.mpmath.quad`` (high precision), and a Trapezoidal rule based on the points in ``x``. .. code-block:: python def least_squares_numerical(f, psi, N, x, integration_method='scipy', orthogonal_basis=False): import scipy.integrate A = np.zeros((N+1, N+1)) b = np.zeros(N+1) Omega = [x[0], x[-1]] dx = x[1] - x[0] for i in range(N+1): j_limit = i+1 if orthogonal_basis else N+1 for j in range(i, j_limit): print '(%d,%d)' % (i, j) if integration_method == 'scipy': A_ij = scipy.integrate.quad( lambda x: psi(x,i)*psi(x,j), Omega[0], Omega[1], epsabs=1E-9, epsrel=1E-9)[0] elif integration_method == 'sympy': A_ij = sp.mpmath.quad( lambda x: psi(x,i)*psi(x,j), [Omega[0], Omega[1]]) else: values = psi(x,i)*psi(x,j) A_ij = trapezoidal(values, dx) A[i,j] = A[j,i] = A_ij if integration_method == 'scipy': b_i = scipy.integrate.quad( lambda x: f(x)*psi(x,i), Omega[0], Omega[1], epsabs=1E-9, epsrel=1E-9)[0] elif integration_method == 'sympy': b_i = sp.mpmath.quad( lambda x: f(x)*psi(x,i), [Omega[0], Omega[1]]) else: values = f(x)*psi(x,i) b_i = trapezoidal(values, dx) b[i] = b_i c = b/np.diag(A) if orthogonal_basis else np.linalg.solve(A, b) u = sum(c[i]*psi(x, i) for i in range(N+1)) return u, c def trapezoidal(values, dx): """Integrate values by the Trapezoidal rule (mesh size dx).""" return dx*(np.sum(values) - 0.5*values[0] - 0.5*values[-1]) Here is an example on calling the function: .. code-block:: python from numpy import linspace, tanh, pi def psi(x, i): return sin((i+1)*x) x = linspace(0, 2*pi, 501) N = 20 u, c = least_squares_numerical(lambda x: tanh(x-pi), psi, N, x, orthogonal_basis=True) .. _fem:approx:global:interp: The interpolation (or collocation) method ----------------------------------------- .. index:: collocation method (approximation) .. index:: single: approximation; collocation The principle of minimizing the distance between :math:`u` and :math:`f` is an intuitive way of computing a best approximation :math:`u\in V` to :math:`f`. However, there are other approaches as well. One is to demand that :math:`u(x_{i}) = f(x_{i})` at some selected points :math:`x_{i}`, :math:`i\in{\mathcal{I}_s}`: .. math:: u(x_{i}) = \sum_{j\in{\mathcal{I}_s}} c_j {\psi}_j(x_{i}) = f(x_{i}), \quad i\in{\mathcal{I}_s}{\thinspace .} This criterion also gives a linear system with :math:`N+1` unknown coefficients :math:`\left\{ {c}_i \right\}_{i\in{\mathcal{I}_s}}`: .. math:: \sum_{j\in{\mathcal{I}_s}} A_{i,j}c_j = b_i,\quad i\in{\mathcal{I}_s}, with .. math:: A_{i,j} = {\psi}_j(x_{i}), .. math:: b_i = f(x_{i}){\thinspace .} This time the coefficient matrix is not symmetric because :math:`{\psi}_j(x_{i})\neq {\psi}_i(x_{j})` in general. The method is often referred to as an *interpolation method* since some point values of :math:`f` are given (:math:`f(x_{i})`) and we fit a continuous function :math:`u` that goes through the :math:`f(x_{i})` points. In this case the :math:`x_{i}` points are called *interpolation points*. When the same approach is used to approximate differential equations, one usually applies the name *collocation method* and :math:`x_{i}` are known as *collocation points*. .. index:: interpolation .. index:: single: approximation; interpolation Given :math:`f` as a ``sympy`` symbolic expression ``f``, :math:`\left\{ {{\psi}}_i \right\}_{i\in{\mathcal{I}_s}}` as a list ``psi``, and a set of points :math:`\left\{ {x}_i \right\}_{i\in{\mathcal{I}_s}}` as a list or array ``points``, the following Python function sets up and solves the matrix system for the coefficients :math:`\left\{ {c}_i \right\}_{i\in{\mathcal{I}_s}}`: .. code-block:: python def interpolation(f, psi, points): N = len(psi) - 1 A = sp.zeros((N+1, N+1)) b = sp.zeros((N+1, 1)) x = sp.Symbol('x') # Turn psi and f into Python functions psi = [sp.lambdify([x], psi[i]) for i in range(N+1)] f = sp.lambdify([x], f) for i in range(N+1): for j in range(N+1): A[i,j] = psi[j](points[i]) b[i,0] = f(points[i]) c = A.LUsolve(b) u = 0 for i in range(len(psi)): u += c[i,0]*psi[i](x) return u The ``interpolation`` function is a part of the ``approx1D`` module. We found it convenient in the above function to turn the expressions ``f`` and ``psi`` into ordinary Python functions of ``x``, which can be called with ``float`` values in the list ``points`` when building the matrix and the right-hand side. The alternative is to use the ``subs`` method to substitute the ``x`` variable in an expression by an element from the ``points`` list. The following session illustrates both approaches in a simple setting: .. code-block:: ipy >>> from sympy import * >>> x = Symbol('x') >>> e = x**2 # symbolic expression involving x >>> p = 0.5 # a value of x >>> v = e.subs(x, p) # evaluate e for x=p >>> v 0.250000000000000 >>> type(v) sympy.core.numbers.Float >>> e = lambdify([x], e) # make Python function of e >>> type(e) >>> function >>> v = e(p) # evaluate e(x) for x=p >>> v 0.25 >>> type(v) float A nice feature of the interpolation or collocation method is that it avoids computing integrals. However, one has to decide on the location of the :math:`x_{i}` points. A simple, yet common choice, is to distribute them uniformly throughout :math:`\Omega`. Example (1) ~~~~~~~~~~~~ Let us illustrate the interpolation or collocation method by approximating our parabola :math:`f(x)=10(x-1)^2-1` by a linear function on :math:`\Omega=[1,2]`, using two collocation points :math:`x_0=1+1/3` and :math:`x_1=1+2/3`: .. code-block:: python f = 10*(x-1)**2 - 1 psi = [1, x] Omega = [1, 2] points = [1 + sp.Rational(1,3), 1 + sp.Rational(2,3)] u = interpolation(f, psi, points) comparison_plot(f, u, Omega) The resulting linear system becomes .. math:: \left(\begin{array}{ll} 1 & 4/3\\ 1 & 5/3\\ \end{array}\right) \left(\begin{array}{l} c_0\\ c_1\\ \end{array}\right) = \left(\begin{array}{l} 1/9\\ 31/9\\ \end{array}\right) with solution :math:`c_0=-119/9` and :math:`c_1=10`. Figure :ref:`fem:approx:global:linear:interp:fig1` (left) shows the resulting approximation :math:`u=-119/9 + 10x`. We can easily test other interpolation points, say :math:`x_0=1` and :math:`x_1=2`. This changes the line quite significantly, see Figure :ref:`fem:approx:global:linear:interp:fig1` (right). .. _fem:approx:global:linear:interp:fig1: .. figure:: parabola_inter.png :width: 800 *Approximation of a parabola by linear functions computed by two interpolation points: 4/3 and 5/3 (left) versus 1 and 2 (right)* .. _fem:approx:global:Lagrange: Lagrange polynomials -------------------- .. index:: Lagrange (interpolating) polynomial In the section :ref:`fem:approx:global:Fourier` we explain the advantage with having a diagonal matrix: formulas for the coefficients :math:`\left\{ {c}_i \right\}_{i\in{\mathcal{I}_s}}` can then be derived by hand. For an interpolation/collocation method a diagonal matrix implies that :math:`{\psi}_j(x_{i}) = 0` if :math:`i\neq j`. One set of basis functions :math:`{\psi}_i(x)` with this property is the *Lagrange interpolating polynomials*, or just *Lagrange polynomials*. (Although the functions are named after Lagrange, they were first discovered by Waring in 1779, rediscovered by Euler in 1783, and published by Lagrange in 1795.) The Lagrange polynomials have the form .. _Eq:fem:approx:global:Lagrange:poly: .. math:: :label: fem:approx:global:Lagrange:poly {\psi}_i(x) = \prod_{j=0,j\neq i}^N \frac{x-x_{j}}{x_{i}-x_{j}} = \frac{x-x_0}{x_{i}-x_0}\cdots\frac{x-x_{i-1}}{x_{i}-x_{i-1}}\frac{x-x_{i+1}}{x_{i}-x_{i+1}} \cdots\frac{x-x_N}{x_{i}-x_N}, for :math:`i\in{\mathcal{I}_s}`. We see from :eq:`fem:approx:global:Lagrange:poly` that all the :math:`{\psi}_i` functions are polynomials of degree :math:`N` which have the property .. index:: Kronecker delta .. _Eq:fem:inter:prop: .. math:: :label: fem:inter:prop {\psi}_i(x_s) = \delta_{is},\quad \delta_{is} = \left\lbrace\begin{array}{ll} 1, & i=s,\\ 0, & i\neq s, \end{array}\right. when :math:`x_s` is an interpolation/collocation point. Here we have used the *Kronecker delta* symbol :math:`\delta_{is}`. This property implies that :math:`A_{i,j}=0` for :math:`i\neq j` and :math:`A_{i,j}=1` when :math:`i=j`. The solution of the linear system is them simply .. math:: c_i = f(x_{i}),\quad i\in{\mathcal{I}_s}, and .. math:: u(x) = \sum_{j\in{\mathcal{I}_s}} f(x_{i}){\psi}_i(x){\thinspace .} The following function computes the Lagrange interpolating polynomial :math:`{\psi}_i(x)`, given the interpolation points :math:`x_{0},\ldots,x_{N}` in the list or array ``points``: .. code-block:: python def Lagrange_polynomial(x, i, points): p = 1 for k in range(len(points)): if k != i: p *= (x - points[k])/(points[i] - points[k]) return p The next function computes a complete basis using equidistant points throughout :math:`\Omega`: .. code-block:: python def Lagrange_polynomials_01(x, N): if isinstance(x, sp.Symbol): h = sp.Rational(1, N-1) else: h = 1.0/(N-1) points = [i*h for i in range(N)] psi = [Lagrange_polynomial(x, i, points) for i in range(N)] return psi, points When ``x`` is an ``sp.Symbol`` object, we let the spacing between the interpolation points, ``h``, be a ``sympy`` rational number for nice end results in the formulas for :math:`{\psi}_i`. The other case, when ``x`` is a plain Python ``float``, signifies numerical computing, and then we let ``h`` be a floating-point number. Observe that the ``Lagrange_polynomial`` function works equally well in the symbolic and numerical case - just think of ``x`` being an ``sp.Symbol`` object or a Python ``float``. A little interactive session illustrates the difference between symbolic and numerical computing of the basis functions and points: .. code-block:: ipy >>> import sympy as sp >>> x = sp.Symbol('x') >>> psi, points = Lagrange_polynomials_01(x, N=3) >>> points [0, 1/2, 1] >>> psi [(1 - x)*(1 - 2*x), 2*x*(2 - 2*x), -x*(1 - 2*x)] >>> x = 0.5 # numerical computing >>> psi, points = Lagrange_polynomials_01(x, N=3) >>> points [0.0, 0.5, 1.0] >>> psi [-0.0, 1.0, 0.0] The Lagrange polynomials are very much used in finite element methods because of their property :eq:`fem:inter:prop`. Approximation of a polynomial ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The Galerkin or least squares method lead to an exact approximation if :math:`f` lies in the space spanned by the basis functions. It could be interest to see how the interpolation method with Lagrange polynomials as basis is able to approximate a polynomial, e.g., a parabola. Running .. code-block:: python for N in 2, 4, 5, 6, 8, 10, 12: f = x**2 psi, points = Lagrange_polynomials_01(x, N) u = interpolation(f, psi, points) shows the result that up to ``N=4`` we achieve an exact approximation, and then round-off errors start to grow, such that ``N=15`` leads to a 15-degree polynomial for :math:`u` where the coefficients in front of :math:`x^r` for :math:`r>2` are of size :math:`10^{-5}` and smaller. Successful example ~~~~~~~~~~~~~~~~~~ Trying out the Lagrange polynomial basis for approximating :math:`f(x)=\sin 2\pi x` on :math:`\Omega =[0,1]` with the least squares and the interpolation techniques can be done by .. code-block:: python x = sp.Symbol('x') f = sp.sin(2*sp.pi*x) psi, points = Lagrange_polynomials_01(x, N) Omega=[0, 1] u, c = least_squares(f, psi, Omega) comparison_plot(f, u, Omega) u, c = interpolation(f, psi, points) comparison_plot(f, u, Omega) Figure :ref:`fem:approx:global:Lagrange:fig:sine:ls:colloc` shows the results. There is little difference between the least squares and the interpolation technique. Increasing :math:`N` gives visually better approximations. .. _fem:approx:global:Lagrange:fig:sine:ls:colloc: .. figure:: Lagrange_ls_interp_sin_4.png :width: 800 *Approximation via least squares (left) and interpolation (right) of a sine function by Lagrange interpolating polynomials of degree 3* .. index:: Runge's phenomenon Less successful example ~~~~~~~~~~~~~~~~~~~~~~~ The next example concerns interpolating :math:`f(x)=|1-2x|` on :math:`\Omega =[0,1]` using Lagrange polynomials. Figure :ref:`fem:approx:global:Lagrange:fig:abs:Lag:unif:7:14` shows a peculiar effect: the approximation starts to oscillate more and more as :math:`N` grows. This numerical artifact is not surprising when looking at the individual Lagrange polynomials. Figure :ref:`fem:approx:global:Lagrange:fig:abs:Lag:unif:osc` shows two such polynomials, :math:`\psi_2(x)` and :math:`\psi_7(x)`, both of degree 11 and computed from uniformly spaced points :math:`x_{x_i}=i/11`, :math:`i=0,\ldots,11`, marked with circles. We clearly see the property of Lagrange polynomials: :math:`\psi_2(x_{i})=0` and :math:`\psi_7(x_{i})=0` for all :math:`i`, except :math:`\psi_2(x_{2})=1` and :math:`\psi_7(x_{7})=1`. The most striking feature, however, is the significant oscillation near the boundary. The reason is easy to understand: since we force the functions to zero at so many points, a polynomial of high degree is forced to oscillate between the points. The phenomenon is named *Runge's phenomenon* and you can read a more detailed explanation on `Wikipedia `__. .. index:: Chebyshev nodes Remedy for strong oscillations ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The oscillations can be reduced by a more clever choice of interpolation points, called the *Chebyshev nodes*: .. math:: x_{i} = \frac{1}{2} (a+b) + \frac{1}{2}(b-a)\cos\left( \frac{2i+1}{2(N+1)}pi\right),\quad i=0\ldots,N, on the interval :math:`\Omega = [a,b]`. Here is a flexible version of the ``Lagrange_polynomials_01`` function above, valid for any interval :math:`\Omega =[a,b]` and with the possibility to generate both uniformly distributed points and Chebyshev nodes: .. code-block:: python def Lagrange_polynomials(x, N, Omega, point_distribution='uniform'): if point_distribution == 'uniform': if isinstance(x, sp.Symbol): h = sp.Rational(Omega[1] - Omega[0], N) else: h = (Omega[1] - Omega[0])/float(N) points = [Omega[0] + i*h for i in range(N+1)] elif point_distribution == 'Chebyshev': points = Chebyshev_nodes(Omega[0], Omega[1], N) psi = [Lagrange_polynomial(x, i, points) for i in range(N+1)] return psi, points def Chebyshev_nodes(a, b, N): from math import cos, pi return [0.5*(a+b) + 0.5*(b-a)*cos(float(2*i+1)/(2*N+1))*pi) \ for i in range(N+1)] All the functions computing Lagrange polynomials listed above are found in the module file ``Lagrange.py``. Figure :ref:`fem:approx:global:Lagrange:fig:abs:Lag:Cheb:7:14` shows the improvement of using Chebyshev nodes (compared with Figure :ref:`fem:approx:global:Lagrange:fig:abs:Lag:unif:7:14`). The reason is that the corresponding Lagrange polynomials have much smaller oscillations as seen in Figure :ref:`fem:approx:global:Lagrange:fig:abs:Lag:Cheb:osc` (compare with Figure :ref:`fem:approx:global:Lagrange:fig:abs:Lag:unif:osc`). Another cure for undesired oscillation of higher-degree interpolating polynomials is to use lower-degree Lagrange polynomials on many small patches of the domain, which is the idea pursued in the finite element method. For instance, linear Lagrange polynomials on :math:`[0,1/2]` and :math:`[1/2,1]` would yield a perfect approximation to :math:`f(x)=|1-2x|` on :math:`\Omega = [0,1]` since :math:`f` is piecewise linear. .. _fem:approx:global:Lagrange:fig:abs:Lag:unif:7:14: .. figure:: Lagrange_interp_abs_8_15.png :width: 800 *Interpolation of an absolute value function by Lagrange polynomials and uniformly distributed interpolation points: degree 7 (left) and 14 (right)* .. _fem:approx:global:Lagrange:fig:abs:Lag:unif:osc: .. figure:: Lagrange_basis_12.png :width: 400 *Illustration of the oscillatory behavior of two Lagrange polynomials based on 12 uniformly spaced points (marked by circles)* .. _fem:approx:global:Lagrange:fig:abs:Lag:Cheb:7:14: .. figure:: Lagrange_interp_abs_Cheb_8_15.png :width: 800 *Interpolation of an absolute value function by Lagrange polynomials and Chebyshev nodes as interpolation points: degree 7 (left) and 14 (right)* .. _fem:approx:global:Lagrange:fig:abs:Lag:Cheb:osc: .. figure:: Lagrange_basis_Cheb_12.png :width: 400 *Illustration of the less oscillatory behavior of two Lagrange polynomials based on 12 Chebyshev points (marked by circles)* How does the least squares or projection methods work with Lagrange polynomials? We can just call the ``least_squares`` function, but ``sympy`` has problems integrating the :math:`f(x)=|1-2x|` function times a polynomial, so we need to fallback on numerical integration. .. code-block:: python def least_squares(f, psi, Omega): N = len(psi) - 1 A = sp.zeros((N+1, N+1)) b = sp.zeros((N+1, 1)) x = sp.Symbol('x') for i in range(N+1): for j in range(i, N+1): integrand = psi[i]*psi[j] I = sp.integrate(integrand, (x, Omega[0], Omega[1])) if isinstance(I, sp.Integral): # Could not integrate symbolically, fallback # on numerical integration with mpmath.quad integrand = sp.lambdify([x], integrand) I = sp.mpmath.quad(integrand, [Omega[0], Omega[1]]) A[i,j] = A[j,i] = I integrand = psi[i]*f I = sp.integrate(integrand, (x, Omega[0], Omega[1])) if isinstance(I, sp.Integral): integrand = sp.lambdify([x], integrand) I = sp.mpmath.quad(integrand, [Omega[0], Omega[1]]) b[i,0] = I c = A.LUsolve(b) u = 0 for i in range(len(psi)): u += c[i,0]*psi[i] return u .. Convergence of Lagrange polynomials.