30 lines
834 B
Python
30 lines
834 B
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
|
|
# Együtthatók
|
|
a, b, c, d = 1, -2, 3, -1
|
|
|
|
# X értékek
|
|
x = np.linspace(-2, 2, 400)
|
|
# Harmadfokú polinom értékek
|
|
y = a + b*x + c*x**2 + d*x**3
|
|
|
|
# Válassz néhány pontot a polinomról
|
|
x_points = np.linspace(-2, 2, 10)
|
|
y_points = a + b*x_points + c*x_points**2 + d*x_points**3
|
|
|
|
# Lineáris regresszió illesztése a pontokra
|
|
coeffs = np.polyfit(x_points, y_points, 1) # 1. fokú polinom = egyenes
|
|
lin_y = coeffs[0]*x + coeffs[1]
|
|
|
|
# Ábrázolás
|
|
plt.plot(x, y, label='Harmadfokú polinom')
|
|
plt.scatter(x_points, y_points, color='blue', marker='x', s=80, label='Polinom pontjai')
|
|
plt.plot(x, lin_y, color='black', linestyle='--', label='Lineáris regresszió')
|
|
plt.xlabel('x')
|
|
plt.ylabel('y')
|
|
plt.title('Polinom és lineáris regresszió')
|
|
plt.legend()
|
|
plt.grid(True)
|
|
plt.show()
|