MATLAB: How should i Implement a function that fits a line using least squares to the 2-D data points. It should output the two parameters of a line without using a toolbox for fitting the line. And i have to do this by doing regression

ransac

I need to plot the points and the line obtained from the function. Can somebody help me writing the code?

Best Answer

the function polyfit(x,y,n) does what you need.
The MATLAB starter help example with n=1
x = linspace(0,1,5);
y = 1./(1+x);
p = polyfit(x,y,1);
x1 = linspace(0,2);
y1 = 1./(1+x1);
f1 = polyval(p,x1);
figure
plot(x,y,'o')
hold on
plot(x1,y1)
plot(x1,f1,'r--')
legend('y','y1','f1')
If you find this answer of any help solving your question, please click on the thumbs-up vote link,
thanks in advance
John