import numpy as np
def f(x):
return x**3 + 0.205*x**2 - 10.880*x - 1.469
def dichotomy_min(f, a, b, tol=1e-6, delta=1e-5):
while (b - a) > tol:
mid = (a + b) / 2
x1 = mid - delta
x2 = mid + delta
if f(x1) < f(x2):
b = x2
else:
a = x1
return (a + b) / 2
def golden_section_max(f, a, b, tol=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) > tol:
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
def bisection_root(f, a, b, tol=1e-6):
if f(a) * f(b) >= 0:
raise ValueError("На отрезке нет корня или чётное количество корней")
while (b - a) / 2 > tol:
c = (a + b) / 2
if f(c) == 0:
return c
if f(a) * f(c) < 0:
b = c
else:
a = c
return (a + b) / 2
print("Поиск минимума...")
x_min = dichotomy_min(f, 1.5, 2.2)
print(f"Минимум: x = {x_min:.6f}, f(x) = {f(x_min):.6f}")
print("Поиск максимума...")
x_max = golden_section_max(f, -2.2, -1.7)
print(f"Максимум: x = {x_max:.6f}, f(x) = {f(x_max):.6f}")
print("Поиск корней...")
root1 = bisection_root(f, -3.5, -3.2)
root2 = bisection_root(f, -0.2, -0.1)
root3 = bisection_root(f, 3.2, 3.3)
print(f"Корни: {root1:.6f}, {root2:.6f}, {root3:.6f}")
print("Программа завершена.")