Area under curve y = sqrt(4 - x^2) where x lies in 0 to 2 is equal to pi
(iterations: int)
| 101 | |
| 102 | |
| 103 | def 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 | |
| 128 | if __name__ == "__main__": |
nothing calls this directly
no test coverage detected