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 - 3) / (x ** 2 + 5) ** 0.5
- def search_method(a, b, e=0.01):
- d = (b - a) / 4
- x0 = a
- while True:
- x1 = x0 + d
- if f(x0) > f(x1):
- x0 = x1
- if a <= x0 <= b:
- continue
- else:
- if abs(d) <= e:
- return x0
- else:
- x0 = x1
- d = -d / 4
- else:
- if abs(d) <= e:
- return x0
- else:
- x0 = x1
- d = -d / 4
- f_min = search_method(-3, -1)
- print('Метод поразрядного поиска')
- print('x =', f_min)
- print('f(x) =', f(f_min))
- print()
- def dichotomy_method(a, b, e=0.01):
- c = 0.002
- while True:
- x1 = (b + a - c) / 2
- x2 = (b + a + c) / 2
- if f(x1) <= f(x2):
- b = x2
- else:
- a = x1
- e_n = (b - a) / 2
- if e_n > e:
- continue
- else:
- return (a + b) / 2
- f_min = dichotomy_method(-3, -1)
- print('Метод дихотомии')
- print('x =', f_min)
- print('f(x) =', f(f_min))
- print()
- def golden_section_method(a, b, e=0.01):
- while True:
- c = a + (3 - 5**0.5) / 2 * (b - a)
- d = a + (5**0.5 - 1) / 2 * (b - a)
- if f(d) > f(c):
- b = d
- else:
- a = c
- if (b - a) > e:
- continue
- else:
- return min(d, c)
- f_min = golden_section_method(-3, -1)
- print('Метод золотого сечения')
- print('x =', f_min)
- print('f(x) =', f(f_min))
- print()
- x = np.arange(-3, -1, 0.01)
- y = list(map(f, x))
- plt.plot(x, y)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement