Question

In: Computer Science

Write a python code that calculates π using numpy rand function.

Write a python code that calculates π using numpy rand function.

Solutions

Expert Solution

We can use the famous monte carlo stimulation ot estimate the value of pi using random numbers. Let us understand it by an example.

Suppose we have a dart board of the following shape. Both the circle and the square are centred at the origin, and the circle has a radius r.

Area of circle =

Area of square =

Suppose when you start throwing darts to the board, then the dart might fall anywhere on the boars with an equal probability.

The probability, P, that the dart falls in the circle is  .

so So we now generate random numbers x,y satisfying

if   then the point lies in the circle else it lies outside.

Here is the code along with the output to do so.

The code is well-commented and easy to understand if the answer helped you please upvote and if you have any doubts please comment i will surely help. please take care of the indentation while copying the code. Check from the screenshots provided.

Code:

# stimulate the value of pi using a monte carlo stimulation
import numpy as np
import matplotlib.pyplot as plt

# scattering n points over the unit square
number_of_points = 1000000
points = np.random.rand(number_of_points,2)

# counting the points inside the unit circle
inside_circle_count = 0
for point in points:
if np.sqrt(point[0]**2 + point[1]**2) < 1:
inside_circle_count += 1

# The probablity is number of points inside the cirle divided by the total number of points

probablity = inside_circle_count/number_of_points
estimated_pi = probablity*4
exact_pi = np.pi
print("the estimated value of pi is:",estimated_pi)
print("the exact value of pi is:",exact_pi)


Related Solutions

Write a python code without using Sympy and Numpy to derive the polynomial 2x2 + 5x...
Write a python code without using Sympy and Numpy to derive the polynomial 2x2 + 5x + 4.
#Python program using loop instead of numpy or pandas. Write a function to enter n student...
#Python program using loop instead of numpy or pandas. Write a function to enter n student scores from 0 to 100 to represent student score, Count how many A (100 - 90), B(89-80), C(79-70), D(69-60), F(<60) We will use numpy array OR pandas later for this problem, for now use loop Invoke the function with 10 student scores, show the result as follow: Test Enter 10 scores Enter a student score: 99 Enter a student score: 88 Enter a student...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade...
USING PYTHON 3.7 AND USING def functions. Write a function called GPA that calculates your grade point average (GPA) on a scale of 0 to 4 where A = 4, B = 3, C = 2, D = 1, and F = 0. Your function should take as input two lists. One list contains the grades received in each course, and the second list contains the corresponding credit hours for each course. The output should be the calculated GPA. To...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
Create a python code that calculates fixed point iteration method using a for loop.
Create a python code that calculates fixed point iteration method using a for loop.
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
Answer question in Python, show all code: Write a program that calculates and displays the end...
Answer question in Python, show all code: Write a program that calculates and displays the end of year balances in a savings account if $1,000 is put in the account at 6% interest for five years. For this problem, assume interest is calculated annually. (That is, if I put $1000 in a bank account at the beginning of the year, then the balance at the end of the year is $1,000 + $1,000*6%.) You may assume no money is removed...
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep...
Using Python coding language (with or without Pandas and/or NumPy), 1. Can you define function sleep to tell whether the participant are of the ages through 18 to 60 and sleep less than 6 hours per day? 2. Develop codes to check whether the sleep function you defined can make correct judgement. Make sure you write comments or create informative vairable name so that I can understand how you check the sleep function. (Hints: You can create toy data/dataframe to...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python...
Create and Compile a Python Script without using Numpy that Generate an nxn matrix using Python Script (ie n=user input) Ask (user input ) and (randomly generated) row and column location Assign Q to (known row and column location ) and 0 to all others location Please show compile script working as well
Using Python programming language, write a LONG piece of code that utilizes the do while function...
Using Python programming language, write a LONG piece of code that utilizes the do while function and the switch statement, please do not make It short, thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT