I want to plane fit my sperical data points.

6 views (last 30 days)
I want to correct this data with the radius, so that it creates a plane. The radius is known due measurements.
Thank you in advance.

Accepted Answer

Bora Eryilmaz
Bora Eryilmaz on 20 Dec 2022
Edited: Bora Eryilmaz on 20 Dec 2022
Assuming a spherical surface with unknown origin (and perhaps radius), you can run an optimization algorithm to estimate the model parameters (origin, etc.) and subtract the location of the points on the surface of the sphere from your data. That would give you, roughly, a plane-like view of your data at a distance equal to the radius of the sphere.
A better planar view might be to actually project the data into an r-theta-z plane, but this would require a more complicated algorithm.
% "Unknown" model parameters.
x0 = 45;
y0 = 30;
z0 = -198;
r = 200.0;
% Construct the data surface
[X,Y] = meshgrid(0:1:100, 0:1:60);
Z = sqrt((r+randn(size(X))).^2 - (X-x0).^2 - (Y-y0).^2) + z0;
surf(X,Y,Z)
% Estimate model parameters x0, y0, z0, and r.
% You can remove "r" from the estimation if it is already known.
P0 = [30, 30, -30, 10];
options = optimset('MaxFunEvals', 2000);
P = fminsearch(@(p) fcn(p,X,Y,Z), P0, options)
P = 1×4
44.1685 29.7778 -200.0555 201.9682
% Project into the plane at distance r.
x0 = P(1);
y0 = P(2);
z0 = P(3);
r = P(4);
Zplane = Z - sqrt(r^2 - (X-x0).^2 - (Y-y0).^2) - z0;
surf(X,Y,Zplane)
function cost = fcn(p, X, Y, Z)
x0 = p(1);
y0 = p(2);
z0 = p(3);
r = p(4);
z = sqrt(r^2 - (X-x0).^2 - (Y-y0).^2) + z0;
cost = norm(z-Z, 2); % Least squares cost.
end
  3 Comments
Bora Eryilmaz
Bora Eryilmaz on 20 Dec 2022
Yes, xco and yco need to be the same size for this to work. You may need to use ngrid() or meshgrid() commands to convert your data into the meshgrid format: https://www.mathworks.com/help/matlab/ref/meshgrid.html#mw_6ae7effe-9402-4974-b1a6-0391eba290d8.
Chiel van Wanrooij
Chiel van Wanrooij on 20 Dec 2022
Thank you for your help. I appreciate it.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 20 Dec 2022
You can use sphericalFit() from this FEX download
to fit a sphere to your points (non-iteratively). This does not currently allow you to constrain the radius to a known value, however, if that is essential, it would at least give you a pretty good initial guess of (x0,y0,z0) for the iterative optimization proposed by @Bora Eryilmaz.

Community Treasure Hunt

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

Start Hunting!