Help with making an Ellipse Area function

6 visualizaciones (últimos 30 días)
Brendan
Brendan el 31 de En. de 2025
Comentada: Star Strider el 31 de En. de 2025
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 comentario
Walter Roberson
Walter Roberson el 31 de En. de 2025
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

Iniciar sesión para comentar.

Respuestas (2)

prabhat kumar sharma
prabhat kumar sharma el 31 de En. de 2025
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!

Star Strider
Star Strider el 31 de En. de 2025
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 comentarios
Brendan
Brendan el 31 de En. de 2025
Thank you
Star Strider
Star Strider el 31 de En. de 2025
My pleasure!

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by