Cone fitting in Matlab

18 次查看(过去 30 天)
Araf
Araf 2023-9-29
编辑: Matt J 2023-9-29
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;

采纳的回答

Matt J
Matt J 2023-9-29
编辑:Matt J 2023-9-29
Consider using rightcircularconeFit(), which is non-iterative, from this FEX download,

更多回答(1 个)

Walter Roberson
Walter Roberson 2023-9-29
移动:Walter Roberson 2023-9-29
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;

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by