Advertisement
Sephinroth

Перебор

Oct 7th, 2020
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4. def F(x):
  5.     return x*x-10*x+2
  6.  
  7. a = 3
  8. b = 6
  9. eps = 0.01
  10.  
  11. #метод поразрядного поиска
  12. h = 0.001
  13. flag = False
  14. x = []
  15.  
  16. for i in np.arange(a, b, h):
  17.     x.append(i)
  18.     x.append(i + h)    
  19.     if F(x[-2]) < F(x[-1]):
  20.         if h < eps and not flag:
  21.             x_ans = x[-2]
  22.             flag = True
  23.         else:
  24.             h /= -4
  25.            
  26. y = [F(i) for i in x]
  27.  
  28. plt.plot (x, y)
  29. plt.grid(True)
  30. plt.show()
  31. print("x* = ", x_ans, "\ny_min = ", F(x_ans))
  32.  
  33. #метод дихотомии
  34. delta = eps/10
  35. h = (b-a)/2
  36.  
  37. while h <= eps:
  38.     x1 = (a+b-delta)/2
  39.     x2 = (a+b+delta)/2
  40.    
  41.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement