#! /usr/bin/env python3 # def cobweb_plot ( f, x0, N, a = 0, b = 1, filename = 'cobweb.png' ): #*****************************************************************************80 # ## cobweb_plot() makes a cobweb plot of a function iteration. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 11 January 2023 # # Author: # # Original Python code by John D Cook. # This version by John Burkardt # # Reference: # # John D Cook, # Cobweb plots, # https://www.johndcook.com/blog/2020/01/19/cobweb-plots/ # Posted 19 January 2020. # # Input: # # function f, a function of the form y = f(x). # # real x0, the initial value of x. # # integer N, the number of iterates. # # real a, b, the range over which the function is to be studied. # # string filename: the name of the file in which to save the plot. # import matplotlib.pyplot as plt import numpy as np y0 = f ( x0 ) plt.clf ( ) # # Plot the function. # t = np.linspace ( a, b, N ) plt.plot ( t, f ( t ), 'k', linewidth = 2 ) plt.grid ( True ) # # Plot the dotted line y = x. # plt.plot ( t, t, "k:", linewidth = 2 ) # # Plot the iterates. # x = x0 y = y0 for _ in range ( N ): fy = f ( y ) plt.plot ( [x, y], [y, y], 'b', linewidth = 1 ) plt.plot ( [y, y], [y, fy], 'b', linewidth = 1 ) x = y y = fy # # Plot the first and last points. # plt.plot ( x0, y0, 'g.', markersize = 20 ) plt.plot ( x, y, 'r.', markersize = 20 ) # # Give x and y axis the same scale. # plt.axis ( 'equal' ) # # Save a copy of the figure. # plt.savefig ( filename ) print ( ' Graphics saved as "%s"' % ( filename ) ) # # Show the figure. # plt.show ( block = False ) plt.close ( ) return def cobweb_plot_test ( ): #*****************************************************************************80 # ## cobweb_plot_test() tests cobweb_plot(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 24 June 2022 # # Author: # # John D Cook. # Modifications by John Burkardt # import numpy as np import platform print ( '' ) print ( 'cobweb_plot_test():' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test cobweb_plot().' ) cobweb_plot ( np.cos, 1.0, 20, 0.0, 1.0, 'cos.png' ) cobweb_plot ( lambda x:0.5*x+1.0/x, 0.5, 20, 0.2, 4.0, 'sqrt2.png' ) cobweb_plot ( lambda x:2*abs(x-0.5), 0.123, 20, 0.0, 1.0, 'tent.png' ) cobweb_plot ( lambda x:0.5*x*(1.0-x), 0.9, 20, 0.0, 1.0, 'logistic_0.5.png' ) cobweb_plot ( lambda x:1.5*x*(1.0-x), 0.01, 20, 0.0, 1.0, 'logistic_1.5.png' ) cobweb_plot ( lambda x:2.5*x*(1.0-x), 0.01, 20, 0.0, 1.0, 'logistic_2.5.png' ) cobweb_plot ( lambda x:3.5*x*(1.0-x), 0.01, 20, 0.0, 1.0, 'logistic_3.5.png' ) cobweb_plot ( lambda x:3.6*x*(1.0-x), 0.01, 40, 0.0, 1.0, 'logistic_3.6.png' ) cobweb_plot ( lambda x:3.7*x*(1.0-x), 0.01, 40, 0.0, 1.0, 'logistic_3.7.png' ) cobweb_plot ( lambda x:3.8*x*(1.0-x), 0.01, 40, 0.0, 1.0, 'logistic_3.8.png' ) # # Terminate. # print ( '' ) print ( 'cobweb_plot_test():' ) print ( ' Normal end of execution.' ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 21 August 2019 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == '__main__' ): timestamp ( ) cobweb_plot_test ( ) timestamp ( )