The Rastrigin function is often used when exploring numerical optimization because the function has one true minimum value at (x=0, y=0) but many local minima nearby.
I’ve graphed the Rastrigin function using R, MatLab and its clone SciLab, Excel, Python, and other tools. I’ve been using a lot of Python lately so I figured I’d take another look at graphing the function using Python.
No real moral to the story. I don’t enjoy writing code that displays graphs or UI. I was talking to a friend yesterday and he mentioned that he enjoyed coding with HTML and CSS to generate nice looking Web pages, but that he didn’t consider himself a programmer.
I suspect that people’s brains are wired differently and there’s a component that determines how much affinity a person has for coding in true programming languages versus coding for UI tasks.
# rastrigin_graph.py import numpy as np import matplotlib.pyplot as plt from matplotlib import cm from mpl_toolkits.mplot3d import Axes3D X = np.linspace(-5.12, 5.12, 100) Y = np.linspace(-5.12, 5.12, 100) X, Y = np.meshgrid(X, Y) Z = (X**2 - 10 * np.cos(2 * np.pi * X)) + \ (Y**2 - 10 * np.cos(2 * np.pi * Y)) + 20 fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.nipy_spectral, linewidth=0.08, antialiased=True) # plt.savefig('rastrigin_graph.png') plt.show()