#! /usr/bin/env python3 # def circle_distance ( ): #*****************************************************************************80 # ## circle_distance() averages the distance between random pairs of points. # # 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 ( "circle_distance():" ) print ( " Given two random points p1 and p2 in the unit circle," ) print ( " * compute the distance d between the points;" ) print ( " * estimate the average value of d over 1000 cases;" ) print ( " * estimate the variance s of d over 1000 cases;" ) # # Do sn simulations # sn = 1000 # # Get coordinates of two random points in the circle. # x1, y1 = circle_sample2 ( sn ) x2, y2 = circle_sample2 ( sn ) xv = x1 - x2 yv = y1 - y2 d = np.zeros ( sn ) for si in range ( 0, sn ): v = np.array ( [ xv[si], yv[si] ] ) d[si] = np.linalg.norm ( v ) d_est = np.mean ( d ) s_est = np.var ( d ) d_exact = 128.0 / 45.0 / np.pi s_exact = ( 2025 * np.pi**2 - 128**2 ) / 45**2 / np.pi**2 print ( "" ) print ( " Number of simulations was ", sn ) print ( " d: estimate = ", d_est, "exact = ", d_exact ) print ( " s: estimate = ", s_est, "exact = ", s_exact ) print ( "" ) print ( "circle_distance:" ) print ( " Normal end of execution." ) return if ( __name__ == '__main__' ): circle_distance_test ( )