# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.8462512,"math_prob":0.88387847,"size":1757,"snap":"2019-43-2019-47","text_gpt3_token_len":416,"char_repetition_ratio":0.11466058,"word_repetition_ratio":0.006756757,"special_character_ratio":0.22538418,"punctuation_ratio":0.117142856,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99278545,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,6,null,6,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-21T23:51:27Z\",\"WARC-Record-ID\":\"<urn:uuid:f289ae14-f390-4f87-9a0c-a90b7693f5db>\",\"Content-Length\":\"153228\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cd544921-ccb9-42c8-9e25-104bc4e7c1f4>\",\"WARC-Concurrent-To\":\"<urn:uuid:9d98b82b-55a0-497e-8ea8-412aacd1600e>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://scicomp.stackexchange.com/questions/10203/visualizing-finite-element-solutions-in-matlab/10204\",\"WARC-Payload-Digest\":\"sha1:RMT3IHW2W2IXTGRNJNQE27KUUQH3VQBF\",\"WARC-Block-Digest\":\"sha1:NMDWXJUWIDMTGRWECG2NCWSWTF6RZUMT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496671053.31_warc_CC-MAIN-20191121231600-20191122015600-00281.warc.gz\"}"}