def sinc_plot ( ): import matplotlib.pyplot as plt import numpy as np # # Set up x,y data for the sinc function # x1 = -12.0 x2 = +12.0 x = np.linspace ( x1, x2, 101 ) y = np.sin ( x ) / x # # Try to compute maximum value of y. # ymax = np.max ( y ) imax = np.argmax ( y ) xmax = x[imax] print ( ymax, imax, xmax ) # # Redefine y(0.0) to be 1. # y = np.sin(x) / x y[x==0] = 1.0 ymax = np.max ( y ) imax = np.argmax ( y ) xmax = x[imax] print ( '' ) print ( ' Redefine y[0]=1' ) print ( ymax, imax, xmax ) ymin = np.min ( y ) imin = np.argmin ( y ) xmin = x[imin] ymean = np.mean ( y ) # # Plot the data. # plt.clf ( ) plt.plot ( x, y, 'g-' ) plt.grid ( True ) plt.title ( 'The sinc function' ) plt.savefig ( 'sinc.png' ) plt.show ( ) # # Make a fancy plot with lines marking max and min y values. # plt.clf ( ) plt.plot ( x, y, 'g-', linewidth = 2 ) plt.plot ( [x1,x2], [ymax,ymax], 'b-' ) plt.plot ( [xmax,xmax], [0.0,ymax], 'b-' ) plt.plot ( [x1,x2], [ymin,ymin], 'r-' ) plt.plot ( [xmin,xmin], [0.0,ymin], 'r-' ) plt.plot ( [x1,x2], [ymean,ymean], 'c:', linewidth = 3 ) plt.plot ( [x1,x2], [0.0,0.0], 'k-' ) plt.grid ( True ) plt.title ( 'Maximum, minimum, mean values of sinc(x)' ) plt.savefig ( 'sinc_marked.png' ) plt.show ( ) # # Mark all the negative points. # plt.clf ( ) plt.plot ( x, y, 'g-', linewidth = 2 ) plt.plot ( x[y<0], y[y<0], 'r.', markersize = 10 ) plt.plot ( [x1,x2], [0.0,0.0], 'k-' ) plt.grid ( True ) plt.title ( 'Location of negative values of sinc(x)' ) filename = 'sinc_negative.png' plt.savefig ( filename ) plt.show ( ) return if ( __name__ == "__main__" ): sinc_plot ( )