import numpy as np
import matplotlib.pyplot as plt
def f(x):
return x**3 + 0.205*x**2 - 10.880*x - 1.469
# Метод золотого сечения для максимума
def golden_max(a, b, eps=1e-6):
gr = (np.sqrt(5) - 1) / 2
x1 = b - gr * (b - a)
x2 = a + gr * (b - a)
f1 = f(x1)
f2 = f(x2)
while (b - a) > eps:
if f1 > f2:
b = x2
x2 = x1
f2 = f1
x1 = b - gr * (b - a)
f1 = f(x1)
else:
a = x1
x1 = x2
f1 = f2
x2 = a + gr * (b - a)
f2 = f(x2)
return (a + b) / 2
# Поиск максимума на отрезке [-2.2, -1.7]
x_max = golden_max(-2.2, -1.7)
y_max = f(x_max)
print("Метод золотого сечения (максимум):")
print(f" x_max = {x_max:.8f}")
print(f" f(x_max) = {y_max:.8f}")
# График
xs = np.linspace(-5, 5, 500)
plt.plot(xs, f(xs), 'b-')
plt.plot(x_max, y_max, 'ro', markersize=8, label='Максимум')
plt.grid(True)
plt.title('f(x) = x³ + 0.205x² - 10.880x - 1.469')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.show()