I think the definition of temp could be brought out one loop here (from the `k` loop to the `j` loop).

Also, the mass matrix `MM` is symmetric, so smarter looping/filling could be used.