# How to Calculate an area in MATLAB

32 views (last 30 days)
shahin sharafi on 6 Aug 2021
Commented: shahin sharafi on 6 Aug 2021
Hi
I am going to to calculate the area covered by nodes using boundary and polyarea functions. However, calculated area is not correct. For comparison, I did sketched a triangle in code and compared its area with area covered by nodes. Even though the nodes area is larger than triangle one, Calculated area of triangle is bigger than nodes area. Why does not ployarea work in this situation?
Regards,
clear all;clc;close all
%%
x=[1.5;1.5;2.7];
y=[0.4;1.2;0.85];
plot(x,y,'o')
k = boundary(x,y,0.1);% generate boundary of data points
hold on;
plot(x(k),y(k));
Triangle_A = polyarea(x,y)
hold on
%%
Mass=60;Lenght=1;g=10;Ja=60;alpha=(Mass*Lenght*g)/Ja;Zeta3=411.67/Ja;
Time_Delay=0.2;
N=7;
%%
s1=0;
s2=-Time_Delay;
[Phi_0_s,Phi_TD_s,Diff_Phi_TD_s]=Shape_Function(N,Time_Delay,s1,s2);
%%
[M,K]=M_K(N,Time_Delay);
%%
count=1;
KP=0:0.1:3;
KD=0:0.1:3;
x=zeros(1,count);
y=zeros(1,count);
for ii=1:length(KP)
KP(ii);
for i=1:length(KD)
KP1=Zeta3*KP(ii);
KD1=Zeta3*KD(i);
L=Evaluation(KP1,KD1,alpha,Phi_0_s,Phi_TD_s,M,K);
Max_Real_EignValues=max(real(eig(L)));
if Max_Real_EignValues<0
plot(KP(ii),KD((i)),'r*','MarkerSize',5)
x(1,count)=KP(ii);
y(1,count)=KD(i);
count=count+1;
hold on
end
end
end
hold on
k = boundary(x',y');% generate boundary of data points
plot(x(k),y(k))
Curve_Area= polyarea(x,y)
%% Below Lines are the functions related to top calculations
function [Phi_0_s,Phi_TD_s,Diff_Phi_TD_s]=Shape_Function(N,Time_Delay,s1,s2)
Phi_0_s(1)=1;
Phi_0_s(2)=1+2*s1/Time_Delay;
for k=3:N
Phi_0_s(k)=((2*k-3)*Phi_0_s(2)*Phi_0_s(k-1)-(k-2)*Phi_0_s(k-2))/(k-1);
end
Phi_0_s=Phi_0_s';
%%
Phi_TD_s(1)=1;
Phi_TD_s(2)=1+2*s2/Time_Delay;
for k=3:N
Phi_TD_s(k)=((2*k-3)*Phi_TD_s(2)*Phi_TD_s(k-1)-(k-2)*Phi_TD_s(k-2))/(k-1);
end
Phi_TD_s=Phi_TD_s';
%%
syms s
Phi_TTD_s=sym(zeros(1,N));
Phi_TTD_s(1)=1;
Phi_TTD_s(2)=1+2*s/Time_Delay;
for k=3:N
Phi_TTD_s(k)=((2*k-3)*Phi_TTD_s(2)*Phi_TTD_s(k-1)-(k-2)*Phi_TTD_s(k-2))/(k-1);
end
Diff_Phi_TD_s=diff(Phi_TTD_s,s);
Diff_Phi_TD_s=eval(subs(Diff_Phi_TD_s,s,-Time_Delay));
Diff_Phi_TD_s=double(Diff_Phi_TD_s);
Diff_Phi_TD_s=Diff_Phi_TD_s';
end
%%
function [A,B]=M_K(N,Time_Delay)
Delta=zeros(N,N);
A=zeros(N,N);
B=zeros(N,N);
for i=1:N
for j=1:N
if i==j
Delta(i,j)=1;
else
Delta(i,j)=0;
end
end
end
for i=1:N
for j=1:N
A(i,j)=(Time_Delay*Delta(i,j))/(2*i-1);
if i<j
if rem(i+j, 2) == 1
B(i,j)=2;
else
B(i,j)=0;
end
end
end
end
end
%%
function [L]=Evaluation(KP1,KD1,alpha,Phi_0_s,Phi_TD_s,M,K)
kp=KP1;
kd=KD1;
%% G1&G2
G1=alpha*Phi_0_s'-kp*Phi_TD_s';
G2=-kd*Phi_TD_s';
%% C3
C3=Phi_0_s'*inv(M)*Phi_0_s;
%% X Matrix
X1=(Phi_0_s*Phi_0_s')/C3;
X2=-((Phi_0_s*Phi_0_s'/M)*K)/C3;
X3=(Phi_0_s*G1)/C3;
X4=(Phi_0_s*G2-(Phi_0_s*Phi_0_s'/M)*K)/C3;
%% L Matrix
L1=K/M+X2/M;
L2=X1/M;
L3=X3/M;
L4=K/M+X4/M;
L=[L1 L2;L3 L4];
end

KSSV on 6 Aug 2021
This line:
Curve_Area= polyarea(x,y)
Should be
Curve_Area= polyarea(x(k),y(k))
Then you shall get:
Triangle_A =
0.4800
Curve_Area =
0.6450
shahin sharafi on 6 Aug 2021
Thank you so much.

R2019a

### Community Treasure Hunt

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

Start Hunting!