MATLAB: How to make a best fit line without using intrinsic matlab functions

polyfitwithout using intrinsic matlab functions

I am trying to make a best fit line out of a set of data for my class, but I don't know how to write a function to get best fit line without using 'polyfit' and 'polyval' functions. please help me

Best Answer

If you want to fit a linear two-parameter (slope-intercept) regression to (x,y) data, use the mldivide,\ function (or operator):
B = [ones(size(x(:)) x(:)]\y(:); % Estimate Parameters
slope = B(2)
intercept = B(1)
fit_line_y = [ones(size(x(:)) x(:)]*B; % Line To Plot As: plot(x, fit_line_y)
Related Question