필터 지우기
필터 지우기

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 CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

태그

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by