2023-01-16 11:17:05 +00:00
|
|
|
import math
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2023-05-16 20:18:38 +00:00
|
|
|
print(findZero(lambda x: x + math.sin(10 * x), 0.2, 0.4, 0.001))
|
2023-01-16 11:17:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def findZero(func, a, b, tol):
|
|
|
|
if func(a) * func(b) > 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
while b - a > tol:
|
|
|
|
m = (a + b) / 2
|
|
|
|
if func(a) * func(m) <= 0:
|
|
|
|
b = m
|
|
|
|
else:
|
|
|
|
a = m
|
|
|
|
|
|
|
|
return (a + b) / 2
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|