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 find_roots():
"""Find real roots of f(x) using numpy.roots."""
coeff = [1, 0.205, -10.880, -1.469]
roots = np.roots(coeff)
# Keep only real roots (imag part close to zero)
real_roots = [r.real for r in roots if np.abs(r.imag) < 1e-12]
return real_roots
def find_critical_points():
"""Find critical points (f'(x)=0) and classify as max/min."""
# Derivative: f'(x) = 3x^2 + 0.41x - 10.88
a, b, c = 3, 0.41, -10.88
discriminant = b**2 - 4*a*c
if discriminant < 0:
return [] # no real critical points
sqrt_disc = np.sqrt(discriminant)
x1 = (-b - sqrt_disc) / (2*a)
x2 = (-b + sqrt_disc) / (2*a)
# Second derivative: f''(x) = 6x + 0.41
points = []
for x in (x1, x2):
y = f(x)
second_deriv = 6*x + 0.41
kind = "max" if second_deriv < 0 else "min"
points.append((x, y, kind))
return points
# Get roots and extrema
roots = find_roots()
extrema = find_critical_points()
# Print results
print("Roots (x-intercepts):")
for i, r in enumerate(roots, 1):
print(f" Root {i}: x = {r:.8f}, f(x) = {f(r):.2e}")
print("\nLocal extrema:")
for x, y, kind in extrema:
print(f" {kind.capitalize()} at x = {x:.8f}, f(x) = {y:.8f}")
# Plot the function and mark points
x_vals = np.linspace(-5, 5, 500)
y_vals = f(x_vals)
plt.figure(figsize=(10, 6))
plt.plot(x_vals, y_vals, 'b-', linewidth=2, label='f(x)')
# Mark roots
for r in roots:
plt.plot(r, f(r), 'go', markersize=8, label='Root' if r == roots[0] else "")
# Mark extrema (max in red, min in blue)
for x, y, kind in extrema:
color = 'r' if kind == 'max' else 'b'
marker = 'v' if kind == 'max' else '^'
label = 'Maximum' if kind == 'max' else 'Minimum'
plt.plot(x, y, color=color, marker=marker, markersize=10, label=label)
plt.grid(True, linestyle='--', alpha=0.7)
plt.title("Function $f(x) = x^3 + 0.205x^2 - 10.880x - 1.469$")
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.show()