Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 27
#---------------------------------------------------------------- # Sample code for UEET2533 Information Theory and Coding # by Chong Zan Kai # Email [email protected] # Universiti Tunku Abdul Rahman, Malaysia. # # Demonstrate how to calculate the area of a circle without # using pi. # http://en.wikipedia.org/wiki/Monte_Carlo_method # # Last modified on 20-Jan-2015. #---------------------------------------------------------------- #------------------------------------------------------------------------------ # Methods #------------------------------------------------------------------------------ # # For Circle # def distance (this_dot, that_dot): term1 = (this_dot[0] - that_dot[0])**2 term2 = (this_dot[1] - that_dot[1])**2 return sqrt(term1 + term2) def dot_in_disk_area(rand_dot, disk_dot, disk_radius): if distance(rand_dot, disk_dot) <= disk_radius: return True else: return False # # For square # def dot_in_square_area(rand_dot, square_length): test1 = rand_dot[0] in range(square_length) test2 = rand_dot[1] in range(square_length) return test1 and test2 #------------------------------------------------------------------------------ # Main #------------------------------------------------------------------------------ # Create a space space_width = 1000 space_height = 1000 # Create a disk disk_center_dot = [space_width / 2, space_height/2] disk_radius = 100 # Create a square square_side_length = 100 # Total run time total_simulation = 10000 total_in_disk_area = 0 total_in_square_area = 0 for s in range(total_simulation): # # Make a random dot first # dot_x = randrange(space_width) dot_y = randrange(space_height) rand_dot = [dot_x, dot_y] #print ('rand_dot = %s' % rand_dot) # # Is this dot within the circle area? # if dot_in_disk_area(rand_dot, disk_center_dot, disk_radius): total_in_disk_area += 1 # print ('Within disk area.') # # Is this dot within the square area? # if dot_in_square_area(rand_dot, square_side_length): total_in_square_area += 1 #print ('Within square area.') # # Calculation # print ('total_in_disk_area = %d' % total_in_disk_area) print ('total_in_square_area = %d' % total_in_square_area) math_square_area = square_side_length * square_side_length math_disk_area = pi * disk_radius **2 print ('math_square_area = %f' % math_square_area) print ('math_disk_area = %f' % math_disk_area) # Calculate the disk area with Monti Carlo method (simplified version) mc_disk_area = total_in_disk_area / total_in_square_area * math_square_area print ('mc_disk_area = %f' % mc_disk_area) diff_percentage = (mc_disk_area - math_disk_area) / math_disk_area * 100 print ('diff_percentage = %f' %diff_percentage )
total_in_disk_area = 309 total_in_square_area = 91 math_square_area = 10000.000000 math_disk_area = 31415.926536 mc_disk_area = 33956.043956 diff_percentage = 8.085445 total_in_disk_area = 301 total_in_square_area = 96 math_square_area = 10000.000000 math_disk_area = 31415.926536 mc_disk_area = 31354.166667 diff_percentage = -0.196588 total_in_disk_area = 320 total_in_square_area = 86 math_square_area = 10000.000000 math_disk_area = 31415.926536 mc_disk_area = 37209.302326 diff_percentage = 18.440888