Circle least squares fit for 3D data

23 ビュー (過去 30 日間)
Ciara Gibbs
Ciara Gibbs 2019 年 8 月 7 日
コメント済み: Martin Fuchs 2020 年 9 月 24 日
Hi everyone,
I have 6000 x coordinates, y coordinates and z cooridinates that form a circle that does not perfectly occupy one plane. I am looking to fit the data with a circle but I can only find functions online that do it for x and y coordinates and not including z. I would appreciate any help in creating some code for this as I am not sure where to start (I am a beginner in MATLAB!)
Thank you in advance
Ciara
  8 件のコメント
Ciara Gibbs
Ciara Gibbs 2019 年 8 月 7 日
@Bruno Luong , please find the data attached in the order of x, y ,x . Thanks again
Ciara Gibbs
Ciara Gibbs 2019 年 8 月 7 日
@Torsten, thank you for the links I will give them a read!

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

採用された回答

Bruno Luong
Bruno Luong 2019 年 8 月 7 日
編集済み: Bruno Luong 2019 年 8 月 7 日
circle.png
X=csvread('data.csv');
XC = mean(X,1);
Y=X-XC;
[~,~,V]=svd(Y,0);
Q = V(:,[1 2]); % basis of the plane
Y=Y*Q;
xc=Y(:,1);
yc=Y(:,2);
M=[xc.^2+yc.^2,-2*xc,-2*yc];
% Fit ellipse through (xc,yc)
P = M\ones(size(xc));
a=P(1);
P = P/a;
r=sqrt(P(2)^2+P(3)^2+1/a); % radius
xyzc = XC' + Q*P(2:3); % center
theta = linspace(0,2*pi);
c = xyzc + r*Q*[cos(theta); sin(theta)]; % fit circle
close all
plot3(X(:,1),X(:,2),X(:,3),'.');
hold on
plot3(c(1,:),c(2,:),c(3,:),'r','LineWidth', 2);
axis equal
  4 件のコメント
Ciara Gibbs
Ciara Gibbs 2019 年 8 月 7 日
Amazing! Thank you very much it is greatly appreciated and you have saved me a lot of time!
Martin Fuchs
Martin Fuchs 2020 年 9 月 24 日
This script is just brilliant!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by