Help with making an Ellipse Area function
조회 수: 6 (최근 30일)
이전 댓글 표시
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
2025년 1월 31일
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
2025년 1월 31일
Hello Brendan,
To calculate the area of an ellipse using your X and Y data, you can follow these steps:
- Scale the Data: As you've done, scale your X and Y data if needed.
- Calculate the Covariance Matrix: Use the covariance of your data to find the semi-major and semi-minor axes of the ellipse.
- 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
2025년 1월 31일
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)
y = rand(5,1)
A = Ellipse_Area(x, y)
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
.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!