Advertisement
Sephinroth

Методы парабол

Oct 14th, 2020
1,867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3.  
  4. def F(x):
  5.     return x**3 + 4*x**2 + 2*x + 1
  6.  
  7. def Par(a, b, eps, x, y):
  8.     #метод парабол
  9.  
  10.     x1 = float(a)
  11.     x2 = (a + b) / 2
  12.     x3 = float(b)
  13.     while not (x3 - x1) < eps:    
  14.         if x3 != x2 != x1 and x2 != x1:
  15.             c1 = (F(x2) - F(x1)) / (x2 - x1)
  16.             c2 = ((F(x3) - F(x1)) / (x3 - x1) - (F(x2) - F(x1)) / (x2 - x1)) / (x3 - x2)
  17.             x_temp = (x1 + x2 - c1 / c2) / 2
  18.             x_list = [x1, x2, x3, x_temp]
  19.             x_list.sort()
  20.             for i in range(2):
  21.                 if F(x_list[i]) >= F(x_list[i+1]) and F(x_list[i+1]) <=  F(x_list[i+2]):
  22.                     x1 = x_list[i]
  23.                     x2 = x_list[i+1]
  24.                     x3 = x_list[i+2]
  25.     print("Метод парабол: \nx* = ", (x1 + x3) / 2, "\ny_min = ", F((x1 + x3) / 2), '\n')        
  26.    
  27. def main():
  28.     a = -1
  29.     b = 1
  30.     eps = 0.01
  31.     x = []
  32.     y = []
  33.     for i in np.arange(a, b, eps/10):
  34.         x.append(i)
  35.     y = [F(i) for i in x]    
  36.     plt.plot(x, y)
  37.     plt.grid()
  38.     plt.suptitle("Метод парабол")
  39.     plt.show()
  40.     Par(a, b, eps, x, y)
  41.  
  42. if __name__ == "__main__":    
  43.     main()    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement