フィルターのクリア

fitting a circle with fitnlm

6 ビュー (過去 30 日間)
Hoschang Noori
Hoschang Noori 2020 年 7 月 4 日
編集済み: Star Strider 2020 年 7 月 4 日
Hello, I tried to fit following data to a circle with help of fitnlm
i used following circle function to determine the center of the circle and the radius:
X = readmatrix('coordinates.txt') %you can find the coordinats.txt in the attachments
circlefun = @(X, b) (X(:,1).^2 + X(:,2).^2 + b(1)*X(:,1) + b(2)*X(:,2) + b(3));
y = zeros(length(X(:,1)),1);
beta0 = [0 0 400];
mdl = fitnlm(X,y,circlefun, beta0)
it gives me following output:
Error using nlinfit (line 219)
MODELFUN must be a function that returns a vector of fitted values the same size as Y (1000-by-1). The model function you provided returned a result that was 1-by-1.
One common reason for a size mismatch is using matrix operators (*, /, ^) in your function instead of the corresponding elementwise operators (.*, ./, .^).
Error in NonLinearModel/fitter (line 1127)
nlinfit(X,y,F,b0,opts,wtargs{:},errormodelargs{:});
Error in classreg.regr.FitObject/doFit (line 94)
model = fitter(model);
Error in NonLinearModel.fit (line 1434)
model = doFit(model);
Error in fitnlm (line 99)
model = NonLinearModel.fit(X,varargin{:});
What I already tried: I used Newton Method for multivariables and the results were quite good in my opinion, but i would like to understand how i can use fitnlm.
Thanks in advance!

採用された回答

Star Strider
Star Strider 2020 年 7 月 4 日
The arguments to ‘circlefun’ are reversed. The parameter vector must always be the first argument, and the independent variable vector (or matrix) must always be the second.
This works:
circlefun = @(b,X) (X(:,1).^2 + X(:,2).^2 + b(1)*X(:,1) + b(2)*X(:,2) + b(3));
and produces:
mdl =
Nonlinear regression model:
y ~ (x1^2 + x2^2 + b1*x1 + b2*x2 + b3)
Estimated Coefficients:
Estimate SE tStat pValue
___________ _______ _______ __________
b1 -156.27 0.79825 -195.76 0
b2 51.732 5.9203 8.7381 9.8825e-18
b3 -3.4442e+05 3394.1 -101.48 0
Number of observations: 1000, Error degrees of freedom: 997
Root Mean Squared Error: 1.08e+03
R-Squared: -Inf, Adjusted R-Squared -Inf
F-statistic vs. constant model: 0, p-value = 1
.
  4 件のコメント
Image Analyst
Image Analyst 2020 年 7 月 4 日
How do you plot the fitted circle once you have the model?
Star Strider
Star Strider 2020 年 7 月 4 日
編集済み: Star Strider 2020 年 7 月 4 日
Since it is an implicit equation, the intent appears to be to estimate the parameters. Everything else would be derived from them.
EDIT — (4 Jul 2020 at 17:55)
The plot is relatively straightforward:
B = mdl.Coefficients.Estimate;
Xm = -B(1)/2;
Ym = -B(2)/2;
R = sqrt((Xm^2 + Ym^2) - B(3));
A = atan2(X(:,2)-Ym, X(:,1)-Xm);
Ycir = R*sin(A) + Ym;
Xcir = R*cos(A) + Xm;
figure
plot(X(:,1), X(:,2), 'p')
hold on
plot(Xcir, Ycir, '-r', 'LineWidth',2)
hold off
grid
axis('equal')
producing:
.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 7 月 4 日
  1 件のコメント
Hoschang Noori
Hoschang Noori 2020 年 7 月 4 日
編集済み: Hoschang Noori 2020 年 7 月 4 日
Thanks, I know the FAQ, i saw some posts from you about this elsewhere. :D The solution presented there works quite well for circles with few outliers, but it seems not to be robust enough for my purposes since i have some noise + some more circles in my data.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeRegression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by