Help with making an Ellipse Area function

37 ビュー (過去 30 日間)
Brendan
Brendan 2025 年 1 月 31 日 6:34
コメント済み: Star Strider 2025 年 1 月 31 日 21:02
I am having trouble making my ellipse function work. I am using X,Y data in the function although there is an error in the script that is not allowing me to find the area. Also I would love help on displaying the ellipse.
This is the code for my function.
function[area] = Ellipse_Area(FCOPX,FCOPY)
sFCOPX=FCOPX*1000;
sFCOPY=FCOPY*1000;
Sx = std(sFCOPX);
Sy = std(sFCOPY);
Syx = 0;
for i = 1:length(sFCOPX)
Syx = Syx + (sFCOPX(i) - mean(sFCOPY))*(sFCOPX(i) - mean(sFCOPX));
end
  1 件のコメント
Walter Roberson
Walter Roberson 2025 年 1 月 31 日 19:57
Syx = Syx + (sFCOPX(i) - mean(sFCOPY))*(sFCOPX(i) - mean(sFCOPX));
Each time through the loop, mean(sFCOPY) and mean(sFCOPX) are going to be the same as on the other iterations, as neither sFCOPY nor sFCOPX are changing. It would therefore make more sense to assign the mean() to variables before the loop and use the variables inside the loop.
mean_sFCOPY = mean(sFCOPY);
mean_sFCOPX = mean(sFCOPX);
for i = 1:length(sFCOPX)
Syx = Syx + (sFCOPX(i) - meansFCOPY)*(sFCOPX(i) - mean_sFCOPX);
end

サインインしてコメントする。

回答 (2 件)

prabhat kumar sharma
prabhat kumar sharma 2025 年 1 月 31 日 7:01
Hello Brendan,
To calculate the area of an ellipse using your X and Y data, you can follow these steps:
  1. Scale the Data: As you've done, scale your X and Y data if needed.
  2. Calculate the Covariance Matrix: Use the covariance of your data to find the semi-major and semi-minor axes of the ellipse.
  3. Calculate the Area: The area of an ellipse is given by (\pi \times a \times b), where (a) and (b) are the lengths of the semi-major and semi-minor axes.
Here's a MATLAB function to calculate the area and display the ellipse using dummy data:
FCOPX = randn(100, 1);
FCOPY = randn(100, 1);
Ellipse_Area(FCOPX, FCOPY);
function [area] = Ellipse_Area(FCOPX, FCOPY)
% Scale the data
sFCOPX = FCOPX * 1000;
sFCOPY = FCOPY * 1000;
% Calculate the covariance matrix
covMatrix = cov(sFCOPX, sFCOPY);
% Eigen decomposition to find the axes
[eigenVecs, eigenVals] = eig(covMatrix);
% Semi-major and semi-minor axes
a = sqrt(max(diag(eigenVals)));
b = sqrt(min(diag(eigenVals)));
% Calculate the area of the ellipse
area = pi * a * b;
% Display the ellipse
theta = linspace(0, 2*pi, 100);
ellipseX = a * cos(theta);
ellipseY = b * sin(theta);
% Rotate the ellipse
phi = atan2(eigenVecs(2, 1), eigenVecs(1, 1));
R = [cos(phi), -sin(phi); sin(phi), cos(phi)];
ellipse = R * [ellipseX; ellipseY];
% Plot the ellipse
figure;
plot(sFCOPX, sFCOPY, 'b.', 'DisplayName', 'Data Points');
hold on;
plot(mean(sFCOPX) + ellipse(1, :), mean(sFCOPY) + ellipse(2, :), 'r-', 'LineWidth', 2, 'DisplayName', 'Fitted Ellipse');
xlabel('Scaled X');
ylabel('Scaled Y');
title('Ellipse Fitting');
legend;
axis equal;
grid on;
% Display the calculated area
fprintf('The area of the ellipse is: %.2f square units\n', area);
end
I hope it helps!
  1 件のコメント
Brendan
Brendan 2025 年 1 月 31 日 19:40
Thank you

サインインしてコメントする。


Star Strider
Star Strider 2025 年 1 月 31 日 10:58
The only error I can see is that you need to assign something to the ‘area’ variable in your ‘EllipseArea’ function.
I assigned ‘Syx’ to it here —
x = rand(5,1)
x = 5×1
0.3437 0.3771 0.6304 0.4375 0.7183
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = rand(5,1)
y = 5×1
0.7692 0.5958 0.9679 0.3846 0.2130
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A = Ellipse_Area(x, y)
A = 1.0808e+05
function [area] = Ellipse_Area(FCOPX,FCOPY)
sFCOPX=FCOPX*1000;
sFCOPY=FCOPY*1000;
Sx = std(sFCOPX);
Sy = std(sFCOPY);
Syx = 0;
for i = 1:length(sFCOPX)
Syx = Syx + (sFCOPX(i) - mean(sFCOPY))*(sFCOPX(i) - mean(sFCOPX));
end
area = Syx;
end
.
  2 件のコメント
Brendan
Brendan 2025 年 1 月 31 日 19:40
Thank you
Star Strider
Star Strider 2025 年 1 月 31 日 21:02
My pleasure!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAdaptive Filters についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by