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

7 views (last 30 days)
I need to plot the points and the line obtained from the function. Can somebody help me writing the code?

Accepted Answer

John BG
John BG on 2 Apr 2016
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

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!