@@ -18,6 +18,10 @@ def pi_estimator(iterations: int):
18
18
4. After all the dots are placed, divide the dots in the circle by the total.
19
19
5. Multiply this value by 4 to get your estimate of pi.
20
20
6. Print the estimated and numpy value of pi
21
+ >>> pi_estimator(1000)
22
+ The estimated value of pi is 3.145
23
+ The numpy value of pi is 3.141592653589793
24
+ The total error is 0.003
21
25
"""
22
26
23
27
# A local function to see if a dot lands in the circle.
@@ -61,8 +65,11 @@ def area_under_curve_estimator(
61
65
c. Expected value = average of the function evaluations
62
66
4. Estimated value of integral = Expected value * (max_value - min_value)
63
67
5. Returns estimated value
68
+ >>> def test_function(x):
69
+ >>> return x * x
70
+ >>> area_under_curve_estimator(1000, test_function)
71
+ 0.334 (estimated value should be close to 1/3)
64
72
"""
65
-
66
73
return mean (
67
74
function_to_integrate (uniform (min_value , max_value )) for _ in range (iterations )
68
75
) * (max_value - min_value )
@@ -77,12 +84,19 @@ def area_under_line_estimator_check(
77
84
1. Calls "area_under_curve_estimator" function
78
85
2. Compares with the expected value
79
86
3. Prints estimated, expected and error value
87
+ >>> area_under_line_estimator_check(1000)
88
+ ******************
89
+ Estimating area under y=x where x varies from 0.0 to 1.0
90
+ Estimated value is 0.332
91
+ Expected value is 0.5
92
+ Total error is 0.168
93
+ ******************
80
94
"""
81
95
82
96
def identity_function (x : float ) -> float :
83
97
"""
84
98
Represents identity function
85
- >>> [function_to_integrate (x) for x in [-2.0, -1.0, 0.0, 1.0, 2.0]]
99
+ >>> [identity_function (x) for x in [-2.0, -1.0, 0.0, 1.0, 2.0]]
86
100
[-2.0, -1.0, 0.0, 1.0, 2.0]
87
101
"""
88
102
return x
@@ -103,6 +117,13 @@ def identity_function(x: float) -> float:
103
117
def pi_estimator_using_area_under_curve (iterations : int ) -> None :
104
118
"""
105
119
Area under curve y = sqrt(4 - x^2) where x lies in 0 to 2 is equal to pi
120
+ >>> pi_estimator_using_area_under_curve(1000)
121
+ ******************
122
+ Estimating pi using area_under_curve_estimator
123
+ Estimated value is 3.141
124
+ Expected value is 3.141592653589793
125
+ Total error is 0.0004
126
+ ******************
106
127
"""
107
128
108
129
def function_to_integrate (x : float ) -> float :
0 commit comments