How can I compute the Area and the Centroid of the following shape?
124 次查看(过去 30 天)
显示 更早的评论
How can I compute the Area and the Centroid of the following shape?
I have used the following code to construct it:
xA = 0;
xB = 1;
xf = 1
x = linspace(0, xf, xf*1e4 + 1);
a = 9.2; %
c = 0.5; % center of function
Y = sigmf(x, [a c]).*((0 <= (x - xA)) & ((x - xA) < (xB - xA)));
plot(x, Y, 'linewidth', 1.5), grid on, xlim ([0 1.1])
采纳的回答
Star Strider
2022-9-11
The polyshape approach is the easiest way to go on this.
xA = 0;
xB = 1;
xf = 1
x = linspace(0, xf, xf*1e4 + 1);
a = 9.2; %
c = 0.5; % center of function
Y = sigmf(x, [a c]).*((0 <= (x - xA)) & ((x - xA) < (xB - xA)));
cx = trapz(x,x.*Y) ./ trapz(x,Y)
cy = trapz(Y,x.*Y) ./ trapz(Y,x)
p = polyshape(x,Y);
[xc,yc] = centroid(p)
figure
plot(x, Y, 'linewidth', 1.5), grid on, xlim ([0 1.1])
The values from the two approaches are not exactly the same, however they are reasonably close.
.
3 个评论
Star Strider
2022-9-12
As always, my pleasure!
That is esentially the approach I used, with trapz.
The polyshape approach may be more accurate, however the difference is slight —
xA = 0;
xB = 1;
xf = 1;
x = linspace(0, xf, xf*1e4 + 1);
a = 9.2; %
c = 0.5; % center of function
Y = sigmf(x, [a c]).*((0 <= (x - xA)) & ((x - xA) < (xB - xA)));
cx = trapz(x,x.*Y) ./ trapz(x,Y)
cy = trapz(Y,x.*Y) ./ trapz(Y,x)
p = polyshape(x,Y);
[xc,yc] = centroid(p)
x_accuracy = abs(xc-cx)/mean([xc cx])
y_accuracy = abs(yc-cy)/mean([yc cy])
figure
plot(x, Y, 'linewidth', 1.5, 'DisplayName','Data'), grid on, xlim ([0 1.1])
hold on
plot(cx,cy,'r+', 'DisplayName','trapz')
plot(xc,yc,'rx', 'DisplayName','polyshape+centroid')
hold off
legend('Location','best')
The relative deviation of the individual values from the mean of each set is about 0.5% for the x-coordinate and about 1% for the y-coordinate. I am not certain how accurate they can be.
.
Star Strider
2024-11-4,12:04
Later, I also wrote an anonymous function for it:
ctrd = @(x,y) [trapz(x, x.*y) / trapz(x, y); trapz(y, x.*y) / trapz(y, x)]; % Returns: [Centroid x-Coordinate; Centroid y-Coordinate]
The (x,y) arguments are the ‘x’ and ‘y’ vectors describing the outline of the region.
.
更多回答(4 个)
Bruno Luong
2022-9-12
编辑:Bruno Luong
2022-9-12
xA = 0;
xB = 1;
a = 9.2;
c = 0.5;
sfun = @(x)sigmf(x, [a c]);
Area = integral(@(x)sfun(x),xA,xB);
xc = integral(@(x)sfun(x).*x,xA,xB)/Area
yc = integral(@(x)sfun(x).^2,xA,xB)/(2*Area)
1 个评论
Sam Chak
2022-9-12
Hi @M
If your intention is to find the defuzzified output value for membership function mf at the interval in x using the centroid method, then you can try this method:
xA = 0;
xB = 1;
x = xA:0.0001:xB;
mf = sigmf(x, [9.2 0.5]);
plot(x, mf), grid on, xlim([-0.2 1.2]), ylim([0 1.2]), xlabel('\it{x}'), ylabel('\mu(\it{x})')
xc = defuzz(x, mf, 'centroid')
xline(xc, '--', sprintf('%.4f', xc), 'LabelVerticalAlignment', 'middle');
Note that there should be no significant difference between
mf = sigmf(x, [9.2 0.5]).*((0 <= (x - xA)) & ((x - xA) < (xB - xA)));
and
mf = sigmf(x, [9.2 0.5]);
0 个评论
Image Analyst
2022-9-11
Kwanele
2024-11-2,23:11
% Define the functions
f1 = @(x) 0.5 * (80.94 - 0.25 * exp(2 * x.^2));
f2 = @(x) 8.997 - 0.5 * exp(x.^2);
% Define the limits of integration
a = 0;
b = 1.7;
% Use MATLAB's integral function
numerator = integral(f1, a, b);
denominator = integral(f2, a, b);
% Calculate the centroid
centroid = numerator / denominator;
% Display the results
fprintf('Numerator: %.4f\n', numerator);
fprintf('Denominator: %.4f\n', denominator);
fprintf('Centroid: %.4f\n', centroid);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!