Skip to content

Instantly share code, notes, and snippets.

@mtrebitsch
Created March 28, 2018 15:34
Show Gist options
  • Save mtrebitsch/d5433561e6dc922dbbeb3fe56e3c7931 to your computer and use it in GitHub Desktop.
Save mtrebitsch/d5433561e6dc922dbbeb3fe56e3c7931 to your computer and use it in GitHub Desktop.
Quick and dirty script to generate Chladni patterns from a square plate, inspired from https://twitter.com/mcnees/status/978996740438315008
def chladni(n, m, N=100):
x = np.linspace(-1,1,N)
y = np.linspace(-1,1,N)
xx, yy = np.meshgrid(x, y)
sgn = -1 if n>m else 1
return np.cos(n*np.pi*xx/2.)*np.cos(m*np.pi*yy/2.) + sgn*np.cos(m*np.pi*xx/2.)*np.cos(n*np.pi*yy/2.)
def all_chladni(nr=10, mr=10):
fig, ax = plt.subplots(nr, mr, figsize=(nr+2.5,mr+2.5), sharex=True, sharey=True)
for n in range(nr):
for m in range(mr):
plate = chladni(n,m,N=100)
ax[n,m].set_aspect('equal')
ax[n,m].set_title(r'$n=%d,\ m=%d$'%(n,m), fontsize=10)
ax[n,m].tick_params(axis='both', which='both', bottom='off', top='off', labelbottom='off', right='off', left='off', labelleft='off')
if plate.min() <= 0 <= plate.max():
ax[n,m].contour(plate, 0, extent=(-1, 1, -1, 1), color='k')
ax[0,0].set_xlim(-1.,1.)
ax[0,0].set_ylim(-1.,1.)
fig.suptitle(r'$\cos\left(\frac{n\pi x}{L}\right) \cos\left(\frac{m\pi y}{L}\right) \pm \cos\left(\frac{m\pi x}{L}\right)\cos\left(\frac{n\pi y}{L}\right) $', fontsize=9+np.maximum(nr,mr))
fig.subplots_adjust(top=.88, bottom=0.08, hspace=.5, wspace=.5)
fig.savefig('chladni_{}_{}.pdf'.format(nr, mr))
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment