Plane Fitting a 3D Scatter Plot

I have a 3D scatter plot that I want to have a planar fit. How do you do this?
x = [14.9 1.7 0.0 10.9 0.0];
y = [11.3 9.1 23.7 12.8 2.9];
z = [5.32787E-17 2.93234E-16 2.09997E-16 5.45E-17 4.55E-16];
scatter3(x,y,z)
3D Scatter Plot.jpg

 採用された回答

Star Strider
Star Strider 2019 年 3 月 7 日

1 投票

A simple linear regression is straightforward:
x = [14.9 1.7 0.0 10.9 0.0];
y = [11.3 9.1 23.7 12.8 2.9];
z = [5.32787E-17 2.93234E-16 2.09997E-16 5.45E-17 4.55E-16];
B = [x(:) y(:) ones(size(x(:)))] \ z(:);
xv = linspace(min(x), max(x), 10)';
yv = linspace(min(y), max(y), 10)';
[X,Y] = meshgrid(xv, yv);
Z = reshape([X(:), Y(:), ones(size(X(:)))] * B, numel(xv), []);
scatter3(x,y,z, 'filled')
hold on
mesh(X, Y, Z, 'FaceAlpha', 0.5)
hold off
view(-120, 35)
title(sprintf('Z = %+.3E\\cdotX %+.3E\\cdotY %+.3E', B))
producing:
Plane Fitting a 3D Scatter Plot - 2019 03 06.png

6 件のコメント

Sarah
Sarah 2019 年 3 月 7 日
Two Questions:
  1. Is there a way to visually indicate the distance or "error" between the measured points and the fit plane?
  2. How would you calculate and display the coefficient of determination (R^2) on the graph?
Star Strider
Star Strider 2019 年 3 月 7 日
Try this:
x = [14.9 1.7 0.0 10.9 0.0];
y = [11.3 9.1 23.7 12.8 2.9];
z = [5.32787E-17 2.93234E-16 2.09997E-16 5.45E-17 4.55E-16];
B = [x(:) y(:) ones(size(x(:)))] \ z(:);
xv = linspace(min(x), max(x), 50)';
yv = linspace(min(y), max(y), 50)';
[X,Y] = meshgrid(xv, yv);
Z = reshape([X(:), Y(:), ones(size(X(:)))] * B, numel(xv), []);
Ze = [x(:) y(:) ones(size(x(:)))] * B; % Calculate Surface At Each Data Triplet
Err = Ze - z(:); % Calculate Errors
SStot = sum((z - mean(z)).^2); % Total Sum-Of-Squares
SSres = sum(Err.^2); % Residual Sum-Of-Squares
Rsq = 1 - SSres/SStot; % Coefficient Of Determination
figure
surf(X, Y, Z, 'FaceAlpha', 0.5, 'EdgeColor','none')
hold on
plot3([x; x], [y; y], [z(:) Ze]', '-r', 'LineWidth',1) % Plot Errors (Red Lines From Surface To Data)
scatter3(x,y,z, 'filled')
hold off
view(-120, 35)
xlabel('X')
ylabel('Y')
zlabel('Z')
% title(sprintf('Z = %+.3E\\cdotX %+.3E\\cdotY %+.3E', B))
title('Z = -2.0998\times10^{-17}X - 1.135\times10^{-17}Y + 4.644\times10^{-16}')
text(10, 20, 3.5E-16, sprintf('R^2 = %0.3f', Rsq))
I tweaked my previous code slightly to incorporate the additions you requested. I also manually entered the title string, with the same data as previously. The first one still works, this one is easier to read.
Experiment to get the result you want.
The updated plot:
Plane Fitting a 3D Scatter Plot (2) - 2019 03 06.png
Sarah
Sarah 2019 年 3 月 7 日
Wonderful!!
Star Strider
Star Strider 2019 年 3 月 7 日
As always, my pleasure!
Damla Yüksel
Damla Yüksel 2019 年 12 月 1 日
Hello, in the following line, matlab returns an error:
"Error using plot3
Vectors must be the same length.
Error in (line 33)
plot3([x; x], [y; y], [z(:) Ze]', '-r','LineWidth',1) % Plot Errors (Red Lines From Surface To Data)"
What should I do for this error?
Star Strider
Star Strider 2019 年 12 月 1 日
You should make the vectors the same length!
It is best for you to post this as a new Question, supplying ‘x’, ‘y’, ‘z’ and ‘Ze’.
Your Comment does not relate to the substance of this thread, so I will delete it (and my Comment here) in a few yours.

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

その他の回答 (1 件)

KSSV
KSSV 2019 年 3 月 7 日

0 投票

x = [14.9 1.7 0.0 10.9 0.0];
y = [11.3 9.1 23.7 12.8 2.9];
z = [5.32787E-17 2.93234E-16 2.09997E-16 5.45E-17 4.55E-16];
nx = 10 ; ny = 10 ;
xi = linspace(min(x),max(x),nx) ;
yi = linspace(min(y),max(y),ny) ;
[X,Y] = meshgrid(xi,yi) ;
F = scatteredInterpolant(x',y',z') ;
Z = F(X,Y) ;
scatter3(x,y,z)
hold on
surf(X,Y,Z)

カテゴリ

ヘルプ センター および File ExchangeLine Plots についてさらに検索

製品

質問済み:

2019 年 3 月 7 日

コメント済み:

2019 年 12 月 1 日

Community Treasure Hunt

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

Start Hunting!

Translated by