![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/144873/image.jpeg)
matlab polyfit with ax+by=c format
9 views (last 30 days)
Show older comments
Hi:
is there anyway to fit a data to ax+by=c format?
for the attached data, it is obviously should better to be x=0.0664. however, if I use polyfit(coord(:,1),coord(:,2),1), the result becomes:
y=-1325.8x+88
Thanks!
Yu
0 Comments
Answers (2)
John D'Errico
on 19 Jan 2019
Edited: John D'Errico
on 19 Jan 2019
Why would you fit a straight line to that data in the first place?
plot(x,y,'o')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/144873/image.jpeg)
What did you expect to see? I would suggest that IF you think the fit should be x==c, thus a constant, then you need to use an orthogonal regression. You need to recognize that polyfit is not designed to fit a vertical line to data, where the errors are allowed to be entirely in the x-direction. Therefore, polyfit cannot have done what you wanted. Polyfit is designed to fit a model of the form
1*y = a*x + b + e_i
I explicitly wrote 1 in there, because that is the actual model. y will ALWAYS have a coefficient of 1 when polyfit is involved. As well, the error for polyfit is ALWAYS assumed to be in y. That is what the e_i term implies. Any noise, any error, is assumed to be entirely in the estimate of y, whereas x is assumed to be known exactly.
If we want to assume that both x and y have noise in them, then an orthogonal regression is more appropriate (although, even in this case we don't see any noise, just lack of fit.) We can get the orthogonal regression reasonably easily if you understand how to use SVD.
MU = mean(coord,1);
[U,S,V] = svd(coord - MU); % requires R2016b or later.
V(:,2)
ans =
0.999999905023262
0.000435836513538122
Your model is now
V(1,2)*(x - MU(1)) + V(2,2)*(y - MU(2)) == 0
which simplifies to this model:
syms x y
vpa(V(1,2)*x + V(2,2)*y == dot(V(:,2),MU), 10)
ans =
0.999999905*x + 0.0004358365135*y == 0.06640067028
So reasonably close to your expected result. But that is very much NOT what polyfit is designed to produce. You need to understand the tools you use, else you can get an unexpected answer.
2 Comments
John D'Errico
on 28 Jan 2019
The method I posed can handle any of those cases, with any slope, including an infinite slope or a horizontal line, or a line of any other slope too. So the case you had has nearly an infinite slope, which causes polyfit to have problems, but the svd has no problem because it looks to see which variable had variation in it.
Walter Roberson
on 19 Jan 2019
No, there is not.
Suppose the equation were 5 * x + 3 * y = 17 . Now multiply through by 2 to get 10 * x + 6 * y = 34 . This equation would be true as well. So are the "real" coefficients a = 5, b = 3, c = 17? Or are the "real" coefficients a = 10, b = 6, c = 34 ?
In order to get unique coefficients, you need to constrain one of the three coeficients to a known value.
0 Comments
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!