#! /usr/bin/env python3 # def distance_histogram ( ): #*****************************************************************************80 # ## distance_histogram() shows the distribution of distances for the circle. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 09 January 2022 # # Author: # # John Burkardt # from circle_sample import circle_sample2 from numpy import linalg as LA import matplotlib.pyplot as plt import numpy as np print ( "distance_histogram():" ) print ( " Given two random points p1 and p2 in the unit circle:" ) print ( " * Let d be the distance between two random points;" ) print ( " * create a histogram of the likelihood of each value of d;" ) print ( " * compare to an exact formula;" ) # # Do sn simulations # sn = 10000 print ( "" ) print ( " Number of simulations was ", sn ) # # Get coordinates of pairs of random points in the circle. # x1, y1 = circle_sample2 ( sn ) x2, y2 = circle_sample2 ( sn ) xv = x1 - x2 yv = y1 - y2 # # Compute d, the distance between each pair. # d = np.zeros ( sn ) for si in range ( 0, sn ): v = np.array ( [ xv[si], yv[si] ] ) d[si] = np.linalg.norm ( v ) # # Create a histogram. # d1 = np.linspace ( 0.0, 2.0, 101 ) p1 = d1 * ( 4.0 * np.arccos ( d1 / 2.0 ) - d1 * np.sqrt ( 4.0 - d1**2 ) ) / np.pi plt.clf ( ) plt.hist ( d, bins = 25, density = True ) plt.plot ( d1, p1, 'r-', linewidth = 3 ) filename = 'distance_histogram.jpg' plt.grid ( True ) plt.xlabel ( '<-- distance -->' ) plt.ylabel ( '<-- probability density -->' ) plt.title ( 'PDF for pairwise distances' ) plt.savefig ( filename ) print ( " Graphics saved as '" + filename + "'" ) plt.show ( ) print ( "" ) print ( "distance_histogram:" ) print ( " Normal end of execution." ) return if ( __name__ == '__main__' ): distance_histogram ( )