Advertisement
12311k

Untitled

Aug 8th, 2021
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. import math
  2.  
  3. def bisect(function, left, right, error):
  4. while right - left > error:
  5. if function(left) == 0:
  6. return left
  7. if function(right) == 0:
  8. return right
  9.  
  10. middle = (left + right) / 2
  11. if function(left) * function(middle) < 0:
  12. right = middle
  13. else:
  14. left = middle
  15. return left
  16.  
  17.  
  18. def f1(x):
  19. return x**3 - x**2 - 2*x
  20.  
  21.  
  22. def f2(x):
  23. return (x+1)*math.log10(x) - x**0.75
  24.  
  25.  
  26. print(bisect(f1, 1, 4, 0.000001))
  27. print(bisect(f2, 1, 4, 0.000001))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement