1. Introduction
2. Topology Optimization
3. Edge-based Smoothed Finite Element Method
4. MATLAB Implementation
4.1 Main program
4.2 Optimality Criteria solver
4.3 Sensitivity filter
4.4 Edge-based S-FEM code
4.5 MALTAB code extensions
5. Conclusion
1. Introduction
MATLAB is an interpreted programming language and numerical computing environment widely used in engineering, science, economics, and related fields (MathWorks, 2025). Although it is generally less computationally efficient than lower-level languages such as C++ or Fortran for large-scale problems, MATLAB offers extensive built-in functions, specialized toolboxes, and powerful visualization capabilities. Consequently, it remains highly popular for teaching, rapid prototyping, and validation. In structural topology optimization, in particular, MATLAB is frequently used in educational settings because it enables concise and readable implementations that are well suited for instructional purposes.
Topology optimization has become an effective design strategy across diverse engineering applications, aiming to identify optimal material distributions within a design domain subject to physical and geometric constraints (Lee, 2003). Building on Sigmund’s (2001) pedagogical MATLAB implementation, which introduced a concise, problem-oriented code, numerous MATLAB-based approaches using the finite element method (FEM) have since been developed. These include further code simplification (Andreassen et al., 2011), evolutionary structural optimization (Huang and Xie, 2010), level-set methods (Challis, 2010), polygonal meshes (Talischi et al., 2012), and three-dimensional formulations (Liu and Tovar, 2014). Additionally, MATLAB-based topology-optimization codes are available for the geometrically nonlinear structures (Chen et al., 2019), the finite-volume method (Araujo et al., 2024), body-fitted meshing (Zhuang et al., 2024), and thermomechanical loading with steady-state heat conduction (Ooms et al., 2023).
Most MATLAB-based topology optimization studies employ FEM with four-node quadrilateral (Q4) elements because Q4 formulations are algebraically simple, align naturally with MATLAB’s matrix-oriented data structures, and support highly vectorized assembly and filtering routines, thereby keeping both research and teaching codes concise and efficient.
As an extension of conventional FEM-based topology optimization, the smoothed finite element method (S-FEM) with three-node triangular (T3) meshes has been explored (Lee et al., 2021, 2022). Compared with standard FEM, certain S-FEM variants often achieve higher accuracy for a given number of degrees of freedom and tend to produce clearer, less noisy layouts (Liu and Trung, 2016). Benchmark studies have reported comparable or improved compliance reduction with smoother material boundaries. However, S-FEM is typically less computationally efficient: assembling smoothed contributions and forming the global stiffness matrix increase bandwidth and fill-in, thereby raising memory demands and solution time.
To address these trade-offs, this study introduces the edge-based smoothed finite element method (ES-FEM) into density-based topology optimization and implements it in MATLAB, following the concise style of Sigmund’s 99-line code. The goal is pedagogical: to provide a compact instructional tool for learners of both topology optimization and ES-FEM. As a reference framework, the Solid Isotropic Material with Penalization (SIMP) model is employed together with an Optimality Criteria (OC) update scheme, and the density filter of Andreassen et al. (2011) is incorporated to improve robustness and reduce mesh dependence. The main contribution is a simplified procedure for constructing edge-based smoothing domains in both interior and boundary regions and assembling the corresponding smoothed stiffness matrix. With this approach, the ES-FEM topology optimization solver is realized in just 175 lines of MATLAB, maintaining conciseness while improving clarity and computational efficiency.
The paper is organized as follows: Section 2 reviews the formulation of density-based topology optimization and Section 3 summarizes ES-FEM. Section 4 describes the MATLAB implementation and extensions to boundary conditions, multiple load cases, and heat conduction. Section 5 presents observations and discussion
2. Topology Optimization
In topology optimization, the objective is to minimize the structural compliance subject to standard constraints, expressed as:
where is the vector of design variables, is the global edge-based smoothed stiffness matrix, U is the global displacement vector, and F is the global force vector. The terms and denote the smoothed stiffness matrix and displacement vector associated with the e-th smoothing domain, respectively. The lower bound is a small positive constant introduced to prevent singularity, and the penalization power =3 is utilized in this study. Moreover, denotes the material volume, is the design domain volume, and is the prescribed volume fraction.
The optimization problem defined in Eq. (1) is solved using the OC method, with the heuristic updating scheme given by:
where =0.2 is the move limit and 𝜂=1/2 is the numerical damping coefficient. The coefficient in Eq. (2) is derived from the optimality condition as:
where 𝜆 is the Lagrange multiplier, determined iteratively using a bisection algorithm.
The sensitivity of the objective function with respect to the design variable of smoothing domain is:
To improve robustness, Eq. (3) is modified by applying a sensitivity filter, expressed as:
where 𝛾=10-3, and denotes the set of smoothing domains whose centroid-to-centroid distance from domain is smaller than the filter radius . The weighting function is defined as:
Eq. (5) and (6) represent one of the main differences from Sigmund(2001), contributing to both improved computational efficiency and a simplification of the MATLAB implementation.
3. Edge-based Smoothed Finite Element Method
This section briefly revisits the fundamental concept of ES-FEM. The main distinction from the standard FEM is that: (a) each element is subdivided into sub-cells; (b) these sub-cells are rebuilt to form smoothing domains; and (c) the stiffness matrix is evaluated over the smoothing domains rather than the original elements. In ES-FEM, smoothing domains are constructed along the edges of T3 elements together with their adjacent sub-cells, as illustrated in Fig. 1.
The infinitesimal strain over each smoothing domain is defined as:
where denotes a representative point on the boundary of smoothing domain, and is the compatible strain field.
The weight function is introduced such that:
where is the area of smoothing domain . By applying divergence theorem to Eq. (7) and using Eq. (8), the smoothed strain can be expressed as:
where denotes the two-dimensional matrix form of the outward normal vector on boundary of the smoothing domain.
From Eq. (9), the smoothed strain-displacement matrix is obtained as:
where and is the shape function associated with node .
The smoothed stiffness matrix using Eq. (10) is then assembled as:
where is the two-dimensional linear constitutive matrix. The external force vector is computed in the same manner as in the standard FEM. In addition, Eq. (11) is also employed in evaluating the sensitivity of the objective function given in Eq. (4).
4. MATLAB Implementation
The proposed ES-FEM for structural topology optimization is implemented in MATLAB, as provided in Appendix A. The code can be executed as a user-defined function by entering the following command in the MATLAB command window:
where nelx and nely are the number of elements in - and -directions, volfrac is the prescribed volume fraction, penal is the penalization power, and rmin is the filter size.
4.1 Main program
The main program (Lines 1-48) begins with the constitutive matrix under the plane stress assumption (Lines 4-5), followed by the generation of triangular meshes for the rectangular domain (Lines 6-10). In this implementation, each triangular element has unit base and height; thus, the number of elements along the - and -directions directly correspond to the geometric dimensions of the domain. The nodes and element numbering convention used in this code is illustrated in Fig. 2.
Once the mesh is generated, the edge-based smoothing domains for both interior and boundary regions are constructed, and the smoothed stiffness matrices are precomputed (Line 15). The optimization loop (Lines 22-47) follows, beginning with the solution of the equilibrium equations (Line 26) to obtain the global displacement vector U.
The objective function and its sensitivity are evaluated on each smoothing domain (Lines 28-35). Sensitivity filtering is applied in Line 37, and the Optimality Criteria (OC) update is performed in Line 39.
During each iteration, compliance, volume fraction, and the maximum change in design variables are printed (Line 42), while the current density distribution is visualized (Lines 44-46). The optimization process terminates once the maximum change in the design variables falls below 0.01.
4.2 Optimality Criteria solver
Design variable updates are performed using the OC method (Lines 50-61). The implementation closely follows the OC function in Sigmund (2001), with the only modification being the inclusion of the smoothing domain weight vector W in the update rule:
A bisection search adjusts the Lagrange multiplier 𝜆 until the prescribed volume fraction is satisfied, while simultaneously enforcing move limits (± 0.2) and design bounds [0.001,1].
4.3 Sensitivity filter
A sensitivity filter based on Eq. (5) is adopted from Andreassen et al. (2011). The filter computes a weighted average of sensitivities across neighboring smoothing domains, thereby reducing numerical instabilities and mesh dependence. In the code, the filter is initialized in Lines 17-18, where pairwise distances between domain centroids are calculated, and the sparse weight matrix H=max(0,rmin-dist) is assembled along with its row-sum vector Hs=sum(H,2). Here, H stores the distance-based weights, while Hs ensures normalization. During the optimization loop, the filtered sensitivities are evaluated in Line 37 as:
In this expression, x.*dc represents the weighted sensitivities, multiplication by H accumulates contributions from neighboring domains, and division by x.*Hs normalizes the sum. The safeguard max(1e-3,x.*Hs) prevents division by very small numbers. This procedure ensures that each design variable is smoothly influenced by its neighbors, resulting in stable convergence and improved robustness of the optimization.
4.4 Edge-based S-FEM code
The core ES-FEM routines are implemented in Lines 62-157, where smoothing domains are constructed along element edges and the corresponding smoothed stiffness matrices are computed. Detection of interior and boundary edges is performed using an edge map in Lines 65-81. Each triangular element contributes three edges that are stored in a map structure; edges shared by two adjacent triangles form interior domains, while edges belonging to a single triangle form boundary domains.
For each domain, the polygon is assembled from the edge endpoints and centroids. Its orientation is checked and corrected using the shoelace formula and the area is evaluated (Lines 100-111 and 131-135):
Outward unit normals along polygon edges are integrated in closed form, giving the coefficients in Eq. (10) (Lines 116-125 and 140-147):
t = cL-xi; L = hypot(t(1),t(2));
n = [t(2),-t(1)]/max(L,eps);
C = C+n'*(L*[2/3 1/6 1/6 0]); Btil = C/As;
These coefficients are then used to assemble the smoothed strain-displacement matrix , and the local stiffness matrix is computed as (given in Eq. (11)) implemented in Lines 152-156:
(1,1:2:end)=Btil(1,:); B(2,2:2:end)=Btil(2,:);
B(3,1:2:end)=Btil(2,:); B(3,2:2:end)=Btil(1,:);
M_dom_e=As*(B'*D*B);
Interior domains are assigned to an average area weight , while boundary domains use . This closed-form design eliminates the need for numerical quadrature, keeping the implementation concise while remaining directly linked to the ES-FEM formulation. Global assembly of the smoothed stiffness matrices is performed in a domain-wise loop, after which boundary conditions and external loads are applied, and the systems is solved in the same as in Sigmund (2001).
4.5 MALTAB code extensions
4.5.1 Messerschmitt-Bölkow-Blohm (MBB)
The present code is first demonstrated on the classical Messerschmitt-Bölkow-Blohm (MBB) beam problem using:
and the resulting optimal topology is presented in Fig. 3.
4.5.1 Multiple loading beam
The first extension addresses multiple load cases. In this study, two independent loadings are applied, with the corresponding boundary conditions and external forces are illustrated in Fig. 4(a). For this case, the force and displacement vectors must be defined with two columns, which requires modifying Lines 170-173 as follows:
F = sparse(2*nx*ny,2);
F(2*nx*ny,1) = 1;
F(2*(nx-1)*ny+2,2) = -1;
fixeddofs = 1:2*ny;
freedofs = setdiff(1:2*nx*ny, fixeddofs);
U = zeros(2*nx*ny,2);
Since the objective function must now account for the sum of the two compliances, Lines 33-34 are replaced by the following loop:
for i=1:2
c = c + x(e)^penal*U(edof,i)'*M_dom{e}*U(edof,i);
dc(e) = dc(e) -
penal*x(e)^(penal-1)*U(edof,i)'*M_dom{e}*U(edof,i);
end
Using these modifications, and with the chosen input parameters, the optimal topology for the two-load case is obtained with:
as shown in Fig. 4(b).
4.5.2 Passive domain
In some optimization problems, certain regions are constrained to remain at a minimum density. A common example is the presence of a passive domain, such as a hole or an excluded region within the design space. To demonstrate this feature, an L-shaped beam with a passive domain located in the top-right corner of the square design domain is considered (Fig. 5(a)). To enforce the minimum density in passive elements, the following line needs to be inserted before the start of the optimization loop: x(passive==1)=0.001;. Passive elements are defined, and the boundaries of the passive domain are detected, by replacing the smoothing-domain construction call in Line 15 with: [M_dom,nodes_dom,cent_dom,W,tri2dom,passive,ne]=build_smoothing_domains(T,cent,A_tri,XY,D,nt,nelx,nely);. In this modified function, the active-passive interface is handled carefully: edges along the passive boundary are treated as boundary domains on the active side only, ensuring that passive flags and associated weights are defined consistently.
For this case, the force and displacement vectors are defined as two-column vectors, requiring the following modification of Lines 170-172:
F = sparse(2*nx*ny,2);
F(2*nx*ny,1) = 1;
F(2*(nx-1)*ny+2,2) = -1;
fixeddofs = 1:2*ny;
freedofs = setdiff(1:2*nx*ny, fixeddofs);
U = zeros(2*nx*ny,2);
The passive-domain example is solved using:
The resulting optimal topology is shown in Fig. 5(b). Compared with the standard case, the passive region in the top-right corner remains fixed at the minimum density throughout optimization, effectively constraining the design space. This alters the load path and redistributes material around the excluded region, demonstrating that the code can accommodate geometric restrictions.
The fully updated MATLAB functions for defining active-passive smoothing domains and computing the associated smoothed stiffness matrices are available at: https://sourceforge.net/projects/topopt-esfem/files/
4.5.3 Heat conduction
This final extension addresses heat-conduction topology optimization. A Dirichlet boundary condition is applied at (and immediately around) the midpoint of the top boundary (Fig. 6(a)) with prescribed temperature =0℃; the remaining boundaries are insulated (zero flux). In the code, the solid/void conductivities and the isotropic conductivity matrix are set in Line 4-5 as:
A uniform volumetric heat source is applied throughout the domain, and the Dirichlet anchor is enforced by fixing a small stencil of nodes around the top-midpoint for numerical stability:
F = sparse(0.01*ones(nx*ny,1));
mid_x = round((nx-1)/2);
fixeddofs = [(mid_x+1)*nx-ny, (mid_x+1)*ny, (mid_x+1)*nx+ny];
freedofs = setdiff(1:nx*ny,fixeddofs);
U = zeros(nx*ny,1);
Because temperature is a scalar quantity per node, each domain’s smoothed matrix is taken directly as B=Btil; (no Voigt expansion), replacing Lines 153-156 in smoothing_domain_stiffness with the scalar form and adjusting node indices accordingly. Material interpolation follows the standard SIMP law for conductivity, , which is applied in both the stiffness matrix and the sensitivity. Thus, Lines 32-34 are replaced with:
edof = node_dom{e};
c = c +
(kmin+(1-kmin)*x(e)^penal)*U(edof)'*M_dom{e}*U(edof);
dc(e) = -penal*(1-kmin)*x(e)^(penal-1)*
U(edof)'*M_dom{e}*U(edof);
The optimal heat-conduction topology is obtained using:
as shown in Fig. 6(b).
5. Conclusion
This paper has presented a compact MATLAB implementation of topology optimization using the edge-based smoothed finite element method (ES-FEM). In line with the pedagogical intent of Sigmund’s 99-line code (2001), the objective was to provide newcomers with an accessible example that incorporates ES-FEM, while also serving as a foundation for further research and extensions. The entire implementation requires only 175 lines of code, including the optimizer, sensitivity filtering, construction of edge-based smoothing domains, and computation of the smoothed stiffness matrix through line integration.
By combining an extended density filter with a simplified ES-FEM formulation, the code achieves both clarity and computational efficiency. The framework is flexible, allowing various boundary condition problems to be addressed with only minor modifications. Moreover, the Method of Moving Asymptotes (MMA) can be employed in place of the Optimality Criteria (OC) update with minimal changes to the implementation.
The complete MATLAB code, along with separate files corresponding to the extensions discussed in this paper, is openly available for download at: https://sourceforge.net/projects/topopt-esfem/files/









