# Displaying finite-element basis functions

**URL:** <https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815>\
**Category:** Uncategorized\
**Created:** [October 13, 2021, 7:20pm UTC](https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815 "2021-10-13T19:20:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tucker](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@tucker](https://fenicsproject.discourse.group/u/tucker)\
**Post date:** [October 13, 2021, 7:20pm UTC](https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815/1 "2021-10-13T19:20:39Z")

</div>

I was hoping to display the basis elements for a degree 2 Lagrange space with a mesh element on the unit interval on got some unexpected behavior. I am hoping that someone can help provide clarity.

For a function u\in\mathcal{V}\_{h}, I expect

u(x) =\sum\_{i=0}^{\text{dim}(\mathcal{V}\_{h})-1}u\_{i}\phi\_{i}(x)

where \lbrace\phi\_{i}\rbrace\_{i} is a basis for \mathcal{V}\_{h} and u\_{i} are the finite-element expansion coefficients. I assume that the expansion coefficients are represented in the `vector` data of a FEniCS `Function`. With this in mind I then attempted to construct \lbrace \phi\_{i}(x)\rbrace as a `Function`, where only the $i$th entry of the associated `vector` data is nonzero.

> import numpy as np  
> import dolfin as dl  
> import matplotlib.pyplot as plt  
> #alter the plot style  
> plt.style.use(‘classic’)  
> plt.rcParams.update({‘font.size’: 16})  
> #define a unit interval mesh with nElem elements  
> nElem = 1  
> mesh = dl.UnitIntervalMesh(nElem)  
> dl.plot(mesh)  
> plt.show()  
> #define degree deg continuous elements over the mesh  
> deg = 2  
> Ph = dl.FiniteElement(“CG”, mesh.ufl\_cell(), deg)  
> Vh = dl.FunctionSpace(mesh, Ph)

> #I now desire to visualize the basis functions  
> #I expect that the vector entry data of a dl.Function(), u  
> #corresponds to the finite element expansion of u  
> #that is u(x) = sum\_i phi\_i(x) u\_i  
> #where u\_i are the finite-element expansion coefficients  
> #and phi\_i(x) are the finite-element basis functions

> n = Vh.dim() # dim(Vh) = number of basis functions of Vh  
> basis\_functions = [dl.Function(Vh) for i in range(n)]  
> ei = np.zeros(n) # numpy unit vector  
> for i in range(n):  
> ei[i] = 1.0  
> # assign the unit vector data to the dl.Function(Vh) function  
> # so that it now corresponds to a basis function  
> basis\_functions[i].vector().set\_local(ei[:])  
> ei[i] = 0.0  
> # plot the result  
> dl.plot(basis\_functions[i])  
> plt.title(r’$k$th finite-element basis function k = {0:1d} '.format(i))  
> plt.show()

In the above example I specified degree `deg` 2 elements and so did not expect to see the following linear functions.

 ![phi2](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/7/750b35fb975e92df5fd37818216263eae6a7fa02.png)  
 ![phi1](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/8/824cfdef2f74c66bfa93eb1a39d4177217a1ac43.png)  
 ![phi0](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/6/6d3b9fc5a7078049a149d6ba5bf9713ffad2d254.png)

Please let me know if anything I have described is unclear.

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [October 13, 2021, 8:11pm UTC](https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815/2 "2021-10-13T20:11:45Z")

</div>

I can’t understand your code, but is this what you mean?

```auto
from dolfin import *
import numpy as np
import matplotlib.pyplot as plt

mesh = UnitIntervalMesh(1)
V = FunctionSpace(mesh, "CG", 2)
u = Function(V)
x = np.linspace(0, 1, 50)

for j in range(V.dim()):
	u.vector()[:] = 0.0
	u.vector()[j] = 1.0

	y = np.fromiter(map(u, x), dtype=np.double)

	plt.plot(x, y, label=f"$j = {j}$")

plt.xlim(0.0, 1.0)
plt.xlabel("$x$")
plt.ylabel(r"$\phi_j(x)$")
plt.legend()
plt.show()

```

E.g.

![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/f/f86d22a1cd9c42c0932a04a311aacbf69b6c5598.png)  
 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/e/edaf62ce0f30525187c2dc21f4a604cb094d2851.png)  
 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/0/02416173ab7d1f82ac297592663d59c2e846c3f9.png)  
 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/4/4f4a321ce5828bed6146b99ab17a85960c26214b.png)

---

<div class="post-metadata">

**Author:** ![tucker](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@tucker](https://fenicsproject.discourse.group/u/tucker)\
**Post date:** [October 13, 2021, 8:22pm UTC](https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815/3 "2021-10-13T20:22:57Z")

</div>

Thanks so much @nate, it appears that I was just not properly visualizing the basis functions. I needed to sample the basis functions at more points, as you have via `x=np.linspace(0, 1, 50)`. I wonder why the `FEniCS` `plot` function did not take care of that for me.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [October 13, 2021, 8:30pm UTC](https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815/4 "2021-10-13T20:30:30Z")

</div>

The fenics plot function only visualizes CG 1 functions (it interpolates higher order functions to CG 1). This is because the plot function Also supports plotting on triangles and tetrahedra.

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [October 13, 2021, 8:32pm UTC](https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815/5 "2021-10-13T20:32:14Z")

</div>

FEniCS’s built in `plot` interpolates at the mesh vertices because it’s cheap. The operation you see above is very expensive in comparison.

It’s often encouraged to output solutions using `XDMFFile.write_checkpoint` for use external visualisation software for this very purpose (e.g. paraview).

E.g. the following which are both visualisations of the same basis function; however, one the cheap interpolant at the mesh verticies, and the other with more expensive interpolant sampling throughout the element.  
 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/2/26219847adc0990d400c4665e256d87303b9942d.png) ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/a/a0a73253e1f071905756fab12feb8d86974611c2.png)

---

<div class="post-metadata">

**Author:** ![tucker](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@tucker](https://fenicsproject.discourse.group/u/tucker)\
**Post date:** [October 13, 2021, 8:32pm UTC](https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815/6 "2021-10-13T20:32:35Z")

</div>

Thanks @dokken for explaining the reason why things occurred as they did. This is useful information.

---

<div class="post-metadata">

**Author:** ![tucker](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@tucker](https://fenicsproject.discourse.group/u/tucker)\
**Post date:** [October 13, 2021, 8:33pm UTC](https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815/7 "2021-10-13T20:33:21Z")

</div>

Very good! Thanks so much! @nate

---

<div class="post-metadata">

**Author:** ![dohertyw1](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dohertyw1/32/2561_2.png) [@dohertyw1](https://fenicsproject.discourse.group/u/dohertyw1)\
**Post date:** [November 30, 2023, 11:14am UTC](https://fenicsproject.discourse.group/t/displaying-finite-element-basis-functions/6815/8 "2023-11-30T11:14:40Z")

</div>

Hi Nate.

Would you be able to share the code for these plots?

Best,  
Will
