# Finite elements/Model finite element approximation
## Finite Element Formulation for Problem 2.

The domain for this problem is $\Omega =\{x~|~x\in (0,1)\}$ and the boundary consists of two points $\Gamma =\{0,1\}$. Let us use $n$ nodes in the domain so that they divide the domain into $n-1$ nonoverlapping, two-noded elements.
Consider the term $K_{ij}$ in equation (22). The integral can be written as a sum of integrals over each element as
    ${\begin{aligned}K_{ij}&=\int _{0}^{1}\left({\cfrac {dN_{i}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{i}N_{j}\right)~dx\\{\text{(29)}}\quad &=\int _{0}^{x_{1}}\left({\cfrac {dN_{i}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{i}N_{j}\right)~dx+\int _{x_{1}}^{x_{2}}\left({\cfrac {dN_{i}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{i}N_{j}\right)~dx+\dots +\int _{x_{j-1}}^{x_{j}}\left({\cfrac {dN_{i}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{i}N_{j}\right)~dx\\&~~~+\int _{x_{j}}^{x_{j+1}}\left({\cfrac {dN_{i}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{i}N_{j}\right)~dx+\dots +\int _{x_{n-1}}^{x_{n}}\left({\cfrac {dN_{i}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{i}N_{j}\right)~dx~.\end{aligned}}$
In this equation, $i$ and $j$ are node numbers. Therefore there are $n\times n$ possible values of $K_{ij}$.
Assume that node $i$ = 2 and node $j$ = 4. Then $N_{i}=1$ at node 2 and zero at all the other nodes. Similarly, $N_{j}=1$ at node 4 and zero at all other nodes. Also, $N_{i}$ is non-zero only between nodes 1, 2, and 3 while $N_{j}$ is nonzero only between nodes 3, 4, and 5. Since the domains of $N_{i}$ and $N_{j}$ do not overlap in this case, all the integrals must be zero.
In general, if $i$ and $j$ are separated by more than one node, at least one of the basis functions has a zero value within each integral. The same holds for the derivatives of the basis functions. Therefore $K_{ij}=0$ if $i$ and $j$ are separated by more than one node.
Therefore, there are three non-trivial cases that need to be looked at
* $i=j$.
* $i=j-1$.
* $i=j+1$.
For the first case, set $i=j$ in equation (29). That means
    ${\text{(30)}}\qquad \int _{0}^{1}\left({\cfrac {dN_{j}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{j}N_{j}\right)~dx=\int _{x_{j-1}}^{x_{j}}\left({\cfrac {dN_{j}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{j}N_{j}\right)~dx+\int _{x_{j}}^{x_{j+1}}\left({\cfrac {dN_{j}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{j}N_{j}\right)~dx~.$
After substituting the values of the basis functions (25) and their derivatives (26) into equation (30) and integrating, we get
    ${K_{jj}=\int _{0}^{1}\left({\cfrac {dN_{j}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{j}N_{j}\right)~dx={\cfrac {1}{x_{j}-x_{j-1}}}+{\cfrac {1}{x_{j+1}-x_{j}}}+{\cfrac {x_{j+1}-x_{j-1}}{3}}~.}$
For the second case, set $i=j-1$ in equation (29). In this case, the only non-zero integrals in equation (29) are the ones between $x_{j-1}$ and $x_{j}$. Hence
    ${\text{(31)}}\qquad \int _{0}^{1}\left({\frac {dN_{j-1}}{dx}}{\frac {dN_{j}}{dx}}+N_{j-1}N_{j}\right)~dx=\int _{x_{j-1}}^{x_{j}}\left({\frac {dN_{j-1}}{dx}}{\frac {dN_{j}}{dx}}+N_{j-1}N_{j}\right)~dx~.$
After substituting the basis functions and their derivatives into equation (31) and integrating, we get
    ${K_{(j-1)j}=\int _{0}^{1}\left({\frac {dN_{j-1}}{dx}}{\frac {dN_{j}}{dx}}+N_{j-1}N_{j}\right)~dx=-{\frac {1}{x_{j}-x_{j-1}}}+{\frac {x_{j}-x_{j-1}}{6}}~.}$
For the third case, set $i=j+1$ in equation (29). In this case, the only non-zero integrals in equation (29) are the ones between $x_{j}$ and $x_{j+1}$. Hence
    ${\text{(32)}}\qquad \int _{0}^{1}\left({\frac {dN_{j}}{dx}}{\frac {dN_{j+1}}{dx}}+N_{j}N_{j+1}\right)~dx=\int _{x_{j}}^{x_{j+1}}\left({\frac {dN_{j}}{dx}}{\frac {dN_{j+1}}{dx}}+N_{j}N_{j+1}\right)~dx~.$
After substituting the basis functions and their derivatives into equation (\ref{eq:Integralij+1}) and integrating, we get
    ${K_{j(j+1)}=\int _{0}^{1}\left({\frac {dN_{j}}{dx}}{\frac {dN_{j+1}}{dx}}+N_{j}N_{j+1}\right)~dx=-{\frac {1}{x_{j+1}-x_{j}}}+{\frac {x_{j+1}-x_{j}}{6}}~.}$
The same process can be followed for the integral
    ${f_{j}=\int _{0}^{1}x~N_{j}~dx=\int _{x_{j-1}}^{x_{j}}x~N_{j}~dx+\int _{x_{j}}^{x_{j+1}}x~N_{j}~dx={\cfrac {2x_{j}^{3}-3x_{j}^{2}x_{j-1}+x_{j-1}^{3}}{6(x_{j}-x_{j-1})}}+{\cfrac {2x_{j}^{3}-3x_{j}^{2}x_{j+1}+x_{j+1}^{3}}{6(x_{j+1}-x_{j})}}~.}$
Now that we know the components $K_{ij}$ and $f_{j}$, we can solve the system of equations (23) for the unknowns $a_{i}$. This gives us our finite element solution for Problem 2.

### Assembly process.

In the above we did not go through the assembly process that you are familiar with from introductory finite elements. We can simplify things if we use just compute the integrals over each element and assemble them to get the final $\mathbf {K}$ and $\mathbf {f}$ matrices.
To see how the assembly process works, let us recall equation (21)
    ${\text{(33)}}\qquad {\sum _{i=1}^{n}a_{i}\int _{0}^{1}\left({\cfrac {dN_{i}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{i}N_{j}\right)~dx-\int _{0}^{1}x~N_{j}~dx=0\qquad j=1\dots n~.}$
We can rewrite this equation as
    $\sum _{i=1}^{n}K_{ji}a_{i}=f_{j}\qquad \equiv \qquad \sum _{i=1}^{n}K_{ij}a_{i}=f_{j}$
where
    ${\text{(34)}}\qquad K_{ij}=a(N_{i},N_{j})=\int _{0}^{1}\left({\cfrac {dN_{i}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{i}N_{j}\right)~dx\qquad {\text{and}}\qquad f_{j}=\langle x,N_{j}\rangle =\int _{0}^{1}x~N_{j}~dx~.$
Let us define
    ${\text{(35)}}\qquad {\mathcal {F}}(N_{i},N_{j}):={\cfrac {dN_{i}}{dx}}{\cfrac {dN_{j}}{dx}}+N_{i}N_{j}~.$
Then the first of the equations in (34) can be written as
    ${\text{(36)}}\qquad a(N_{i},N_{j})=\int _{0}^{1}{\mathcal {F}}(N_{i},N_{j})~dx~.$
From equation (29) we can see that the integral over the entire domain $\Omega$ can be written as a sum of integrals over the elements $\Omega _{e}$. Therefore, we can write equation (36) as
    ${\text{(37)}}\qquad a(N_{i},N_{j})=\int _{\Omega }{\mathcal {F}}(N_{i},N_{j})~dx=\sum _{e=1}^{E}\int _{\Omega _{e}}{\mathcal {F}}(N_{i},N_{j})~dx~.$
Let $N_{i}^{e}$ be the local basis functions in an element. Then equation (37) can be written as
    $a(N_{i},N_{j})=\sum _{e=1}^{E}\int _{\Omega _{e}}{\mathcal {F}}(N_{i}^{e},N_{j}^{e})~dx=\sum _{e=1}^{E}a^{e}(N_{i}^{e},N_{j}^{e})=\sum _{e=1}^{E}K_{ij}^{e}~.$
Similarly, the second equation in (34) can be written as
    $\langle x,N_{j}\rangle =\sum _{e=1}^{E}\langle x^{e},N_{j}^{e}\rangle =\sum _{e=1}^{E}f_{j}^{e}$
where $x^{e}$ indicates that the integral is over the element.
Therefore, the matrix $\mathbf {K}$ and the vector $\mathbf {f}$ can be expressed as a sum over the elements in the form
    $\mathbf {K} =\sum _{e=1}^{E}\mathbf {K} ^{e}\qquad {\text{and}}\qquad \mathbf {f} =\sum _{e=1}^{E}\mathbf {f} ^{e}~.$
This is the familiar assembly process. From this process it is clear that if we can find the weak form for one element, then the finite element system of equations for any combination of such elements can be computed by assembly.