How can I compute the Area and the Centroid of the following shape?

조회 수: 121 (최근 30일)
M
M 2022년 9월 11일
편집: Bruno Luong 2022년 9월 12일
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
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])
  댓글 수: 6
M
M 2022년 9월 11일
@Torsten If that the case, the x_Centroid will be the length of the x always?
M
M 2022년 9월 11일
Hi @Star Strider, Do you have any idea how to compute the centroid here. Thanks

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2022년 9월 11일
The polyshape approach is the easiest way to go on this.
However it is possible to calculate this using the trapz function and the centroid definition —
xA = 0;
xB = 1;
xf = 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)
cx = 0.7138
cy = trapz(Y,x.*Y) ./ trapz(Y,x)
cy = 0.3934
p = polyshape(x,Y);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
[xc,yc] = centroid(p)
xc = 0.7176
yc = 0.3974
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.
.
  댓글 수: 2
M
M 2022년 9월 11일
편집: M 2022년 9월 11일
@Star Strider Thank you so much.
What do you think of the approach that Torsten suggested (https://en.wikipedia.org/wiki/Centroid
Section:
Locating by integral formula of a bounded region)
Do you think it gives more accurate answer?
Because any difference it causes effects in my application
Thanks
Star Strider
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)
cx = 0.7138
cy = trapz(Y,x.*Y) ./ trapz(Y,x)
cy = 0.3934
p = polyshape(x,Y);
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
[xc,yc] = centroid(p)
xc = 0.7176
yc = 0.3974
x_accuracy = abs(xc-cx)/mean([xc cx])
x_accuracy = 0.0053
y_accuracy = abs(yc-cy)/mean([yc cy])
y_accuracy = 0.0100
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.
.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Bruno Luong
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
xc = 0.7138
yc = integral(@(x)sfun(x).^2,xA,xB)/(2*Area)
yc = 0.3935

Sam Chak
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')
xc = 0.7138
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]);

Image Analyst
Image Analyst 2022년 9월 11일
centroid
Centroid of polyshape
Syntax
Description
[x,y] = centroid(polyin) returns the x-coordinates and the y-coordinates of the centroid of a polyshape.
[x,y] = centroid(polyin,I) returns the coordinates of the centroid of the Ith boundary of polyin.
This syntax is only supported when polyin is a scalar polyshape object.
  댓글 수: 4
M
M 2022년 9월 11일
편집: M 2022년 9월 11일
@Image Analyst I didnt get you. Can you explain please!
Image Analyst
Image Analyst 2022년 9월 12일
You didn't look up the help for polyshape, did you? It's just as trivial as @Star Strider showed:
p = polyshape(x,Y);
[x,y] = centroid(p)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by