Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- def F(x):
- return x*x-10*x+2
- def PP(a, b, eps, x, y):
- #метод поразрядного поиска
- h = 0.001
- flag = False
- for i in np.arange(a, b, h):
- x.append(i)
- x.append(i + h)
- if F(x[-2]) < F(x[-1]):
- if h < eps and not flag:
- x_ans = x[-2]
- flag = True
- else:
- h /= -4
- y = [F(i) for i in x]
- plt.plot (x, y)
- plt.grid(True)
- plt.show()
- print("Метод поразрядного поиска: \nx* = ", x_ans, "\ny_min = ", F(x_ans))
- def Dych(a, b, eps, x, y):
- #метод дихотомии
- delta = eps/10
- for i in np.arange(a, b, delta):
- x.append(i)
- y = [F(i) for i in x]
- h = (b-a)/2
- x_ans = a
- while h > eps:
- h = (b-a)/2
- x1 = (a+b-delta)/2
- x2 = (a+b+delta)/2
- if F(x1) <= F(x2):
- b = x2
- else:
- a = x1
- if h <= eps:
- x_ans = (a+b)/2
- print("\nМетод дихотомии: \nx* = ", x_ans, "\ny_min = ", F(x_ans), '\n')
- def GoldenRatio(a, b, eps, x, y):
- #метод золотого сечения
- for i in np.arange(a, b, eps/10):
- x.append(i)
- y = [F(i) for i in x]
- h = (b-a)/2
- tau = (5**(1/2) - 1) / 2
- h = (b-a)/2
- while h > eps:
- x1 = a + (3 - 5**(1/2))/2 * (b - a)
- x2 = a + (5**(1/2) - 1)/2 * (b - a)
- if F(x1) <= F(x2):
- b = x2
- h *= tau
- else:
- a = x1
- h *= tau
- x_ans = (a+b)/2
- print("Метод золотого сечения: \nx* = ", x_ans, "\ny_min = ", F(x_ans))
- def main():
- a = 3
- b = 6
- eps = 0.01
- x = []
- y = []
- PP(a, b, eps, x, y)
- Dych(a, b, eps, x, y)
- GoldenRatio(a, b, eps, x, y)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement