codexoverhung-shaft-moment-diagram.py
overhung-shaft-moment-diagram.py
"""Original overhung shaft equilibrium diagram; Python 3.14, NumPy 2.3.5, Matplotlib 3.10.7.
Run from the output directory: python ../overhung-shaft-moment-diagram.py
Writes a white PNG to cwd, as required by the repository media Make workflow.
"""
from pathlib import Path
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
P=200.0
L=0.100
e=0.030
RA=P*e/L
RB=-P*(L+e)/L
z=np.unique(np.r_[np.linspace(0,L+e,501),L])
moment=RA*z+RB*np.maximum(z-L,0)
assert np.isclose(RA+RB+P,0)
assert np.isclose(RB*L+P*(L+e),0)
assert np.isclose(moment.max(),P*e)
assert np.isclose(moment[-1],0)
plt.rcParams.update({'font.size':10,'axes.spines.top':False,'axes.spines.right':False})
fig,(ax,bx)=plt.subplots(2,1,figsize=(8.5,4.8),dpi=100,gridspec_kw={'height_ratios':[1,1.25]},layout='constrained')
fig.patch.set_facecolor('white')
fig.suptitle('Overhung shaft: support force and internal moment',fontsize=13)
ax.plot([-4,134],[0,0],color='#25374a',lw=4,solid_capstyle='round')
ax.scatter([0,100],[0,0],color='black',s=38,zorder=4)
for x,y,label,tx in [(0,.62,'A: +60 N',4),(100,-.82,'B: -260 N',66),(130,.78,'P: +200 N',104)]:
ax.annotate('',xy=(x,y),xytext=(x,0),arrowprops={'arrowstyle':'-|>','color':'#b04430','lw':1.8})
ax.text(tx,y+(0.06 if y>0 else -0.13),label,color='#8c3023',fontsize=10)
ax.text(48,-.34,'L = 100 mm',ha='center')
ax.text(117,-.34,'e = 30 mm',ha='center')
ax.set(xlim=(-8,145),ylim=(-1.12,1.08))
ax.set_title('Chosen signs: upward force positive',fontsize=10,pad=0)
ax.axis('off')
bx.plot(z*1000,moment,color='#245c8b',lw=2.3)
bx.fill_between(z*1000,0,moment,color='#dceaf4')
bx.axvline(100,color='#69717b',linestyle='--',lw=1)
bx.scatter([0,100,130],[0,6,0],color='#245c8b',s=24,zorder=3)
bx.annotate('M at B = Pe = 6 N m',xy=(100,6),xytext=(28,6.5),arrowprops={'arrowstyle':'->','color':'#245c8b'},color='#245c8b')
bx.set(xlim=(-5,140),ylim=(-.35,7.3),xticks=[0,50,100,130],xlabel='Axial position z (mm)',ylabel='Bending moment M (N m)')
bx.grid(axis='y',alpha=.2)
fig.savefig(Path.cwd()/'overhung-shaft-moment-diagram.png',facecolor='white',dpi=100)
plt.close(fig)