# # Numerical Mathematics and Computing, Fifth Edition # Ward Cheney & David Kincaid # Brooks/Cole Publ. Co. # (c) 2003 # # file: lstsq2 # # linear least squares example # values := [1.0, 2.0, 2.5, 3.0]; Yvalues := [3.7, 4.1, 4.3, 5.0]; A := Matrix([seq([1, Xvalues[k]], k=1..4)]); b := Vector([seq(Yvalues[k], k=1..4)]); with(LinearAlgebra): c := LeastSquares(A,b); pts := [seq([Xvalues[k], Yvalues[k]], k=1..4)]; bestline := c[1] + c[2]*x; with(plots): PLT1 := plot(pts, style=point, symbol=circle): PLT2 := plot(bestline, x=-1..1): display(PLT1,PLT2);