Cone fitting in Matlab

18 views (last 30 days)
Araf
Araf on 29 Sep 2023
Edited: Matt J on 29 Sep 2023
I am trying to fit data points based on cone fitting. I have developed a code but there is an error that is always coming up:
Pixel_coordinates_of_the_inside_vortex
Error using lsqcurvefit
Function value and YDATA sizes are not equal.
Error in Pixel_coordinates_of_the_inside_vortex (line 20)
paramsFit = lsqcurvefit(@(params, xy) coneEquation(params, xy), initialGuess, xyData, z);
My code is:
theta = linspace(0, 2*pi, 100);
r = linspace(0, 5, 100);
z = linspace(0, 10, 100);
xData = r .* cos(theta);
yData = r .* sin(theta);
% Define the cone equation as a function
coneEquation = @(params, xy) sqrt(xy(:,1).^2 + xy(:,2).^2) * tan(params(1)) - params(2);
% Initial guess for parameters (angle and height)
initialGuess = [pi/4, 5];
% Concatenate xData and yData into a single matrix
xyData = [xData(:), yData(:)];
% Fit the cone equation to the data using lsqcurvefit
paramsFit = lsqcurvefit(@(params, xy) coneEquation(params, xy), initialGuess, xyData, z);
% Extract the fitted parameters
angle = paramsFit(1);
height = paramsFit(2);
% Display the equation of the fitted cone
fprintf('Fitted Cone Equation: sqrt(x^2 + y^2) * tan(%.4f) = %.4f\n', angle, height);
% Create a grid of points for the fitted cone plot
[X, Y] = meshgrid(linspace(min(xData), max(xData), 100), linspace(min(yData), max(yData), 100));
XY = [X(:), Y(:)]; % Concatenate X and Y into a single matrix
Z = sqrt(X.^2 + Y.^2) * tan(angle);
% Plot the data points and the fitted cone surface
figure;
scatter3(xData, yData, z, 'o', 'filled');
hold on;
surf(X, Y, Z, 'FaceAlpha', 0.5);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Cone Curve Fitting');
hold off;

Accepted Answer

Matt J
Matt J on 29 Sep 2023
Edited: Matt J on 29 Sep 2023
Consider using rightcircularconeFit(), which is non-iterative, from this FEX download,

More Answers (1)

Walter Roberson
Walter Roberson on 29 Sep 2023
Moved: Walter Roberson on 29 Sep 2023
theta = linspace(0, 2*pi, 100);
r = linspace(0, 5, 100);
z = linspace(0, 10, 100);
xData = r .* cos(theta);
yData = r .* sin(theta);
% Define the cone equation as a function
coneEquation = @(params, xy) sqrt(xy(:,1).^2 + xy(:,2).^2) * tan(params(1)) - params(2);
% Initial guess for parameters (angle and height)
initialGuess = [pi/4, 5];
% Concatenate xData and yData into a single matrix
xyData = [xData(:), yData(:)];
% Fit the cone equation to the data using lsqcurvefit
paramsFit = lsqcurvefit(@(params, xy) coneEquation(params, xy), initialGuess, xyData, z(:));
Local minimum found. Optimization completed because the size of the gradient is less than the value of the optimality tolerance.
% Extract the fitted parameters
angle = paramsFit(1);
height = paramsFit(2);
% Display the equation of the fitted cone
fprintf('Fitted Cone Equation: sqrt(x^2 + y^2) * tan(%.4f) = %.4f\n', angle, height);
Fitted Cone Equation: sqrt(x^2 + y^2) * tan(1.1071) = -0.0000
% Create a grid of points for the fitted cone plot
[X, Y] = meshgrid(linspace(min(xData), max(xData), 100), linspace(min(yData), max(yData), 100));
XY = [X(:), Y(:)]; % Concatenate X and Y into a single matrix
Z = sqrt(X.^2 + Y.^2) * tan(angle);
% Plot the data points and the fitted cone surface
figure;
scatter3(xData, yData, z, 'o', 'filled');
hold on;
surf(X, Y, Z, 'FaceAlpha', 0.5);
xlabel('X');
ylabel('Y');
zlabel('Z');
title('Cone Curve Fitting');
hold off;

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!