フィルターのクリア

Cone fitting in Matlab

13 ビュー (過去 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 Center および File ExchangeInterpolation についてさらに検索

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by