Question

Write a PYTHON program that will approximate the value of π. You can do this by...

Write a PYTHON program that will approximate the value of π. You can do this by computing π to be the ratio of the area of a circle to the area of the square that bounds that circle. Assume a circle of radius 0.5 enclosed by a 1x1 square. The area of the circle then is πr^2=π/4, since r=0.5=1/2 and the area of the square is 1. To approximate the ratio, take a large number of uniformly distributed random points. These points can be in any position within the square i.e. between (0,0) and (1,1). If they fall within the circle, they are considered in, otherwise they are out. Keep track of the total number of points, and the number of points that are inside the circle. If we divide the number of points within the circle by the total number of points, we should get a value that is an approximation of the ratio of the areas, which should equate to π/4. Hence, you would get an estimate of π to be 4 * approximate ratio of areas. Of course, larger the number of points, better will be our estimate of the areas.

To get a uniformly distributed random point, use the function random.random(). Example usage of the function is:

import random as r
x = r.random()
0 0
Add a comment Improve this question Transcribed image text
Answer #1
import random as r


n = 1000000
inside = 0
for i in range(n):
    x = r.random()
    y = r.random()
    if x**2 + y**2 <= 1:
        inside += 1


print("Pi = " + str(4*inside/n))

Add a comment
Know the answer?
Add Answer to:
Write a PYTHON program that will approximate the value of π. You can do this by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CODE IN JAVA PLEASE MAKE SURE IT 'LL COMPILE AND RUN Determining t Experimentally Recall that...

    CODE IN JAVA PLEASE MAKE SURE IT 'LL COMPILE AND RUN Determining t Experimentally Recall that π is the ratio of a circle's circumference to its diameter and that we can calculate the area of a circle with the formula A-r Below is a circle enscribed within the unit square What is the ratio of the areas of the enscribed circle to that of the unit square? If we pick a random point within the unit square what is the...

  • last 2pictures are lab7,just need to do the first picture,dont do the lab 7 Functional π...

    last 2pictures are lab7,just need to do the first picture,dont do the lab 7 Functional π Take the sample solutions for approximating π from labZ and put each solution into its own function. You'll need to decide: 1. What are the inputs that this function requires? 2. What is the output that this function produces? 3. What is an appropriate name for this function? You should create 3 separate functions 1, One that approximates π using the area of a...

  • Write a Java program that uses the Monte Carlo method to estimate the value of PI....

    Write a Java program that uses the Monte Carlo method to estimate the value of PI. This method uses the unit circle inscribed in a square with sides of length 2 and random numbers to perform the estimation. The estimation works as follows: • Two random numbers are generated during each iteration of a loop. • The random numbers are each in the range of -1 to 1. One random number is the x-coordinate and the other is the y-coordinate....

  • Can't seem to figure this out as a matlab file and was looking for help, you...

    Can't seem to figure this out as a matlab file and was looking for help, you can't use any for or while loops and all functions used must be built into matlab. Consider a square dartboard with each side having dimension equal to 2. Centered inside this square place a circle having radius 1. Now throw N = 1e4 darts at the dartboard in a random fashion so that they are uniformly distributed over the surface of the dartboard. The...

  • This problem deals with continuous (rather than discrete) probability, but it's an interesting problem! It involves...

    This problem deals with continuous (rather than discrete) probability, but it's an interesting problem! It involves probabilistically estimating the value of pi. Consider a circle of radius 1 inscribed within a square with side 2. Both shapes are centered at the origin (0, 0). Using basic geometry, the ratio of the circle's area to the square's area is pi (1)^2/2^2 = pi/4. Now, suppose that you randomly throw some darts at this figure. Out of n total attempts, m attempts...

  • Answer in Python please: Compose a function mc_pi( n ) to estimate the value of π...

    Answer in Python please: Compose a function mc_pi( n ) to estimate the value of π using the Buffon's Needle method. n describes the number of points to be used in the simulation. mc_pi should return its estimate of the value of π as a float. Your process should look like the following: Prepare an array of coordinate pairs xy. This should be of shape ( n,2 ) selected from an appropriate distribution (see notes 1 and 2 below). Calculate...

  • Create a NOTEPAD or PDF file that restates the problem in your own words, specifies what input is needed, w...

    Create a NOTEPAD or PDF file that restates the problem in your own words, specifies what input is needed, what output is expected, the step by step process (algorithm) to get the output from the input, and test data (input for which you know the expected output) for each of the 3 problems given below. You should not write any actual C++ code. Make sure the problem statement is in your own words and is descriptive enough so someone not...

  • i BOX Number- " Parts reference Text CommentsHeader&Footer Links MATLAB Lab 8 This exercise was done previously for a full circle. Perform all the steps shown including the plot, but only...

    i BOX Number- " Parts reference Text CommentsHeader&Footer Links MATLAB Lab 8 This exercise was done previously for a full circle. Perform all the steps shown including the plot, but only for the function shown in part b). Objective: In calculus, integration allows us to find the area under a curve. Numerical methods exist which enable us to approximate the area An interesting approach is to randomly select points in an x-y plane and find the fraction of those points...

  • Using Python Version 1. Write a program that uses a "while" loop to print the first...

    Using Python Version 1. Write a program that uses a "while" loop to print the first 10 positive integers and to compute their sum. Print the sum after it is computed. Do the same with a "for" loop. Version 2. Write a program to approximate the square root of a number. Recall that the square root of a number x is a number r such that r*r = x. Newton discovered that if one initial estimate of r is z...

  • Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script...

    Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script that evaluates this expression, ignoring all terms that are strictly smaller than .000001. Your script should display the number of terms summed and the sum. Use a while-loop. Think about the initialization of your variables and the order of computation carefully! In the loop body you need to calculate the value of a term only once. We use the above series for approximating (π^2)/8...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT