# A Compact FEM Implementation for Parabolic Integro-Differential Equations in 2D
## 7 Overview of an Implementation in Matlab
\begin{table}
\begin{tabular}{c c c c c c c c c} \hline \hline \(\mathbf{h}\) & \(\mathbf{N}\) & \(\mathbf{t}_{unrec}^{\text{BELR}}\) [s] & \(\mathbf{t}_{rec}^{\text{BELR}}\) [s] & \(\mathbf{t}_{unrec}^{\text{BERR}}\) [s] & \(\mathbf{t}_{vec}^{\text{BERR}}\) [s] & \(\mathbf{t}_{unrec}^{\text{CNN}}\) [s] & \(\mathbf{t}_{vec}^{\text{CN}}\) [s] & \(\mathbf{t}_{C}\) [s] \\ \hline
0.2 & 258 & 0.81 & 0.77 & 0.82 & 0.77 & 0.83 & 0.77 & 0.31 \\
0.1 & 978 & 0.98 & 0.81 & 1.00 & 0.81 & 1.04 & 0.82 & 0.55 \\
0.05 & 4086 & 2.06 & 1.08 & 1.96 & 1.08 & 2.13 & 1.08 & 1.73 \\
0.025 & 16,406 & 10.26 & 4.36 & 10.51 & 4.40 & 10.93 & 4.41 & 6.93 \\
0.0125 & 66,142 & 114.40 & 38.04 & 112.86 & 38.24 & 114.42 & 38.36 & 34.13 \\ \hline \hline \end{tabular}
\end{table} 
Table 3: Quantitative information on the runtimes of the unvectorized and vectorized MATLAB sets of code with different numerical schemes, and the same for code from COMSOL Multiphysics, when solving the PIDE. Abbreviations: BELR, backward-Euler scheme with left rectangular rule; BERR, backward-Euler scheme with right rectangular rule; CN, Crank–Nicolson scheme with trapezoidal rule; \(N\), number of elements; \(t_{unrec}^{\text{SC}}\), runtime of the unvectorized code in seconds involving loops in the assembly process, using a particular method \(SCHEME\) (= BELR, BER or CN); \(t_{vec}^{\text{SC}}\), runtime of the vectorized code in seconds without loops in the assembly process, using a particular method \(SCHEME\); \(t_{C}\), runtime for COMSOL Multiphysics. **Funding:** This research was funded by FAPESP (Fundacao de Amparo a Pesquisa do Estado de Sao Paulo) grant numbers 2016/19648-9, 2017/11428-2 and 2018/07643-8 and the Programa de Excelencia Academica (Proex)--Coordenacao de Aperfeicoamento de Pessoal de Nivel Superior--Brazil (CAPES)--Finance Code PROEX-9740044/D. The APC was funded by KTH Royal Institute of Technology. **Acknowledgments:** The sixth author acknowledges the award of a visiting researcher grant from the University of Sao Paulo. **Conflicts of Interest:** The authors declare no conflict of interest. The funders had no role in the design of the study; in the collection, analyses or interpretation of data; in the writing of the manuscript, or in the decision to publish the results. ## Appendix A Unvectorized Pseudocode

In this section, the unvectorized sets of pseudocode are presented. * **Pseudo-code for Algorithms A1-A6 the backward-Euler scheme with the left rectangular rule, corresponding to Equation (9) and employing (7):**

```
1:procedureBELRFEMPIDE(\(c4n,n4e,dir,N,dt\))
2:\(r\leftarrow\)NROW(\(c4n\))
3:\(U\leftarrow\)ZEROS(\(r,N+1\))
4:\(FN\leftarrow\)UNIQUE(\([1\;2\;\ldots\;])\(\backslash\)UNIQUE(\(dir\))
5:\(A\leftarrow\)StiffAssemb(\(n4e,c4n\))
6:\(B\leftarrow\)MassAssemb(\(n4e,c4n\))
7:\(U(:,1)\leftarrow\)U0(\(c4n\))
8:for\(n\gets 2\) to \((N+1)\),do
9:\(U(:,n)\leftarrow\)solveBELRR(\(c4n,n4e,dir,dt,FN,A,B.n,U\))
10:endfor
11:return\(U\)
12:endprocedure
```

**Algorithm A1** Backward-Euler scheme with left rectangular rule. ```
1:proceduresolveBELRR(\(c4n,n4e,dir,dt,FN,A,B,n,U\))
2:\(r\leftarrow\)NROW(\(c4n\))
3:\(b\leftarrow\)SPARSE(\(r,1\))
4:for\(j\gets 1\) to NROW(\(n4e\)),do
5:\(M_{1}\leftarrow[[1\;11\;]\);\(c4n(n4e(j,:,:)^{T}]\)
6:\(S\leftarrow\)SUM(\(c4n(n4e(j,:,:)\))
7:\(b(n4e(j,:))\gets b(n4e(j,:))+\text{DET}(M_{1})*dt*\frac{1}{6}*\text{F}( \frac{S}{3},(n-1)*dt)\)
8:endfor
9:\(Int\gets dt^{2}*A*U(:,1)*\textsc{integral}((n-1)*dt,0)\)
10:if\(n>2\),then
11:for\(k\gets 2\) to \((n-1)\),do
12:\(Int\gets Int+\left(dt^{2}*A*U(:,k)*\textsc{integral}((n-1)*dt,(k-1)*dt)\right)\)
13:endfor
14:endif
15:\(b\gets b+(B*U(:,n-1))+Int\)
16:\(u\leftarrow\)SPARSE(\(r,1\))
17:\(u_{d}\leftarrow\)UNIQUE(\(dir\))
18:\(u(u_{d})\leftarrow\)UD(\(c4n(u_{d},:),(n-1)*dt\))
19:\(b\gets b-(dt*A+B)*u\)
20:\(u(FN)\leftarrow\)INV(\(dt*A(FN,FN)+B(FN,FN))*b(FN\))
21:\(solBELR\gets u\)
22:return\(solBELR\)
23:endprocedure
```

**Algorithm A2** Backward-Euler scheme with left rectangular rule.