Linear static analysis of planar (2D) and space (3D) truss structures using the Finite Element Method and the Direct Stiffness Method — implemented from scratch in pure MATLAB.
Features · Theory · Inputs · Results · Getting Started · Assumptions
This repository provides two standalone MATLAB scripts for truss structural analysis:
| Solver | Structure | DOFs per Node | Element Type |
|---|---|---|---|
truss2D.m |
Planar truss | 2 (UX, UY) |
2-node axial bar |
truss3D.m |
Space truss | 3 (UX, UY, UZ) |
2-node axial bar |
Both solvers implement the complete linear static finite element workflow — from model definition (nodes, elements, loads, boundary conditions) through global stiffness matrix assembly, system solution, and post-processing of axial forces, stresses, strains, and yield-stress utilization — without relying on any external structural-analysis solver or toolbox.
The project is intended for educational, computational mechanics, structural analysis, and engineering programming applications.
| Feature | 2D | 3D |
|---|---|---|
| Direct stiffness method | ✅ | ✅ |
| Element stiffness matrix formulation | ✅ | ✅ |
| Global stiffness matrix assembly | ✅ | ✅ |
| Nodal load definition | ✅ (magnitude + angle) | ✅ (components Fx Fy Fz) |
| Flexible boundary-condition definition | ✅ | ✅ (per-DOF flags) |
| Static nodal displacement solution | ✅ | ✅ |
| Element elongation (µm) | ✅ | ✅ |
| Axial force (N) | ✅ | ✅ |
| Normal stress (MPa) | ✅ | ✅ |
| Strain (microstrain, µε) | ✅ | ✅ |
| Yield-stress utilization (%) | ✅ | ✅ |
| Members > 100% utilization highlighted in red | ✅ | ✅ |
| Undeformed + deformed geometry plot | ✅ | ✅ |
| Node & element labeling | ✅ | ✅ |
| Command-window result reporting | ✅ | ✅ |
The solver follows the standard linear static finite element procedure for truss structures.
The global governing system is:
where:
| Symbol | Meaning |
|---|---|
[K] |
Global stiffness matrix |
{u} |
Global nodal displacement vector |
{F} |
Global nodal force vector |
With c = cos(θ) and s = sin(θ) derived from the element orientation:
The 3D formulation uses the direction cosines of the element axis:
The element stiffness matrix is written as:
with:
- Assemble the full global stiffness matrix
[K]by direct stiffness summation. - Apply boundary conditions by eliminating the rows and columns of restrained DOFs.
- Solve the reduced system for free-DOF displacements.
- Recover the complete displacement vector (zero at restrained DOFs).
All model data are defined directly at the top of each script.
| Quantity | Unit |
|---|---|
| Coordinates | m |
Cross-sectional area A |
m² |
Young's modulus E |
Pa |
| Yield stress | Pa |
| Forces | N |
| Force angle (2D only) | degrees |
Nodes — [x y] per row:
data{1,1}.nodes = [0 0
3.8 9.12/7
...];Elements — [node1 node2 A E yieldStress]:
data{1,1}.elements = [1 2 0.0005 200e9 20e7
...];Forces — [nodeNumber F angle], with components computed as Fx = F·cos(θ), Fy = F·sin(θ) (θ in degrees):
data{1,1}.forces = [1 5700 -90
...];Supports — [nodeNumber type orien]:
type |
orien |
Restrained DOFs |
|---|---|---|
1 |
1 |
UX only |
1 |
2 |
UY only |
2 |
any | UX + UY (pin) |
data{1,1}.supports = [1 2 0 % pin at node 1
8 1 2]; % UY restraint at node 8Nodes — [x y z] per row.
Elements — [node1 node2 A E yieldStress] (same format as 2D).
Forces — direct Cartesian components [nodeNumber Fx Fy Fz]:
data{1,1}.forces = [1 0 0 -25000
...];Supports — [nodeNumber Dx Dy Dz], where each translational DOF is defined independently:
| Flag | Meaning |
|---|---|
0 |
Free |
1 |
Restrained |
For example [3 1 1 1] fully fixes node 3 in X, Y, and Z.
- Define nodal coordinates.
- Define element connectivity, area, elastic modulus, and yield stress.
- Compute element lengths and orientation parameters (angle / direction cosines).
- Build element stiffness matrices.
- Assemble the global stiffness matrix.
- Assemble the global load vector.
- Apply boundary conditions.
- Solve the reduced linear system.
- Recover the full displacement vector.
- Compute element elongation and axial force.
- Compute element stress and strain.
- Compute yield-stress utilization.
- Visualize undeformed and deformed configurations.
Per-element results are reported in the MATLAB command window:
| Quantity | Symbol | Unit | Sign Convention |
|---|---|---|---|
| Elongation | ΔL |
µm | positive = elongation |
| Axial force | N |
N | positive = tension |
| Normal stress | σ = N/A |
MPa | positive = tension |
| Strain | ε |
µε (microstrain) | positive = elongation |
| Utilization | — | % | always positive |
Yield-stress utilization is computed as:
Members whose utilization exceeds 100% are drawn in red in the structure plot.
Each script produces a labeled plot of the structure:
| Item | Representation |
|---|---|
| Undeformed members | Blue (red if utilization > 100%) |
| Deformed configuration | Magenta overlay (indicative visual aid) |
| Node numbers | Labeled at node positions |
| Element numbers | Labeled at member midpoints |
| Supports | Marked at restrained nodes |
3D support marker colors indicate the restrained DOF combination:
Dx Dy Dz |
Color |
|---|---|
1 1 1 |
⚫ Black |
1 1 0 |
⚪ White |
1 0 0 |
🟢 Green |
0 1 0 |
🔵 Cyan |
0 0 1 |
🟣 Magenta |
0 1 1 |
🫒 Olive |
1 0 1 |
🟠 Orange |
0 0 0 |
🔵 Blue (free) |
MATLAB-Truss-Analysis/
├── 2D/
│ └── truss2D.m
├── 3D/
│ └── truss3D.m
├── LICENSE
└── README.md
Finite Element Analysis (FEA) of a 2D planar truss, showing the deformed configuration.
Three-dimensional space truss analysis using the Finite Element Method (FEM), including structural deformation and visualization of failed elements.
- MATLAB (base installation — no toolboxes required)
git clone https://github.com/amirhh-04/MATLAB-Truss-Analysis.git
cd MATLAB-Truss-Analysis% 2D planar truss
run('2D/truss2D.m')
% 3D space truss
run('3D/truss3D.m')To model your own structure, edit the Inputs Section at the top of the corresponding script (nodes, elements, forces, supports) and re-run it.
The implementation performs linear static analysis of ideal truss structures.
- Members carry axial force only (pin-jointed idealization)
- Linear elastic material behavior
- Constant cross-sectional properties per element
- Small displacements
- Static loads
- Translational DOFs only
- Geometric nonlinearity
- Material plasticity
- Buckling analysis
- Dynamic / transient / modal analysis
- Fatigue analysis
- Joint slip modeling
- Design-code compliance checking
Notes:
- Reported strain is expressed in microstrain (µε).
- The deformed-geometry overlay is provided as a qualitative visual aid alongside the exact numerical element results.
- The nodal force vector
[K]{u}is computed internally; explicit support reaction reporting is planned for a future release.
The project should be considered an educational and computational-mechanics implementation, not a certified structural design package.
- Support reaction reporting
- Deformation scale factor control
- Improved input validation
- Verification & benchmark examples
- Integration of the GUI version
- Expanded material and section libraries
- Structural optimization features
Issues and pull requests are welcome — feel free to open an issue for bug reports, benchmark models, or feature suggestions.
This project is licensed under the MIT License — see the LICENSE file for details.
Amirhh
| 🐙 GitHub | amirhh-04 |
| 🌐 Website | amirhh.ir |
| amirhh |
MATLAB · Finite Element Method · FEM · FEA · Finite Element Analysis · Truss Analysis · Truss Solver · 2D Truss · 3D Truss · Space Truss · Structural Analysis · Structural Engineering · Direct Stiffness Method · Stiffness Matrix · Computational Mechanics · Mechanical Engineering


