import numpy as np
import matplotlib.pyplot as plt


np.random.seed(0)
x = np.linspace(-10, 10, 10)
a_real, b_real, c_real = 1, -2, 3
y = a_real * x**2 + b_real * x + c_real + np.random.normal(0, 10, size=x.shape)


X = np.vstack([x**2, x, np.ones(len(x))]).T
coefficients = np.linalg.inv(X.T @ X) @ X.T @ y
a_est, b_est, c_est = coefficients


print(f"Estimated coefficients: a = {a_est}, b = {b_est}, c = {c_est}")


# Plot the fitted curve
y_predicted = a_est * x ** 2 + b_est * x + c_est
MSE = np.sum((y_predicted - y) ** 2) / 10
print(MSE)
plt.figure(dpi=500)
x_plot = np.linspace(-12.5, 12.5, 100)
y_plot = a_est * x_plot ** 2 + b_est * x_plot + c_est
plt.scatter(x, y, color='blue', label='Data points')
plt.plot(x_plot, y_plot, color='red', label='Fitted quadratic curve')
plt.title("Quadratic Fit to Data")
plt.xlabel("x")
plt.ylabel("y")
plt.legend()
plt.show()
