(a) The following is an infinite product expression: 11 13 17 X 19 23 29 31 X X X X X X 8 12 12 16 20 24 28 32 ㅠ 3 4 4 = 54 1 π = 1+ 2 XX 700 In this formula, the numerators are the primes > 2, while each denominator is the multiple of 4 closest to the numerator. Write a function pi_euler1(n) which computes the value of the first n terms in the product. For example, pi_euler1 (3) should return 3.28125. (b) The next formula is an infinite sum: 1 1 1 1 1 1 1 +=+ + + + + 4 5 6 7 8 9 3 1 1 1 + + 10 11 12 1 13 In this formula, each fraction 1/n has a sign (+) determined by: the first two terms have positive signs; after that, if the denominator is a prime of the form 4m - 1 (for example, n = 3, 7, 11, ...), the sign is positive; if the denominator is a prime of the form 4m + 1 (for example, n = 5, 13, 17,...), the sign is negative; if the denominator is a composite number, then the sign is equal to the product of the signs corresponding to its factors (for example, n = 9 = 3 × 3, so its sign is positive (a positive times a positive), while n = 10 = 5 x 2, so its sign is negative (a negative times a positive)). Write a function pi_euler2(n) which computes the value of the first n terms of this expression. For example, pi_euler2 (2) should return 1.5. (c) Compute the errors for both Euler expressions, as in part 1, again for n=1 to 500. Plot a graph showing the errors for both Euler expressions, together with the errors for the previous three approximations from part 1, on the same graph. Comment briefly on the convergence of the Euler methods. (d) Recall the Monte Carlo method, from week 6 (section 6.2.2), for approximating . Suppose we choose a point (x, y) randomly (with uniform distribution) in the unit square. The probability that it lies inside a circle of diameter 1 contained in the unit square is equal to the area of that circle, or π/4. So this Monte Carlo method works as follows: (i) Generate a large number M of points (x, y) with both x and y uniformly distributed random variables in [0, 1]. You can use the module random to do this, as this module contains a function (also called random) which returns a number from the uniform distribution on [0, 1]. (ii) For each (x,y) produced, check whether it lies inside a circle of diameter 1 centred at (0.5, 0.5). Let

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
(a) The following is an infinite product expression:
π
4
=
3
Aco
4
5 7 11 13 17 19 23 29
X X
X
X
X
X
X
X
4 8 12 12 16 20 24
28
|
In this formula, the numerators are the primes > 2, while each denominator is the multiple of 4 closest
to the numerator. Write a function pi_euler1(n) which computes the value of the first n terms in the
product. For example, pi_euler1 (3) should return 3.28125.
(b) The next formula is an infinite sum:
1 1 1 1 1 1 1 1
π = 1+ + +
+ + + +
2 3 4 5 6 7 8 9
31
32
1 1 1
+ +
10 11 12
1
13
+...
In this formula, each fraction 1/m has a sign (1) determined by: the first two terms have positive signs;
after that, if the denominator is a prime of the form 4m - 1 (for example, n = 3, 7, 11, ...), the sign is
positive; if the denominator is a prime of the form 4m + 1 (for example, n = 5, 13, 17,...), the sign is
negative; if the denominator is a composite number, then the sign is equal to the product of the signs
corresponding to its factors (for example, n = 9 = 3 × 3, so its sign is positive (a positive times a
positive), while n = 10 = 5 x 2, so its sign is negative (a negative times a positive)). Write a function
pi_euler2(n) which computes the value of the first n terms of this expression. For example,
pi_euler2 (2) should return 1.5.
(c) Compute the errors for both Euler expressions, as in part 1, again for n=1 to 500. Plot a graph showing
the errors for both Euler expressions, together with the errors for the previous three approximations from
part 1, on the same graph. Comment briefly on the convergence of the Euler methods.
(d) Recall the Monte Carlo method, from week 6 (section 6.2.2), for approximating . Suppose we choose a
point (x, y) randomly (with uniform distribution) in the unit square. The probability that it lies inside a
circle of diameter 1 contained in the unit square is equal to the area of that circle, or π/4. So this Monte
Carlo method works as follows:
(i) Generate a large number M of points (x, y) with both x and y uniformly distributed random
variables in [0, 1]. You can use the module random to do this, as this module contains a function
(also called random) which returns a number from the uniform distribution on [0, 1].
(ii) For each (x, y) produced, check whether it lies inside a circle of diameter 1 centred at (0.5, 0.5). Let
Transcribed Image Text:(a) The following is an infinite product expression: π 4 = 3 Aco 4 5 7 11 13 17 19 23 29 X X X X X X X X 4 8 12 12 16 20 24 28 | In this formula, the numerators are the primes > 2, while each denominator is the multiple of 4 closest to the numerator. Write a function pi_euler1(n) which computes the value of the first n terms in the product. For example, pi_euler1 (3) should return 3.28125. (b) The next formula is an infinite sum: 1 1 1 1 1 1 1 1 π = 1+ + + + + + + 2 3 4 5 6 7 8 9 31 32 1 1 1 + + 10 11 12 1 13 +... In this formula, each fraction 1/m has a sign (1) determined by: the first two terms have positive signs; after that, if the denominator is a prime of the form 4m - 1 (for example, n = 3, 7, 11, ...), the sign is positive; if the denominator is a prime of the form 4m + 1 (for example, n = 5, 13, 17,...), the sign is negative; if the denominator is a composite number, then the sign is equal to the product of the signs corresponding to its factors (for example, n = 9 = 3 × 3, so its sign is positive (a positive times a positive), while n = 10 = 5 x 2, so its sign is negative (a negative times a positive)). Write a function pi_euler2(n) which computes the value of the first n terms of this expression. For example, pi_euler2 (2) should return 1.5. (c) Compute the errors for both Euler expressions, as in part 1, again for n=1 to 500. Plot a graph showing the errors for both Euler expressions, together with the errors for the previous three approximations from part 1, on the same graph. Comment briefly on the convergence of the Euler methods. (d) Recall the Monte Carlo method, from week 6 (section 6.2.2), for approximating . Suppose we choose a point (x, y) randomly (with uniform distribution) in the unit square. The probability that it lies inside a circle of diameter 1 contained in the unit square is equal to the area of that circle, or π/4. So this Monte Carlo method works as follows: (i) Generate a large number M of points (x, y) with both x and y uniformly distributed random variables in [0, 1]. You can use the module random to do this, as this module contains a function (also called random) which returns a number from the uniform distribution on [0, 1]. (ii) For each (x, y) produced, check whether it lies inside a circle of diameter 1 centred at (0.5, 0.5). Let
r
π = 1+
1
2
+
13
+
1 1
|
-
=+
4 5
1
1
1 1
- + + +
8 9
6
7
1
1
1
+ +
10 11 12
1
13
In this formula, each fraction 1/n has a sign (+) determined by: the first two terms have positive signs;
after that, if the denominator is a prime of the form 4m - 1 (for example, n = 3, 7, 11, ...), the sign is
positive; if the denominator is a prime of the form 4m + 1 (for example, n = 5, 13, 17,...), the sign is
negative; if the denominator is a composite number, then the sign is equal to the product of the signs
corresponding to its factors (for example, n = 9 = 3 x 3, so its sign is positive (a positive times a
positive), while n = 10 = 5 x 2, so its sign is negative (a negative times a positive)). Write a function
pi_euler2(n) which computes the value of the first n terms of this expression. For example,
pi_euler2 (2) should return 1.5.
(c) Compute the errors for both Euler expressions, as in part 1, again for n=1 to 500. Plot a graph showing
the errors for both Euler expressions, together with the errors for the previous three approximations from
part 1, on the same graph. Comment briefly on the convergence of the Euler methods.
(d) Recall the Monte Carlo method, from week 6 (section 6.2.2), for approximating . Suppose we choose a
point (x, y) randomly (with uniform distribution) in the unit square. The probability that it lies inside a
circle of diameter 1 contained in the unit square is equal to the area of that circle, or π/4. So this Monte
Carlo method works as follows:
(i) Generate a large number M of points (x, y) with both x and y uniformly distributed random
variables in [0, 1]. You can use the module random to do this, as this module contains a function
(also called random) which returns a number from the uniform distribution on [0, 1].
(ii) For each (x, y) produced, check whether it lies inside a circle of diameter 1 centred at (0.5, 0.5). Let
the number of such points be Min.
(iii) Then an approximation for π is given by 4Min/M.
Write a function montecarlo (M) which takes an integer M and returns an approximation to T. (I can't
give you an example output, as the random nature of the procedure means approximations will differ!)
(e) Write a function montecarlo_accuracy (eps) which takes as an input some all float eps, and
repeatedly performs the Monte Carlo procedure, until the approximation has an error <eps. State in a
comment what this function tells you about the convergence of the Monte Carlo method for
approximating 7 (please comment out any code for 2e, or the codes will take us a long time to run!).
Transcribed Image Text:r π = 1+ 1 2 + 13 + 1 1 | - =+ 4 5 1 1 1 1 - + + + 8 9 6 7 1 1 1 + + 10 11 12 1 13 In this formula, each fraction 1/n has a sign (+) determined by: the first two terms have positive signs; after that, if the denominator is a prime of the form 4m - 1 (for example, n = 3, 7, 11, ...), the sign is positive; if the denominator is a prime of the form 4m + 1 (for example, n = 5, 13, 17,...), the sign is negative; if the denominator is a composite number, then the sign is equal to the product of the signs corresponding to its factors (for example, n = 9 = 3 x 3, so its sign is positive (a positive times a positive), while n = 10 = 5 x 2, so its sign is negative (a negative times a positive)). Write a function pi_euler2(n) which computes the value of the first n terms of this expression. For example, pi_euler2 (2) should return 1.5. (c) Compute the errors for both Euler expressions, as in part 1, again for n=1 to 500. Plot a graph showing the errors for both Euler expressions, together with the errors for the previous three approximations from part 1, on the same graph. Comment briefly on the convergence of the Euler methods. (d) Recall the Monte Carlo method, from week 6 (section 6.2.2), for approximating . Suppose we choose a point (x, y) randomly (with uniform distribution) in the unit square. The probability that it lies inside a circle of diameter 1 contained in the unit square is equal to the area of that circle, or π/4. So this Monte Carlo method works as follows: (i) Generate a large number M of points (x, y) with both x and y uniformly distributed random variables in [0, 1]. You can use the module random to do this, as this module contains a function (also called random) which returns a number from the uniform distribution on [0, 1]. (ii) For each (x, y) produced, check whether it lies inside a circle of diameter 1 centred at (0.5, 0.5). Let the number of such points be Min. (iii) Then an approximation for π is given by 4Min/M. Write a function montecarlo (M) which takes an integer M and returns an approximation to T. (I can't give you an example output, as the random nature of the procedure means approximations will differ!) (e) Write a function montecarlo_accuracy (eps) which takes as an input some all float eps, and repeatedly performs the Monte Carlo procedure, until the approximation has an error <eps. State in a comment what this function tells you about the convergence of the Monte Carlo method for approximating 7 (please comment out any code for 2e, or the codes will take us a long time to run!).
Expert Solution
steps

Step by step

Solved in 5 steps with 5 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY