polyfitn throws an error with seemingly sufficient input data

I have two data points that lie on a straight line:
x = [1;2];
y = [3;4];
Matlab's built-in polyfit function nicely fits the data to a linear equation:
P = polyfit(x,y,1);
which can be used to get an equation for the line y = x + 2. As I understand it, polyfitn should work the same way:
Pn = polyfitn(x,y,1);
but it gives
Error using betainc
X must be in the interval [0,1].
Error in polyfitn (line 233)
polymodel.p = betainc(polymodel.DoF./(t.^2 + polymodel.DoF),polymodel.DoF/2,1/2);
I can trick polyfitn by over-defining the problem:
Pn = polyfitn([x;x],[y;y],1);
but is that legal? It gives the answer I expect, but I want to make sure I'm not missing something. I'd like to understand why polyfitn only works when I give it superfluous data.

1 个评论

I get the same error with polyfitn. Looks like polyfitn thinks there is not enough data points; however, the builtin polyfit is good with it:
x = [-1;0;1]
y = [-0.1;0;0.1]
polyfit(x, y, 1)
ans = 0.1 0
polyfitn(x, y, 1)
Error using betainc
X must be in the interval [0,1].
Error in polyfitn (line 233)
polymodel.p = betainc(polymodel.DoF./(t.^2 + polymodel.DoF),polymodel.DoF/2,1/2);

请先登录,再进行评论。

回答(1 个)

polyfitn is a File Exchange Contribution, not part of MATLAB. You can read the source there.
If you trace through the code, your degrees of freedom is coming out as 0, and t is coming out as 0, so t^2+DoF is coming out as 0 for the denominator. 0/0 is NaN and so is not in range for betainc
The t is coming out as 0 because the variances are being set to infinity because
% we cannot form variance or standard error estimates
% unless there are at least as many data points as
% parameters to estimate.
The particular test involves "n > nt" when n (2 points) = nt (number of terms generated for middleterms). Since the comment says "at least as many" and there appear to be exactly as many there just might be a bug there. But I'll let John D'Errico the author comment on that.

Community Treasure Hunt

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

Start Hunting!

Translated by