Hello Lukas,
The performance issue you are experiencing is likely due to the overhead associated with storing and accessing function handles in a loop. In MATLAB, function handles can introduce significant overhead, especially when accessed frequently in performance-critical sections of code like matrix assembly. Here are some strategies to optimize your code:
- Instead of storing basis functions as function handles, you can precompute the basis function values and their derivatives at the quadrature points and store these values in arrays. This way, you avoid the overhead of calling function handles repeatedly.
- Precompute the basis functions and their derivatives at the quadrature points for each element and store them in arrays. This reduces the need to evaluate the basis functions repeatedly during the assembly process.
- Ensure that your loops are structured efficiently. For example, avoid unnecessary computations inside nested loops and consider using vectorized operations where possible.
- If possible, vectorize operations inside loops to take advantage of MATLAB's optimized matrix operations. This might involve restructuring how you compute and store basis function values.
- Continue using MATLAB's profiler to identify other potential bottlenecks. Sometimes, small changes in indexing or loop order can lead to significant performance improvements.
- If applicable, use sparse matrices to store M and S, especially if they are large and mostly zeros. This can reduce memory usage and improve performance when solving the system.
I hope this helps!