Every mechanical engineering student solves beam deflection problems by hand. Doing the same calculation in Python takes about 30 lines of code and gives you something a hand calculation cannot: the full deflection curve, instant results for any material or load, and a graph ready for your report. In this tutorial we calculate and plot the beam deflection in Python for a simply supported beam, and compare a steel beam with an aluminium one.
Quick answer: For a simply supported beam of span L with a point load P at mid-span, the deflection at a distance x (up to L/2) is y = Px(3L² − 4x²) / 48EI, and the maximum deflection at the centre is PL³ / 48EI. In Python, write this as a function, evaluate it over np.linspace(0, L, 201) and plot it with Matplotlib.
The Problem
- Simply supported beam, span L = 2 m
- Point load P = 5 kN at mid-span
- Rectangular section 50 mm wide × 100 mm deep
- Two materials: steel (E = 200 GPa) and aluminium (E = 69 GPa)
We want the deflection along the whole beam, the maximum deflection and the maximum bending stress.
Step 1: The Theory
For a rectangular section, the second moment of area is I = bh³/12 = 0.05 × 0.1³ / 12 = 4.167 × 10⁻⁶ m⁴.
The maximum bending moment for a central point load is M = PL/4 = 5000 × 2 / 4 = 2500 N·m, and the maximum bending stress is σ = Mc/I, where c = h/2 = 0.05 m. That gives σ = 30 MPa – the same for both materials, because stress does not depend on E.
The maximum deflection is δ = PL³/48EI. For steel: 5000 × 2³ / (48 × 200×10⁹ × 4.167×10⁻⁶) = 1.00 mm. Doing this by hand for every point along the beam is slow – this is where Python helps.
Step 2: The Python Code
You need Python with NumPy and Matplotlib (pip install numpy matplotlib). The function uses symmetry: the deflection on the right half mirrors the left half, so we only need the formula for x ≤ L/2.
import numpy as np
import matplotlib.pyplot as plt
# Beam and load data (SI units)
L = 2.0 # span, m
P = 5000.0 # central point load, N
b, h = 0.05, 0.10 # rectangular section, m
I = b * h**3 / 12 # second moment of area, m^4
c = h / 2 # distance to outer fibre, m
materials = {"Steel": 200e9, "Aluminium": 69e9} # Young's modulus, Pa
def deflection_ss_center(x, P, L, E, I):
"""Simply supported beam, point load at mid-span (valid for 0 <= x <= L)."""
x = np.where(x <= L / 2, x, L - x) # use symmetry
return P * x * (3 * L**2 - 4 * x**2) / (48 * E * I)
x = np.linspace(0, L, 201)
M_max = P * L / 4
sigma_max = M_max * c / I
print(f"I = {I:.4e} m^4")
print(f"Max bending moment = {M_max:.0f} N·m")
print(f"Max bending stress = {sigma_max/1e6:.1f} MPa")
plt.figure(figsize=(8, 4))
for name, E in materials.items():
y = deflection_ss_center(x, P, L, E, I)
print(f"{name}: max deflection = {y.max()*1000:.2f} mm")
plt.plot(x, -y * 1000, label=f"{name} (E = {E/1e9:.0f} GPa)")
plt.xlabel("Position along beam, x (m)")
plt.ylabel("Deflection (mm)")
plt.title("Simply supported beam, 5 kN load at mid-span")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.savefig("beam_deflection.png", dpi=150)
plt.show()
Step 3: The Results
Running the script prints:
I = 4.1667e-06 m^4
Max bending moment = 2500 N·m
Max bending stress = 30.0 MPa
Steel: max deflection = 1.00 mm
Aluminium: max deflection = 2.90 mm
The Python result for steel matches the hand calculation exactly, which is the first check you should always make. The plot shows both curves:

What the Graph Tells You
- Maximum deflection is at mid-span, under the load, and zero at both supports – as the boundary conditions require.
- Aluminium deflects 2.9 times more than steel, exactly the ratio of their Young’s moduli (200 / 69). Deflection is inversely proportional to E.
- Bending stress is the same (30 MPa) for both. Material choice changes stiffness, not stress, for a statically determinate beam.
- Check against a common serviceability limit of span/360 (5.6 mm here): both beams pass, but aluminium has less margin.
Common Mistakes
- Mixing units – keep everything in N, m and Pa, and convert to mm only for the output.
- Using the formula past mid-span – the x(3L² − 4x²) form is valid only for x ≤ L/2; use symmetry for the rest.
- Wrong I – bh³/12 uses the depth h in the direction of bending. Swapping b and h gives a beam four times more flexible here.
Try It Yourself: Extend the Code
- Add a cantilever with an end load, where y = Px²(3L − x) / 6EI.
- Add a uniformly distributed load and compare it with the point load.
- Plot the shear force and bending moment diagrams under the deflection curve.
- Loop over section depths to find the lightest beam that meets the span/360 limit.
- Compare your Python results with an FEA model of the same beam.
Steps 3 to 5 together make a good Python-based mechanical project. Next, try heat exchanger design in Python (LMTD method). See more ideas in Python projects for final year students.
Next tutorial: how to plot CFD and FEA results with Python Matplotlib – mesh study and validation graphs for your report.
Learn Python for Mechanical Engineering
This tutorial is part of how we teach Python to mechanical and aerospace students – through engineering problems you already know, not abstract examples. In our Python for mechanical engineers classes you learn NumPy, Matplotlib and data handling step by step, in live screen-share sessions or recorded videos, and write the code yourself.
Frequently Asked Questions
How do you calculate beam deflection in Python?
Write the deflection formula for your beam and load case as a Python function, create an array of positions along the beam with NumPy, evaluate the function at every point, and plot the result with Matplotlib. For a simply supported beam with a central load the maximum deflection is PL³/48EI.
Which Python libraries are used for beam calculations?
NumPy for the calculations and arrays, and Matplotlib for the deflection and bending moment plots. SymPy is useful if you want Python to derive the equations symbolically.
Can Python replace FEA software for beam analysis?
For standard beams with textbook load cases, Python gives the same answer as FEA and is a quick way to check FEA results. For complex 3D geometry, contact or non-linear materials, FEA software is still needed.
Why does aluminium deflect more than steel?
Deflection is inversely proportional to Young’s modulus. Aluminium has about one third of the modulus of steel (69 GPa vs 200 GPa), so the same beam deflects about 2.9 times more.
Is this a good final year project for mechanical students?
A beam calculator on its own is a small task. Extended into a tool that handles several supports and load types, draws shear force and bending moment diagrams and compares results with FEA, it becomes a strong Python-based final year project.
Start Learning
Tell us your branch and what you want to build. Message us on WhatsApp or use the enquiry form on our home page.
Interested in this project? Get the details, files and guidance from our team.
Enquire on WhatsApp