Advertisement
George_Ivanov05

0.1

Jun 21st, 2022
810
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. import math
  2.  
  3. # exp(x) - 2x - 2 = 0
  4. # x3 + 3x - 5
  5.  
  6. try:
  7.     a = int(input())
  8.     b = int(input())
  9. except ValueError:
  10.     raise ValueError('a and b must be integers')
  11.  
  12.  
  13. def math_function(x):
  14.     function = math.pow(x, 3) - 3 * x - 5
  15.     return function
  16.  
  17.  
  18. def bisection_method(num1, num2):
  19.     if math_function(num1) * math_function(num2) >= 0:
  20.         raise Exception('Cannot f(a) and f(b) to be with same signs')
  21.  
  22.     middle = 0
  23.     while num2 - num1 >= 0.001:
  24.         middle = (num1 + num2) / 2
  25.         if math_function(middle) * math_function(num1) < 0:
  26.             num2 = middle
  27.         if math_function(middle) == 0.0:
  28.             break
  29.         else:
  30.             num1 = middle
  31.  
  32.     return f"{middle:.4f}"
  33.  
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement