import numpy as np
import matplotlib.pyplot as plt
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 3.2), gridspec_kw={'width_ratios': [1.3, 1]})
lw, col = 1.5, 'k'
# --- (a) Aufbau: Wand, Feder, Dämpfer, Masse ---
ax1.set_xlim(-0.2, 7.2)
ax1.set_ylim(-0.6, 4.4)
ax1.set_aspect('equal')
ax1.axis('off')
# Wand und Unterlage mit Schraffur
ax1.plot([0.6, 0.6], [0.3, 3.9], color=col, lw=2)
for y in np.arange(0.5, 4.0, 0.4):
ax1.plot([0.6, 0.25], [y, y - 0.3], color=col, lw=1)
ax1.plot([0.6, 6.8], [0.3, 0.3], color=col, lw=1.5)
for x in np.arange(0.9, 6.8, 0.4):
ax1.plot([x, x - 0.3], [0.3, 0.0], color=col, lw=1)
def feder(ax, x0, x1, y, n=6, h=0.3):
xs, ys = [x0, x0 + 0.3], [y, y]
dx = (x1 - x0 - 0.6) / (2 * n)
for i in range(2 * n):
xs.append(x0 + 0.3 + (i + 0.5) * dx)
ys.append(y + h if i % 2 == 0 else y - h)
xs += [x1 - 0.3, x1]
ys += [y, y]
ax.plot(xs, ys, color=col, lw=lw)
# Feder (oben) und Dämpfer (unten) zwischen Wand und Masse
xm = 3.6 # linke Kante der Masse
feder(ax1, 0.6, xm, 3.0)
ax1.text(2.1, 3.55, '$k$', ha='center', fontsize=12)
yd = 1.4
ax1.plot([0.6, 1.6], [yd, yd], color=col, lw=lw)
ax1.plot([1.6, 2.5, 2.5, 1.6], [yd + 0.35, yd + 0.35, yd - 0.35, yd - 0.35], color=col, lw=lw)
ax1.plot([2.2, 2.2], [yd + 0.3, yd - 0.3], color=col, lw=lw)
ax1.plot([2.2, xm], [yd, yd], color=col, lw=lw)
ax1.text(2.1, 0.55, '$c$', ha='center', fontsize=12)
# Masse
ax1.add_patch(plt.Rectangle((xm, 0.3), 1.4, 3.5, facecolor='lightgray', edgecolor=col, lw=lw))
ax1.text(xm + 0.7, 2.05, '$m$', ha='center', va='center', fontsize=13)
# Ruhelage und Koordinate x
ax1.plot([xm + 0.7, xm + 0.7], [3.8, 4.3], color='gray', lw=1, ls='--')
ax1.annotate('', xy=(6.6, 4.1), xytext=(xm + 0.7, 4.1),
arrowprops=dict(arrowstyle='->', color=col, lw=1.2))
ax1.text(6.0, 4.25, '$x$', ha='center', fontsize=12)
ax1.text(xm + 0.7, 4.4, '$x = 0$', ha='center', fontsize=9, color='gray')
ax1.text(-0.1, 4.1, '(a)', fontsize=12, fontweight='bold')
# --- (b) Freikörperbild ---
ax2.set_xlim(-3.6, 3.6)
ax2.set_ylim(-0.6, 4.4)
ax2.set_aspect('equal')
ax2.axis('off')
ax2.add_patch(plt.Rectangle((-0.7, 0.3), 1.4, 3.5, facecolor='lightgray', edgecolor=col, lw=lw))
ax2.text(0, 2.05, '$m$', ha='center', va='center', fontsize=13)
# Federkraft und Dämpfungskraft wirken nach links
ax2.annotate('', xy=(-3.1, 2.9), xytext=(-0.7, 2.9),
arrowprops=dict(arrowstyle='-|>', color='red', lw=2, mutation_scale=18))
ax2.text(-1.9, 3.15, r'$F_\mathrm{k} = -kx$', ha='center', fontsize=11, color='red')
ax2.annotate('', xy=(-3.1, 1.2), xytext=(-0.7, 1.2),
arrowprops=dict(arrowstyle='-|>', color='blue', lw=2, mutation_scale=18))
ax2.text(-1.9, 0.5, r'$F_\mathrm{d} = -c\dot{x}$', ha='center', fontsize=11, color='blue')
# Auslenkung und Geschwindigkeit zeigen nach rechts
ax2.annotate('', xy=(2.6, 2.9), xytext=(0.7, 2.9),
arrowprops=dict(arrowstyle='->', color='gray', lw=1.5))
ax2.text(1.65, 3.15, '$x > 0$', ha='center', fontsize=10, color='gray')
ax2.annotate('', xy=(2.6, 1.2), xytext=(0.7, 1.2),
arrowprops=dict(arrowstyle='->', color='gray', lw=1.5))
ax2.text(1.65, 0.5, r'$\dot{x} > 0$', ha='center', fontsize=10, color='gray')
ax2.text(-3.5, 4.1, '(b)', fontsize=12, fontweight='bold')
plt.tight_layout()
plt.show()