import numpy as np import matplotlib.pyplot as plt print('------ 3.13 ---------------') print("Droite passant par deux points donnés.") # b + -2*a = 1 # b + 3*a = 5 # (1 -2) _ (1) # (1 3) ¯ (5) matA = np.array([[1, -2], [1, 3]], np.float64) vecb = np.array([1, 5], np.float64) v_sol = np.linalg.lstsq(matA, vecb, rcond=None)[0] # Solution approchée = solution exacte ici print("matA=\n", matA) print("v_sol=", v_sol) print("matA*v_sol=", np.dot(matA, v_sol)) # Vérification. b = v_sol[0] a = v_sol[1] x1 = matA[:,1] # Abscisses des points donnés y1 = vecb # Ordonnées des points donnés x2 = np.array([-3, 4]) y2 = a*x2 + b plt.plot(x1, y1, 'o') plt.plot(x2, y2, '-') plt.title('droite passant par les deux points.') plt.xlabel('x') plt.ylabel('y = a*x + b') plt.text(-3, 5.5, 'a = ' + '{:10.6f}'.format(a), color='black', fontsize=12) plt.text(-3, 5.0, 'b = ' + '{:10.6f}'.format(b), color='black', fontsize=12) plt.show()