# Visualizing finite element solutions in MATLAB

On my triangular mesh, I have the $(x,y,z)$ coordinates of each vertex of each triangle. For higher order elements, I refine each element a few times so I have more points to work with. If I just need to visualize the solution for myself, I use fill3 but I'm not entirely sure how to insert my own colorbar. Ideally, I want the smallest z value to correspond to blue, and the largest to correspond to red, like the typical colorbar in the surf plots. Here's an example of what I'm trying to achieve: fill3 works fine, I'm just not really sure how to set the custom colormap to my needs, and the documentation hasn't helped much.

I'm open to other suggestions as well, it's just that MATLAB is what I'm familiar with for visualization.

Also, just curious if anyone knows what software this plot was made in as I've seen plots like it in a number of papers.

For higher order elements, I refine each element a few times so I have more points to work with. If I just need to visualize the solution for myself.

Let's use quadratic Lagrange elements as examples. You need the mesh data, points p and triangles t, also the numerical solution $u_h$. For visualization purpose, we merely need the nodal values from the numerical solution using quadratic Lagrange elements (quadratic Lagrange has edge DoFs and node DoFs). Same for the higher order elements, we only need the nodal DoF's value to visualize the solution.

For example: your solution column vector is u which is gonna be the $z$ value of the surf plot. For quadratic element, the most natural data structure is to assign the first NNodes = size(p,1) rows the value of nodal DoFs, then NEdges = size(e,1) rows the value of edge DoFs. Because you use triangular mesh, we can use MATLAB's trisurf to do this:

h = trisurf(t, p(:,1), p(:,2), u(1:size(p,1))');


The colormap's range can be manually assigned, see MATLAB's documents here. There are some built-in schemes like jet, you can use an $m\times 3$ RGB matrix as well (you can retrieve the color matrix by typing c = colormap; then check what c is like).

Here is another very helpful MATLAB doc on how to manually setting the colormap for a patch object (surf and trisurf are both using patch to draw things): Coloring Mesh and Surface Plots

Here is what the numerical solution using quadratic elements is like for $-\Delta u = 1$ with homogeneous Dirichlet boundary data on an L-shape domain. There are no discontinuous gap between the nodes like your plot yields. Unless you use $hp$-Discontinuous Galerkin, your plot is wrong for usually continuous finite element solutions for any elliptic equations. :

• Thanks, this worked perfectly. And the sample plot I gave was from a journal paper and it is indeed DGFEM. – Justin Dong Dec 3 '13 at 8:41

You could output your data in one of the widely used file formats, for example VTK, and then use either VisIt or Paraview to visualize. These programs are used a lot for visualizing PDE solutions and are made for this purpose.

It's simpler to use the functions pdemesh or pdesurf since there are done exactly for that purpose. You can then change the colorscale with

colormap('jet')


This changes the color map to what you need. There are other preset colormap and you can also define yours, see the colormap function help.

If you have not the PDE package in Matlab, you can maybe use Octave which has the same functionalities.

• where do I specify the colormap function though? I have one for loop where I plot one triangle per iteration. So I have something like for k=1:nElem; fill3(x,y,z,'r'); end; Placing colormap('jet') right after the call to fill3 doesn't seem to do anything, and I can't input it as an argument to fill3 since the dimensions don't match. – Justin Dong Dec 2 '13 at 8:31
• also, I don't think I have access to the PDE toolbox. My programming is in C and I'm just exporting to MATLAB for visualization. – Justin Dong Dec 2 '13 at 8:57
• Sorry, I assumed that fill3 would create a standard plot, I did not know how you used it. And yes, those functions are in the PDE toolbox. But they are available in Octave if this is an option. (this is what I use to visualize my C calculations as well) – Dr_Sam Dec 2 '13 at 9:22