MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / pi_estimator_using_area_under_curve

Function pi_estimator_using_area_under_curve

maths/monte_carlo.py:103–125  ·  view source on GitHub ↗

Area under curve y = sqrt(4 - x^2) where x lies in 0 to 2 is equal to pi

(iterations: int)

Source from the content-addressed store, hash-verified

101
102
103def pi_estimator_using_area_under_curve(iterations: int) -> None:
104 """
105 Area under curve y = sqrt(4 - x^2) where x lies in 0 to 2 is equal to pi
106 """
107
108 def function_to_integrate(x: float) -> float:
109 """
110 Represents semi-circle with radius 2
111 >>> [function_to_integrate(x) for x in [-2.0, 0.0, 2.0]]
112 [0.0, 2.0, 0.0]
113 """
114 return sqrt(4.0 - x * x)
115
116 estimated_value = area_under_curve_estimator(
117 iterations, function_to_integrate, 0.0, 2.0
118 )
119
120 print("******************")
121 print("Estimating pi using area_under_curve_estimator")
122 print(f"Estimated value is {estimated_value}")
123 print(f"Expected value is {pi}")
124 print(f"Total error is {abs(estimated_value - pi)}")
125 print("******************")
126
127
128if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

Tested by

no test coverage detected