Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use Python Code to add a block of code to count the number of dots within the red circle on the graph that is produced.

Use Python Code to add a block of code to count the number of dots within the red circle on the graph that is produced.

import sys

import numpy as np # an alias for the namespace

import random

import matplotlib.pyplot as plt

#setting MC parameters

NumP=10000 # generate how many random point in side the square

n=100

#MC simulation block

pi_all=[]

#

for j in range(n):

cordx=[];cordy=[] # initialize the two empty coordinate lists

inside=0

for i in range(0,NumP): #generate random coordinates

cordx.append(random.random())

cordy.append(random.random())

r2 = cordx[i]*cordx[i]+cordy[i]*cordy[i]

if r2 <= 1:

inside=inside+1

# write control to count inside

pit= 4.0*inside/NumP

#End of sampling

pi_all.append(pit)

#print out result

print("pi: ","{:10.6f}".format(pit))

plt.hist(pi_all) # print hist of the sampling

# plot the random pair: cordx,cordy graphically for the last MC simulation

Symbol=10

LineW = 3

Lsp=100

f = plt.figure()

f.set_figwidth(8)

f.set_figheight(8)

plt.xlim(0,1)

plt.ylim(0,1)

plt.xlabel('X Cordinate')

plt.ylabel('Y Cordinate')

plt.scatter(cordx, cordy, s=Symbol,marker='o')

#plot circle

x = np.linspace(0,1,Lsp)

y = np.sqrt(1. - x*x)

plt.plot(x, y,'-r',color='red', linestyle='-', lw=LineW)

plt.show()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

Students also viewed these Databases questions

Question

How many three-digit numbers are divisible by 7?

Answered: 1 week ago

Question

What is Indian Polity and Governance ?

Answered: 1 week ago

Question

2. What should an employer do when facing an OSHA inspection?

Answered: 1 week ago