matlab polyfit with ax+by=c format

7 次查看(过去 30 天)
Yu Li
Yu Li 2019-1-18
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

回答(2 个)

John D'Errico
John D'Errico 2019-1-19
编辑:John D'Errico 2019-1-19
Why would you fit a straight line to that data in the first place?
plot(x,y,'o')
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 个评论
Yu Li
Yu Li 2019-1-27
Thanks for your reply, and i'm sorry for the late response.
after read through your answer I think I know where the problem is, but I'm not very strong in the mathematic theory inside.
I have a question about your method, is this method only availble at fixed x or y situation, i.e. x=a or y=b, or if this is available for all situations?
Thanks!
Yu
John D'Errico
John D'Errico 2019-1-28
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
Walter Roberson 2019-1-19
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.

Community Treasure Hunt

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

Start Hunting!

Translated by