Beam Bending Analysis is a classic strength-of-materials problem. The beam has a length of 25 units and is subjected to distributed loads, concentrated forces, and moments. It is supported by pin, fixed, and elastic supports at specified points. Two independent approaches are implemented:
- Analytical solution – written in SageMath using the initial parameters method with Heaviside functions.
- Numerical solution – a finite element method (FEM) implemented in C++ (stiffness matrix assembly and solving a linear system).
Results from both methods are compared and visualised with a Python script.
├── data/ # Input data
│ ├── parameters.txt # Physical parameters (P, M, Q1, Q2, K, E, J)
│ ├── coordinates.txt # Coordinates of characteristic points
│ └── Балка_2025.xlsx # Loading scheme (Excel file)
├── src/
│ ├── sage/ # Analytical solution (Sage)
│ │ ├── beams_bending.sage
│ │ └── beams_bending.py
│ ├── fem/ # Finite element method (C++)
│ │ └── beams_bending.cpp
│ └── plotting/ # Visualisation
│ └── paint.py
├── results/ # Output data
│ ├── sage_res_*.txt # Deflections, angles, moments, forces (Sage)
│ ├── FEM_res_*.txt # Same for FEM
│ └── result_plot.png # Comparative plots
├── parameters.txt → data/parameters.txt # Symlinks for convenience
├── coordinates.txt → data/coordinates.txt
├── README.md
├── README.html
└── .gitignore
| Component | Required software / versions |
|---|---|
| Sage solution | SageMath ≥ 9.0 (supports symbolic computation and desolve) |
| C++ FEM | C++17 compiler (g++, clang), standard library |
| Python plotting | Python 3, matplotlib, numpy |
The easiest way to run the entire pipeline (analytical solution, FEM, and plotting) is to use the provided Makefile in the project root.
make all
This will compile the C++ FEM program (if needed), run the Sage analytical solution,
execute the FEM simulation, and finally generate the comparison plot.
All results will be saved in the results/ folder.
Other useful Make targets:
make sage– only the analytical solutionmake fem– compile and run the FEM codemake plot– generate the plot (runs Sage and FEM if necessary)make clean– remove all generated files (results, executable)make rebuild– clean + all
If you prefer to run each component separately, follow these steps from the project root:
-
Sage (analytical solution)
cd src/sage sage beams_bending.sageOutput files (
sage_res_*.txt) will appear inresults/. -
C++ FEM (numerical solution)
cd src/fem g++ -std=c++11 -Wall -O2 beams_bending.cpp -o beams_bending ./beams_bendingThe executable expects
parameters.txtandcoordinates.txtto be present (symlinks in the project root point todata/). Results are saved asFEM_res_*.txtinresults/. -
Plotting (Python)
cd src/plotting python3 paint.pyThis script reads the generated
sage_res_*.txtandFEM_res_*.txtfiles fromresults/and producesresult_plot.pngcomparing deflection, rotation, moment, and shear force.
After a successful run, the results/ directory will contain:
-
sage_res_*.txt– arrays of (x, W, θ, M, Q) from the analytical solution. The exact filename includes a timestamp or run identifier. -
FEM_res_*.txt– similar data from the finite element method. -
result_plot.png– a comparative plot showing both solutions for deflections, slopes, bending moments, and shear forces. This allows you to visually assess the accuracy and convergence of the FEM approach.
All text files are space-separated and can be easily imported into other tools (e.g., Excel, MATLAB, or further Python analysis).
Below is a sample comparison plot generated by the Python script. It shows deflection, slope, bending moment, and shear force from both methods.
The plot clearly demonstrates the agreement between the analytical solution (Sage) and the finite element method (C++).
This project is distributed under the MIT License.
See the LICENSE file in the repository for full details.
Feel free to use, modify, and redistribute the code for educational or commercial purposes.
@g30613740 (GitHub)
For questions or suggestions, please open an issue in the repository.
