import matplotlib.pyplot as plt import numpy as np # # Sample the unit interval. # t = np.linspace ( 0.0, 1.0, 101 ) # # Plot the function. # plt.plot ( t, np.cos ( t ), 'r', linewidth = 2 ) # # Plot the dotted line y = x. # plt.plot ( t, t, "g:", linewidth = 2 ) # # Plot the iterates x, cos(x), cos(cos(x)), ... # x = 1.0 y = np.cos ( x ) for _ in range ( 20 ): fy = np.cos ( y ) plt.plot ( [x, y], [y, y], 'b', linewidth = 1 ) plt.plot ( [y, y], [y, fy], 'b', linewidth = 1 ) x = y y = fy # # Give x and y axis the same scale. # plt.axis ( 'equal' ) # # Save a copy of the figure. # plt.savefig ( 'cos_cobweb.png' ) # # Show the figure. # plt.show ( )