This will be done using SageMath.
Once you have the vector output X from the procedure listed below, interpret the returned vector X like this:
the initial entry is interpreted as the y-intercept of the least squares line
the second entry is interpreted as the slope of the least squares line
Previous Procedure:
def least_squares_2d(alist, blist):
if len(alist) != len(blist):
raise ValueError(‘alist must be the same length as blist’)
m = len(alist)
A = zero_matrix(m,2)
for i in range(m):
A[i] = [1, alist[i]]
B = vector(blist)
X = (A.transpose()*A).inverse()*A.transpose()*B
return X
This procedure should start by calling the previous procedure and then it should output a graphic containing all the points as well as the least squares line plotted on an appropriate interval (depending on given points).
def plot_ls(alist, blist):
Read more here: Source link