# Assemble P2 finite elements - Matlab or references

I would like to implement P2 (or even P3...) finite elements in Matlab. I need to be able to control the construction of the rigidity and mass matrices ($\int \nabla \varphi_i\cdot \nabla \varphi_j$ and $\int \varphi_i \varphi_j$) (since I want to tackle some precision computation problems afterwards, and maybe go beyond machine precision using Advanpix...)

If there are already some good Matlab codes which do this, please point me to them. If not, are there any good references from where I could learn to do this properly?

I would like to avoid doing all the computations by hand if possible since I'm sure someone else has done them before :) ... (find the right basis functions, compute explicitly all integrals of products of neighbor basis functions, etc...)

Please check “The Finite Element Method: Theory, Implementation, and Applications” by Larson and Bengzon. They have Matlab implementation of P2 Lagrange elements (assembly of stiffness matrix) on page 217. It should be easy to implement higher order elements based on their code as well.

• Thank you for your answer. I'll take a look in the book you mention. – Beni Bogosel Jun 8 '17 at 6:59

Is your question just regarding the final assembly of the system into a global one then you can find the answer here FEM assembly on unstructured meshes although implementation is in c++ it is very easily translatable into Matlab.

• Thank you for your answer. Unfortunately, translating C++ code into efficient Matlab code is not that easy... Embedded for loops are very slow in Matlab. – Beni Bogosel Jun 8 '17 at 6:59
• I have tested both the version for assembly alone and they are not that different. Of course c++ is faster when you consider for large problems 10^6 – kaush Jun 8 '17 at 7:07