with(LinearAlgebra): with(plots): #LS(L,x): Inputs a list of points, L, finds the Least Squares line (regression). For example, Try: #LS([[1,1],[2,2],[3,3],[4,5]],x); LS:=proc(L,x) local A,b,i: A:=<|>: b:=: LeastSquares(A,b)[1]+LeastSquares(A,b)[2]*x: end: #LSc(L,x): Same as LS(L,x), but using multi-variable calculus instead of Linear Algebra. Try: #LS([[1,1],[2,2],[3,3],[4,5]],x); LSc:=proc(L,x) local b,m,f,v: f:=add((L[i][2]-b-m*L[i][1])^2,i=1..nops(L)): v:=solve({diff(f,b),diff(f,m)},{b,m}): subs(v,b)+subs(v,m)*x: end: #PLS(L): Inputs a list of points, L. Oututs the scatter- plot and plots the Least Squares Line. Try: #PLS([[1,1],[2,2],[3,3],[4,5]]); PLS:=proc(L) local gu,x,mu,minx,maxx: gu:=LS(L,x): minx:=min(seq(L[i][1],i=1..nops(L))): maxx:=max(seq(L[i][1],i=1..nops(L))): mu:=plot(gu,x=minx..maxx,color=red): mu:=mu,plot(L,style=point,color=black): display(mu); end: