3-D line of best fit from origin to cloud of data points

26 ビュー (過去 30 日間)
Wes Anderson
Wes Anderson 2019 年 12 月 3 日
編集済み: Umar 2024 年 8 月 20 日
I'm trying to get a line of best fit for a 3-D set of data points. This line of fit should go from the origin to the "cloud" of points, and I'd like to know the equation for that fit. Any ideas?
Thanks
  1 件のコメント
Umar
Umar 2024 年 8 月 20 日
編集済み: Umar 2024 年 8 月 20 日

Hi @Wes Anderson ,

You can utilize the concept of principal component analysis (PCA). The line of best fit can be represented as a vector that points in the direction of maximum variance of the data points. First, create 3-D data points into an Nx3 matrix, where each row corresponds to a point in 3D space.



% Example data points
A = [1, 2, 3; 
2, 3, 4; 
3, 5, 6; 
4, 7, 8]; % Nx3 matrix


Then, compute the covariance matrix which will capture how much the dimensions vary together.



 C = cov(A); % Compute covariance matrix


Perform eigenvalue decomposition on the covariance matrix to find the principal components.



% V contains eigenvectors, D contains eigenvalues
[V, D] = eig(C);


Identify the principal component, bear in mind that the eigenvector corresponding to the largest eigenvalue indicates the direction of maximum variance, which is the direction of the line of best fit.



[~, i] = max(diag(D)); % Index of the largest eigenvalue
u = V(:, i); % Direction vector of the line of best fit

Formulate the equations by using line_points = t' * u'; % Points along the line where ( t ) is a scalar parameter that allows you to generate points along the line.

 % Example of generating points along the line

t = linspace(0, 10, 100); % Generate 100 points from 0 to 10
line_points = t' * u'; % Points along the line


Finally, plot the results and visualize the original data points along with the line of best fit.



figure;
scatter3(A(:,1), A(:,2), A(:,3), 'filled'); % Original data points
hold on;
plot3(line_points(:,1), line_points(:,2), line_points(:,3), 'r-',   'LineWidth', 2); % 
Line of best fit
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
title('3D Line of Best Fit');
grid on;
hold off;

Please see attached plot.



If you have new data, you can apply the same transformation using the derived direction vector ( u ) to assess its position relative to the established line of best fit. Hope this helps. Please let me know if you have any further questions.

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

採用された回答

Matt J
Matt J 2019 年 12 月 3 日
編集済み: Matt J 2019 年 12 月 3 日
The equation is t*u where u is 3D line direction vector obtained by,
[V,d]=eig(A.'*A,'vector');
[~,i]=max(d);
u=V(:,i);
and A is an Nx3 matrix whose rows are your cloud points.
  6 件のコメント
Chang hsiung
Chang hsiung 2021 年 11 月 13 日
wow, thanks a lot Matt !!!
Matt J
Matt J 2021 年 11 月 13 日
@Chang hsiung You're quite welcome. Please accept-click the answer if it helped you.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by