import numpy as np import matplotlib.pyplot as plt print('------ 3.15 ---------------') print("Droite passant au mieux par 5 points donnés.") points = np.array([[-2, 13], [0, 10], [2, 8], [4, 7], [6, 5]], np.float64) # Données matA = np.array([np.ones(points.shape[0]), points[:,0]]).T print("A =\n", matA) vecb = points[:,1] v_sol = np.linalg.lstsq(matA, vecb, rcond=None)[0] # Solution approchée print("Solution = ", v_sol) b = v_sol[0] a = v_sol[1] xx = np.array([-3, 7]) # Abscisses des extrémités de la droite yy = a*xx + b # Ordonnées des extrémités de la droite plt.plot(points[:,0], points[:,1], 'o') # Affiche les points plt.plot(xx, yy, '-') # Trace la droite plt.title('droite passant "au mieux" par les points.') plt.xlabel('x') plt.ylabel('y = a*x + b') plt.text(-2.5, 2, 'a = ' + '{:10.6f}'.format(a), color='black', fontsize=12) plt.text(-2.5, 1, 'b = ' + '{:10.6f}'.format(b), color='black', fontsize=12) plt.axis([-3, 7, 0, 14]) # Limites des axes plt.show()