Figure 2.16:

Energy landscape for a pendulum; H = \frac {1}{2} p^2 -\kappa \cos q; \kappa =2.

Code for Figure 2.16

Text of the GNU GPL.

main.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import numpy as np
import matplotlib.pyplot as plt

n = 101
x = np.linspace(-5, 5, n)
y = np.linspace(-5, 5, n)
kappa = 2

xx, yy = np.meshgrid(x, y)

z = 0.5 * yy**2 - kappa * np.cos(xx)

val = [-1, 0, 1, 2, 3, 5, 8, 12]
contour_set = plt.contour(x, y, z, val)
plt.axis([-5, 5, -5, 5])
plt.xlabel("$q$")
plt.text(-5.75, 0, "$p$")

with open('heteroclinic.dat', 'w') as myfile:
    for collection in contour_set.collections:
        for path in collection.get_paths():
            data = path.vertices
            np.savetxt(myfile, data, fmt='%f')
            myfile.write('\n\n')

plt.show(block=False)