Help with linear regression function
28 views (last 30 days)
Jesus Alejandro Rodriguez Morales on 30 Sep 2020
Can someone help me with the following assignment.
Given a set of approximate x and y coordinates of points in a plane, determine the best fitting line in the least square sense. Using the standard formula of a line ax + b = y, compute a and b. That is, write a function that takes two row verctors of the same length called x and y as input arguments (containing x and y coordinates of points) and returns two scalars, a and b specifying the line, as output arguments.
I can't use polyfit
Thank you in advance!
More Answers (2)
This was done without using backslash operator. But it works!
function [a b] = lin_reg(x,y)
X = mean(x);
Y = mean(y);
a = sum((x-X).*(y-Y))./sum((x-X).^2);
b = Y - a*X;