Cone fitting in Matlab
25 ビュー (過去 30 日間)
古いコメントを表示
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;
0 件のコメント
採用された回答
その他の回答 (1 件)
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(:));
% 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;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Interpolation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!