Download this page as a Matlab LiveScript

Table of contents

Video lectures

Solving differential equations using Galerkin

Piecewise approximation

Solving differential equations using Galerkin

Example

{d2ud  x=x2for  x[0,1]u(0)=1  d  ud  x(1)=2   \def\arraystretch{2.0} \left\lbrace \begin{array}{ll} -\dfrac{d^2 u}{d\;x}=x^2 & \textrm{for}\;x\in \left\lbrack 0,1\right\rbrack \\ u\left(0\right)=-1 & \;\\ \dfrac{d\;u}{d\;x}\left(1\right)=-2 & \; \end{array}\right.

Weak form:

01d2ud  x  v  dx=01x2v  dx\int_0^1 -\dfrac{d^2 u}{d\;x}\;v\;dx=\int_0^1 x^2 v \;dx
01d  udx  d  vd  x  d  x=01x2v  dx+[d  ud  x(x)  v(x)]01\Rightarrow \int_0^1 \dfrac{d\;u}{dx}\;\dfrac{d\;v}{d\;x}\;d\;x=\int_0^1 x^2 v \; dx + {\left\lbrack \dfrac{d\;u}{d\;x}\left(x\right)\;v\left(x\right)\right\rbrack }_0^1

Approximation using Galerkin

du1=d  ud  x(0),    du2=d  ud  x(1)=2,  u1=1\textrm{du1}=\dfrac{d\;u}{d\;x}\left(0\right),\;\;\textrm{du2}=\dfrac{d\;u}{d\;x}\left(1\right)=-2,\;u_1 =-1

Approximate using one quadratic element on the whole domain

Quadratic basis functions: For x[0,1]  x\in \left\lbrack 0,1\right\rbrack \; we have

φ1=2(x1)(x1/2)φ2=4x(x1)φ3=2x(x1/2) \def\arraystretch{1.5} \begin{array}{l} \varphi_1 =2\left(x-1\right)\left(x-1/2\right)\\ \varphi_2 =-4x\left(x-1\right)\\ \varphi_3 =2x\left(x-1/2\right) \end{array}

The solution is then approximated by

U(x)=i=13ui  φiU\left(x\right)=\sum_{i=1}^3 u_i {\;\varphi }_i

and the derivate is given by

d  Ud  x=dd  x(  ui  φi(x))=  ui  d  φid  x\dfrac{d\;U}{d\;x}=\dfrac{d}{d\;x}\left(\sum_{\;} u_{i\;} \varphi_i \left(x\right)\right)=\sum_{\;} u_i \;\dfrac{d{\;\varphi }_i }{d\;x}

Insert the approximation into the weak form, replacing v  v\;with φ\varphi and d  vd  x\frac{d\;v}{d\;x} with d  φd  x\frac{d\;\varphi }{d\;x}.

01  d  φid  x  ui  d  φjd  x  d  x=01x2  φj  d  x+d  ud  x(1)φ(1)2\int_0^1 \sum_{\;} \dfrac{d\;\varphi_i }{d\;x}\;u_i \;\dfrac{d\;\varphi_j }{d\;x}\;d\;x=\int_0^1 x^2 \;\varphi_j \;d\;x+\underset{-2}{\underbrace{\dfrac{d\;u}{d\;x}\left(1\right)\varphi \left(1\right)} }

Switch to Matlab to solve the equations.

clear
syms u1 u2 u3 x 
phi(x) = [2*(x-1)*(x-1/2)  -4*x*(x-1)   2*x*(x-1/2)];
u = [u1; u2; u3];
dphi = diff(phi,x)
(4x348x4x1)\left(\begin{array}{ccc} 4\,x-3 & 4-8\,x & 4\,x-1 \end{array}\right)
syms du0
eqs = int(dphi*u*dphi,x,0,1) == int(x^2*phi, x, 0, 1) + phi(1)*-2 - phi(0)*du0;
eqs.'
(7u138u23+u33=du016016u238u138u33=15u138u23+7u33=3720) \def\arraystretch{2.5} \left(\begin{array}{c} \dfrac{7\,u_1 }{3}-\dfrac{8\,u_2 }{3}+\dfrac{u_3 }{3}=-{\textrm{du} }_0 -\dfrac{1}{60}\\ \dfrac{16\,u_2 }{3}-\dfrac{8\,u_1 }{3}-\dfrac{8\,u_3 }{3}=\dfrac{1}{5}\\ \dfrac{u_1 }{3}-\dfrac{8\,u_2 }{3}+\dfrac{7\,u_3 }{3}=-\dfrac{37}{20} \end{array}\right)

Set the boundary condition, the known nodal value:

u1 = -1

Here we have two alternatives, either reduce the equations to two, by eliminating the equation where u1u_1 is known, which is row 1, or by solving for d  ud  x(0)\frac{d\;u}{d\;x}\left(0\right) which we denote by du0. The result is the same (Try it! Experiment!).

% [u2, u3, du0] = solve(subs(eqs),[u2, u3, du0] )
[u2, u3] = solve(subs(eqs(2:3)),[u2, u3] )
U = simplify(subs(phi*u))
3x2208x51 -\dfrac{3\,x^2 }{20}-\dfrac{8\,x}{5}-1
syms ue(x)
DE = -diff(ue,x,2) == x^2;
du = diff(ue,x,1);
BC = [ue(0)==-1, du(1)==-2];
ue = dsolve(DE,BC)
x4125x31 -\dfrac{x^4 }{12}-\dfrac{5\,x}{3}-1
figure
fplot(ue, [0,1],'r','DisplayName','Exact solution'); hold on
fplot(U,[0,1],'b','DisplayName','Approximation')
plot([0,0.5,1],[u1,u2,u3],'ob','DisplayName','u_i')
legend show

Piecewise approximation

Instead of approximating the solution using one trial function, we can split the domain into NN elements and approximate the solution by finding more nodal solutions.

In this example we want to find all 6 nodal values uiu_i such that

ab(Uu)φidx=0\int_a^b \left(U-u\right)\varphi_i dx=0

Deriving basis functions

In order to solve this we need to define φi(x)\varphi_i \left(x\right) for each element. Let's see how basis functions are defined.

We can define a polynomial function using its nodal values:

U(x)=x2xx2x1u1+xx1x2x1=φ1u1+φ1u1U\left(x\right)=\dfrac{x_2 -x}{x_2 -x_1 }u_1 +\dfrac{x-x_1 }{x_2 -x_1 }=\varphi_1 u_1 +\varphi_1 u_1

The definition of a nodal basis function: Let the i'th basis function be equal to 1 in the i'th node and 0 in all other nodes.

φj(xi)={1if  i=j0if  ij\varphi_j \left(x_i \right)=\left\lbrace \begin{array}{ll} 1 & \textrm{if}\;i=j\\ 0 & \textrm{if}\;i\not= j \end{array}\right.

To derive the basis functions use the definition and make the ansatz:

φ(x)=c1+c2x\varphi \left(x\right)=c_1 +c_2 x φ1(0)=c1+c20=1φ1(1)=c1+c21=0φ2(0)=c1+c20=0φ2(1)=c1+c21=1 \def\arraystretch{1.5} \begin{array}{l} \varphi_1 \left(0\right)=c_1 +c_2 \cdot 0=1\\ \varphi_1 \left(1\right)=c_1 +c_2 \cdot 1=0\\ \varphi_2 \left(0\right)=c_1 +c_2 \cdot 0=0\\ \varphi_2 \left(1\right)=c_1 +c_2 \cdot 1=1 \end{array}

or on matrix form

[1x11x2][c11c21]=[10]\left\lbrack \begin{array}{cc} 1 & x_1 \\ 1 & x_2 \end{array}\right\rbrack \left\lbrack \begin{array}{c} c_{11} \\ c_{21} \end{array}\right\rbrack =\left\lbrack \begin{array}{c} 1\\ 0 \end{array}\right\rbrack and [1x11x2][c12c22]=[01]\left\lbrack \begin{array}{cc} 1 & x_1 \\ 1 & x_2 \end{array}\right\rbrack \left\lbrack \begin{array}{c} c_{12} \\ c_{22} \end{array}\right\rbrack =\left\lbrack \begin{array}{c} 0\\ 1 \end{array}\right\rbrack

or

[1x11x2]A[c11c12c21c22]C=[1001]IC=A1I\underset{\mathit{\mathbf{A}}}{\underbrace{\left\lbrack \begin{array}{cc} 1 & x_1 \\ 1 & x_2 \end{array}\right\rbrack } } \underset{\mathit{\mathbf{C}}}{\underbrace{\left\lbrack \begin{array}{cc} c_{11} & c_{12} \\ c_{21} & c_{22} \end{array}\right\rbrack } } =\underset{\mathit{\mathbf{I}}}{\underbrace{\left\lbrack \begin{array}{cc} 1 & 0\\ 0 & 1 \end{array}\right\rbrack } } \Rightarrow \mathit{\mathbf{C}}={\mathit{\mathbf{A}}}^{-1} \mathit{\mathbf{I}} φ(x)=[1  x]C\varphi \left(x\right)=\left\lbrack 1\;x\right\rbrack \mathit{\mathbf{C}}
syms x1 x2 x
A = [1 x1
     1 x2]
(1x11x2) \left(\begin{array}{cc} 1 & x_1 \\ 1 & x_2 \end{array}\right)
C = A\eye(2)
(x2x1x2x1x1x21x1x21x1x2) \def\arraystretch{2.5} \left(\begin{array}{cc} -\dfrac{x_2 }{x_1 -x_2 } & \dfrac{x_1 }{x_1 -x_2 }\\ \dfrac{1}{x_1 -x_2 } & -\dfrac{1}{x_1 -x_2 } \end{array}\right)
phi = simplify([1 x]*C)
(xx2x1x2xx1x1x2) \left(\begin{array}{cc} \dfrac{x-x_2 }{x_1 -x_2 } & -\dfrac{x-x_1 }{x_1 -x_2 } \end{array}\right)

φ1(0)=c1+c20+c302=1φ1(1/2)=c1+c21/2+c3(1/2)2=0φ1(1)=c1+c21+c312=0φ2(0)=c1+c20+c302=0φ2(1/2)=c1+c21/2+c3(1/2)2=1φ2(1)=c1+c21+c312=0φ3(0)=c1+c20+c302=0φ3(1/2)=c1+c21/2+c3(1/2)2=0φ3(1)=c1+c21+c312=1 \def\arraystretch{1.5} \begin{array}{l} \varphi_1 \left(0\right)=c_1 +c_2 \cdot 0+c_3 \cdot 0^2 =1\\ \varphi_1 \left(1/2\right)=c_1 +c_2 \cdot 1/2+c_3 \cdot {\left(1/2\right)}^2 =0\\ \varphi_1 \left(1\right)=c_1 +c_2 \cdot 1+c_3 \cdot 1^2 =0\\ \\ \varphi_2 \left(0\right)=c_1 +c_2 \cdot 0+c_3 \cdot 0^2 = 0\\ \varphi_2 \left(1/2\right)=c_1 +c_2 \cdot 1/2+c_3 \cdot {\left(1/2\right)}^2 = 1\\ \varphi_2 \left(1\right)=c_1 +c_2 \cdot 1+c_3 \cdot 1^2 = 0\\ \\ \varphi_3 \left(0\right)=c_1 +c_2 \cdot 0+c_3 \cdot 0^2 = 0\\ \varphi_3 \left(1/2\right)=c_1 +c_2 \cdot 1/2+c_3 \cdot {\left(1/2\right)}^2 = 0\\ \varphi_3 \left(1\right)=c_1 +c_2 \cdot 1+c_3 \cdot 1^2 = 1\\ \end{array}

which leads to the matrix system

[1x1x121x2x221x3x  33][c11c12c13c21c22c23c31c32c33]=[100010001]\left\lbrack \begin{array}{ccc} 1 & x_1 & x_1^2 \\ 1 & x_2 & x_2^2 \\ 1 & x_3 & x_{\;3}^3 \end{array}\right\rbrack \left\lbrack \begin{array}{ccc} c_{11} & c_{12} & c_{13} \\ c_{21} & c_{22} & c_{23} \\ c_{31} & c_{32} & c_{33} \end{array}\right\rbrack =\left\lbrack \begin{array}{ccc} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{array}\right\rbrack φ(x)=[1    x    x2]C\varphi \left(x\right)=\left\lbrack 1\;\;x\;\;x^2 \right\rbrack \mathit{\mathbf{C}}
syms x1 x2 x3 x
A = [1 x1 x1^2
     1 x2 x2^2
     1 x3 x3^2];
C = A\eye(3);

phi = simplify([1 x x^2]*C).'
φ(x)=((xx2)(xx3)(x1x2)(x1x3)(xx1)(xx3)(x1x2)(x2x3)(xx1)(xx2)(x1x3)(x2x3))for x[x1,x2] \def\arraystretch{2.5} \boldsymbol \varphi(x) = \left(\begin{array}{c} \dfrac{ {\left(x-x_2 \right)}\,{\left(x-x_3 \right)} }{ {\left(x_1 -x_2 \right)}\,{\left(x_1 -x_3 \right)} }\\ -\dfrac{ {\left(x-x_1 \right)}\,{\left(x-x_3 \right)} }{ {\left(x_1 -x_2 \right)}\,{\left(x_2 -x_3 \right)} }\\ \dfrac{ {\left(x-x_1 \right)}\,{\left(x-x_2 \right)} }{ {\left(x_1 -x_3 \right)}\,{\left(x_2 -x_3 \right)} } \end{array}\right) \quad \text{for } x \in [x_1, x_2]

and for x1=0,x2=1/2,x3=1x_1 =0,x_2 =1/2,x_3 =1 we have

subs(phi,[x1 x2 x3], [0, 1/2, 1])
φ(x)=(2(x1)(x12)4x(x1)2x(x12))for x[0,1] \def\arraystretch{2.0} \boldsymbol \varphi(x) = \left(\begin{array}{c} 2\,{\left(x-1\right)}\,{\left(x-\dfrac{1}{2}\right)}\\ -4\,x\,{\left(x-1\right)}\\ 2\,x\,{\left(x-\dfrac{1}{2}\right)} \end{array}\right) \quad \text{for } x \in [0, 1]

Piecewise approximation of a differential equation using a systematic approach

Solve

{d2udx2=1for  x[1,3]u(1)=2d  ud  x(3)=1 \def\arraystretch{2.0} \left\lbrace \begin{array}{ll} -\dfrac{d^2u}{dx^2}=1 & \textrm{for}\;x\in \left\lbrack 1,3\right\rbrack \\ u\left(1\right)=2 & \\ \dfrac{d\;u}{d\;x}\left(3\right)=-1 & \end{array}\right.

using 4 linear elements.

Solution: Weak form:

13d  ud  xd  vd  x  d  x=1  31v  d  x+[d  ud  x  v]13\int_1^3 \dfrac{d\;u}{d\;x}\dfrac{d\;v}{d\;x}\;d\;x=\int_{1\;}^3 1\cdot v\;d\;x+{\left\lbrack \dfrac{d\;u}{d\;x}\;v\right\rbrack }_1^3

Galerkin approximation using linear polynomials:

U(x)=i=12φi(x)uiU\left(x\right)=\sum_{i=1}^2 \varphi_i \left(x\right)u_i φ(x)=[x2xx2x1      xx1x2x1]\varphi \left(x\right)=\left\lbrack \dfrac{x_2 -x}{x_2 -x_1 }\;\;\;\dfrac{x-x_1 }{x_2 -x_1 }\right\rbrack d  ud  xd  Ud  x=d  d  x(φi(x)ui)=d  φd  xui\dfrac{d\;u}{d\;x}\approx \dfrac{d\;U}{d\;x}=\dfrac{d}{\;d\;x}\left(\sum \varphi_i \left(x\right)u_i \right)=\dfrac{d\;\varphi }{d\;x}u_i

Insert the approximation into the weak form

13id  φid  xui  d  φjd  x  d  x=131φj  d  x+[d  ud  xφj]13\int_1^3 \sum_i \dfrac{d\;\varphi_i }{d\;x}u_i \;\dfrac{d\;\varphi_j }{d\;x}\;d\;x=\int_1^3 1\cdot \varphi_j \;d\;x+{\left\lbrack \dfrac{d\;u}{d\;x}\varphi_j \right\rbrack }_1^3 =i13d  φid  x  d  φjd  x  d  x    ui=13φj  d  x+d  ud  x(3)φj(3)ged  ud  x(1)φj(1)gs=\sum_i \int_1^3 \dfrac{d\;\varphi_i }{d\;x}\;\dfrac{d\;\varphi_j }{d\;x}\;d\;x\;\;u_i =\int_1^3 \varphi_j \;d\;x+\underset{g_e }{\underbrace{\dfrac{d\;u}{d\;x}{\left(3\right)\varphi }_j \left(3\right)} } \underset{g_s }{\underbrace{-\dfrac{d\;u}{d\;x}\left(1\right)\varphi_j \left(1\right)} }

d  φid  x  d  φjd  x\frac{d\;\varphi_i }{d\;x}\;\frac{d\;\varphi_j }{d\;x} leads to the matrix

[d  φ1d  xd  φ2d  x][d  φ1d  xd  φ2d  x]=[φ1φ1φ1φ2φ2φ1φ2φ2] \def\arraystretch{2.0} \left\lbrack \begin{array}{c} \dfrac{d\;\varphi_1 }{d\;x}\\ \dfrac{d\;\varphi_2 }{d\;x} \end{array}\right\rbrack \left\lbrack \begin{array}{cc} \dfrac{d\;\varphi_1 }{d\;x} & \dfrac{d\;\varphi_2 }{d\;x} \end{array}\right\rbrack =\left\lbrack \begin{array}{cc} \varphi_1^{\prime } \varphi_1^{\prime } & \varphi_1^{\prime } \varphi_2^{\prime } \\ \varphi_2^{\prime } \varphi_1^{\prime } & \varphi_2^{\prime } \varphi_2^{\prime } \end{array}\right\rbrack Sij  ui=fj+gjS_{ij} \;u_i =f_j +g_j

Here SijS_{ij} is known as the stiffness matrix

The mesh

To describe any mesh we need a coordinate matrix (vector in 1D)

X=[x1x2x3x4x5] \mathbf{X}=\begin{bmatrix}x_{1}\\ x_{2}\\ x_{3}\\ x_{4}\\ x_{5} \end{bmatrix}

and a nodal connectivity matrix

T=[12233445] \mathbf{T}=\begin{bmatrix}1 & 2\\ 2 & 3\\ 3 & 4\\ 4 & 5 \end{bmatrix}

4 linear elements will lead to 5 equations to be solved for ui,i=1,2,...,5u_i ,i=1,2,\ldotp \ldotp \ldotp ,5

u=[u1u2u3u4u5] \mathbf{u}=\begin{bmatrix}u_{1}\\ u_{2}\\ u_{3}\\ u_{4}\\ u_{5} \end{bmatrix}

An element ee between nodes xix_i and xjx_j contributes only to equations i  i\;and jj. We can use the ee'th row of T\mathbf T to see which nodes are associated with the element ee.

Example: Element stiffness matrix for element 3 between node 3 and 4:

          1  2    3      4    512345[S33S34S43S44]S\begin{array}{l} \;\;\;\;\begin{array}{ccccc} \;1 & \;2 & \;\;3 & \;\;\;4 & \;\;5 \end{array}\\ \begin{array}{c} 1\\ 2\\ 3\\ 4\\ 5 \end{array}\underset{\mathit{\mathbf{S}}}{\underbrace{\left\lbrack \begin{array}{ccccc} & & & & \\ & & & & \\ & & S_{33} & S_{34} & \\ & & S_{43} & S_{44} & \\ & & & & \end{array}\right\rbrack } } \end{array}
S33=x3x4d  φ3d  x  d  φ3d  x  d  xS34=x3x4d  φ3d  x  d  φ4d  x  d  xS43=x3x4d  φ4d  x  d  φ3d  x  d  xS44=x3x4d  φ4d  x  d  φ4d  x  d  x \def\arraystretch{2.5} \begin{array}{l} S_{33} =\int_{x_3 }^{x_4 } \dfrac{d\;\varphi_3 }{d\;x}\;\dfrac{d\;\varphi_3 }{d\;x}\;d\;x\\ S_{34} =\int_{x_3 }^{x_4 } \dfrac{d\;\varphi_3 }{d\;x}\;\dfrac{d\;\varphi_4 }{d\;x}\;d\;x\\ S_{43} =\int_{x_3 }^{x_4 } \dfrac{d\;\varphi_4 }{d\;x}\;\dfrac{d\;\varphi_3 }{d\;x}\;d\;x\\ S_{44} =\int_{x_3 }^{x_4 } \dfrac{d\;\varphi_4 }{d\;x}\;\dfrac{d\;\varphi_4 }{d\;x}\;d\;x \end{array}

Local Numbering

We can use local node numbering to simplify things:

Now, for each element ee we define x1x_1 and x2x_2, compute φ\varphi and d  φd  x\frac{d\;\varphi }{d\;x} and then compute Se{\mathit{\mathbf{S}}}^e and fe{\mathit{\mathbf{f}}}^e

Element stiffness matrix

Se=x1x2d  φTd  xd  φd  x  d  x{\mathit{\mathbf{S}}}^e =\int_{x_1 }^{x_2 } \dfrac{d\;\varphi^T }{d\;x}\dfrac{d\;\varphi }{d\;x}\;d\;x

or

Se=[x1x2d  φ1d  xd  φ1d  x  d  xx1x2d  φ1d  xd  φ2d  x  d  xx1x2d  φ2d  xd  φ1d  x  d  xx1x2d  φ2d  xd  φ2d  x  d  x] \def\arraystretch{2.5} {\mathit{\mathbf{S}}}^e =\left\lbrack \begin{array}{cc} \int_{x_1 }^{x_2 } \dfrac{d\;\varphi_1 }{d\;x}\dfrac{d\;\varphi_1 }{d\;x}\;d\;x & \int_{x_1 }^{x_2 } \dfrac{d\;\varphi_1 }{d\;x}\dfrac{d\;\varphi_2 }{d\;x}\;d\;x\\ \int_{x_1 }^{x_2 } \dfrac{d\;\varphi_2 }{d\;x}\dfrac{d\;\varphi_1 }{d\;x}\;d\;x & \int_{x_1 }^{x_2 } \dfrac{d\;\varphi_2 }{d\;x}\dfrac{d\;\varphi_2 }{d\;x}\;d\;x \end{array}\right\rbrack

and element load vector

fe=x1x2φTf  d  x{\mathit{\mathbf{f}}}^e =\int_{x_1 }^{x_2 } \varphi^T f\;d\;x

or

fe=[x1x2fφ1  dxx1x2fφ2  dx] \def\arraystretch{2.0} {\mathit{\mathbf{f}}}^e =\left\lbrack \begin{array}{c} \int_{x_1 }^{x_2 } f\varphi_1 \;\textrm{dx}\\ \int_{x_1 }^{x_2 } f\varphi_2 \;\textrm{dx} \end{array}\right\rbrack

Assembly process

For each element ee, we add Se{\mathit{\mathbf{S}}}^e and fe{\mathit{\mathbf{f}}}^e to the correct place in S\mathit{\mathbf{S}} and f\mathit{\mathbf{f}}.

Element 1: S([1,2],[1,2])

Element 2: S([2,3],[2,3])

Element 3: S([3,4],[3,4])

Element 4: S([4,5],[4,5])

We need to add each element contribution to the global stiffness matrix.

Siinew=Siiold+S11eSijnew=Sijold+S12e\begin{array}{l} S_{\textrm{ii}}^{\textrm{new}} =S_{\textrm{ii}}^{\textrm{old}} +S_{11}^e \\ S_{\textrm{ij}}^{\textrm{new}} =S_{\textrm{ij}}^{\textrm{old}} +S_{12}^e \\ \vdots \end{array}
finew=fiold+f1efjnew=fjold+f2e\begin{array}{l} f_i^{\textrm{new}} =f_i^{\textrm{old}} +f_1^e \\ f_j^{\textrm{new}} =f_j^{\textrm{old}} +f_2^e \end{array}

or on matrix form

S(row,col) S(\text{row},\text{col})

where row denotes the array containing the row indices and col the column indices. Thus we could write

S([i,j],[i,j]) S([i,j],[i,j])

to mean a sub-matrix of SS with rows ii and jj and columns ii and jj.

The corresponding Matlab (or Julia) syntax is

S([i,j],[i,j]) = S([i,j],[i,j]) + Se

All nodes that have several elements will get contributions from each element. We are "accumulating" stiffness to these nodes from the elements.

Contribution from element 1

S([1,2],[1,2])2×2=S([1,2],[1,2])2×2+S2×2e1{\mathit{\mathbf{S}}\left(\left\lbrack 1,2\right\rbrack ,\left\lbrack 1,2\right\rbrack \right)}_{2\times 2} ={\mathit{\mathbf{S}}\left(\left\lbrack 1,2\right\rbrack ,\left\lbrack 1,2\right\rbrack \right)}_{2\times 2} +S_{2\times 2}^{e_1 }
                  1      234512345[S111S121S211S221]\begin{array}{l} \;\;\;\;\begin{array}{ccccc} \;\;\;\;\;1 & \;\;\;2 & 3 & 4 & 5 \end{array}\\ \begin{array}{c} 1\\ 2\\ 3\\ 4\\ 5 \end{array}\left\lbrack \begin{array}{ccccc} S_{11}^1 & S_{12}^1 & & & \\ S_{21}^1 & S_{22}^1 & & & \\ & & & & \\ & & & & \\ & & & & \end{array}\right\rbrack \end{array}

Contribution from element 2

S([2,3],[2,3])2×2=S([2,3],[2,3])2×2+S  e2{\mathit{\mathbf{S}}\left(\left\lbrack 2,3\right\rbrack ,\left\lbrack 2,3\right\rbrack \right)}_{2\times 2} ={\mathit{\mathbf{S}}\left(\left\lbrack 2,3\right\rbrack ,\left\lbrack 2,3\right\rbrack \right)}_{2\times 2} +S_{\;}^{e_2 }
                  1                2                34512345[S111S121S211S221+S112S122S212S222]\begin{array}{l} \;\;\;\;\begin{array}{ccccc} \;\;\;\;\;1 & \;\;\;\;\;\;\;\;2 & \;\;\;\;\;\;\;\;3 & 4 & 5 \end{array}\\ \begin{array}{c} 1\\ 2\\ 3\\ 4\\ 5 \end{array}\left\lbrack \begin{array}{ccccc} S_{11}^1 & S_{12}^1 & & & \\ S_{21}^1 & S_{22}^1 +S_{11}^2 & S_{12}^2 & & \\ & S_{21}^2 & S_{22}^2 & & \\ & & & & \\ & & & & \end{array}\right\rbrack \end{array}

etc

Boundary conditions

Since u1u_1 is known, we remove the associated equation from the system.

We also need to modify the right-hand side, since u1  u_1 \;is non-zero it will cause a reaction force that need to be added to the force vector.

fnew=fold+g[S11S21S51]u1{\mathit{\mathbf{f}}}^{\textrm{new}} ={\mathit{\mathbf{f}}}^{\textrm{old}} +\mathit{\mathbf{g}}-\left\lbrack \begin{array}{c} S_{11} \\ S_{21} \\ \vdots \\ S_{51} \end{array}\right\rbrack u_1

and finally our equation system is

[S22S23S24S25S32S42S52S55][u2u3u4u5]=[f2+0S21u1f3+0S31u1f4+0S41u1f51g5S51u1]\left\lbrack \begin{array}{cccc} S_{22} & S_{23} & S_{24} & S_{25} \\ S_{32} & & & \vdots \\ S_{42} & & & \vdots \\ S_{52} & \cdots & \cdots & S_{55} \end{array}\right\rbrack \left\lbrack \begin{array}{c} u_2 \\ u_3 \\ u_4 \\ u_5 \end{array}\right\rbrack =\left\lbrack \begin{array}{c} f_2 +0-S_{21} u_1 \\ f_3 +0-S_{31} u_1 \\ f_4 +0-S_{41} u_1 \\ f_{5}\underset{g_{5}}{\underbrace{-1}}-S_{51}u_{1} \end{array}\right\rbrack

Matlab implementation

{d2udx2=1for  x[1  3]u(1)=2d  ud  x(3)=1 \def\arraystretch{2.0} \left\lbrace \begin{array}{ll} -\dfrac{d^2 u}{d x^2}=1 & \textrm{for}\;x\in \left\lbrack 1\;3\right\rbrack \\ u\left(1\right)=2 & \\ \dfrac{d\;u}{d\;x}\left(3\right)=-1 & \end{array}\right.
clear
a = 1; b = 3;
N = 4;
xnod = linspace(a,b,N+1);
nodes = [(1:N)',(2:N+1)'];

syms x
Phi = @(x1,x2,x)[(x-x2)/(x1-x2), -(x-x1)/(x1-x2)];
Dphi = matlabFunction(diff(Phi,x));

neq = N+1;
S = zeros(neq,neq); F = zeros(neq,1);

for iel = 1:N
    inods = nodes(iel,:);
    x1 = xnod(inods(1));
    x2 = xnod(inods(2));

    phi = Phi(x1,x2,x);
    dphi = Dphi(x1,x2);

    Se = double(int(dphi.'*dphi,x,x1,x2));
    fe = double(int(1*phi.',x,x1,x2));
    
    S(inods,inods) = S(inods,inods) + Se;
    F(inods) = F(inods) + fe;
end

presc = 1;
alldofs = 1:N+1;
free = setdiff(alldofs,presc);

u = zeros(neq,1); u(presc) = 2;
g = zeros(neq,1); 
g(1) = -0;
g(end) = -1;

F = F+g-S(:,presc)*u(presc);

u(free) = S(free,free)\F(free)

u = 5x1    
    2.0000
    2.3750
    2.5000
    2.3750
    2.0000

syms ue(x)
DE = -diff(ue,x,2) == 1;
du = diff(ue,x,1);
BC = [ue(1)==2, du(3)==-1];
ue = dsolve(DE,BC)
12x(x4)2 \dfrac{1}{2}-\dfrac{x\,{\left(x-4\right)} }{2}

figure
fplot(ue, [1,3],'r','DisplayName','Exact solution'); hold on
plot(xnod,u,'ob-','DisplayName','Approximation')
legend show

1: Approximate solution using 4 linear elements

Computing the L2L_2 error

L2err:=13(Uue)2  d  xL_2 \textrm{err}:=\sqrt{\int_1^3 {\left(U-u_e \right)}^2 \;d\;x}

For each element ee the approximate function between nodes ii and jj is given by

U(x)=φ(x)ue U(x) = \boldsymbol \varphi(x) \cdot \boldsymbol u_e

Specifically for our case we have

U(x)=[φi, φj][ui, uj] U(x) = [\varphi_i,\ \varphi_j] \cdot [u_i,\ u_j]
L2err = 0;
for iel = 1:N
    inods = nodes(iel,:);
    x1 = xnod(inods(1));
    x2 = xnod(inods(2));
    phi = Phi(x1,x2,x);

    U = phi*u(inods);

    L2err = L2err + int((U-ue)^2,x,x1,x2);
end
L2err = double(sqrt(L2err))
L2err = 0.0323