How to find area enclosed between two curves?

2 visualizaciones (últimos 30 días)
Prakash Chettri
Prakash Chettri el 21 de Feb. de 2022
Respondida: Torsten el 22 de Feb. de 2022
Lx = 150;
Ly = 150;
T0 =0;
T1 = pi/4;
T=3.175;
N=8;
W=N*T;
X0=Lx/2;
Y0=0;
W_nominal=W/2;
for a=1:2
r=rem(a,2);
if r==0
xp=0; xn=0; i=1;
while (xp>=0 && xp<=Lx/2)
XPaxisl(i)=xp+(i-1)*Lx/1200; xp=XPaxisl(i);
XNaxisl(i)=xn+(i-1)*(-Lx)/1200; xn=XNaxisl(i);
Th(i)=T0+2*((T1-T0)*(XPaxisl(i)))/Lx;
TH(i)= T0+2*((T0-T1)*(XNaxisl(i)))/Lx;
Wp(i)=W_nominal/cos(Th(i));
WP(i)=W_nominal/cos(TH(i));
ypl(i)=Wp(i)+(Lx/(2*(T0-T1))*((-log(cos(T0)))+log(cos(T0+((2*(T1-T0)*XPaxisl(i))/Lx)))));
ynl(i)=WP(i)+(Lx/(2*(T1-T0))*((-log(cos(T0)))+log(cos(T0+((2*(T0-T1)*XNaxisl(i))/Lx)))));
i=i+1;
end
plot(XPaxisl,ypl, '-g')
hold on
plot(XNaxisl,ynl, '-g')
hold on
end
%% shifting
r=rem(a,2);
if r==0
xp=0; xn=0; i=1; y=0; yn=0;
while (xp>=0 && xp<=Lx/2) && (y<=Ly/2)
XPaxiss1(i)=xp+(i-1)*Lx/1200; xp=XPaxiss1(i);
yps1(i) =(a*W_nominal)+Lx/(2*(T0-T1))*((-log(cos(T0)))+log(cos(T0+((2*(T1-T0)*XPaxiss1(i))/Lx))));
i=i+1;
end
else
xp=0; xn=0; i=1; y=0; yn=0;
while (xp>=0 && xp<=Lx/2) && (y<=Ly/2)
XPaxiss1(i)=xp+(i-1)*Lx/1200; xp=XPaxiss1(i);
XNaxiss1(i)=xn+(i-1)*(-Lx)/1200; xn=XNaxiss1(i);
yps1(i) =(a*W_nominal)+Lx/(2*(T0-T1))*((-log(cos(T0)))+log(cos(T0+((2*(T1-T0)*XPaxiss1(i))/Lx))));
yns1(i)= (a*W_nominal)+Lx/(2*(T1-T0))*((-log(cos(T0)))+log(cos(T0+((2*(T0-T1)*XNaxiss1(i))/Lx))));
i=i+1;
end
plot(XPaxiss1,yps1, '-r')
hold on
plot(XNaxiss1,yns1, '-r')
xlim([-Lx/2 Lx/2])
ylim([-Ly/2 Ly/2])
end
end
I want to claculate the area between red and green curves using trapz but im not getting the exact result and it is showing error while calculating.can anyone tell how can i calculate area? I attached code for reference.

Respuesta aceptada

Torsten
Torsten el 22 de Feb. de 2022
Lx = 150;
Ly = 150;
T0 =0;
T1 = pi/4;
T=3.175;
N=8;
W=N*T;
X0=Lx/2;
Y0=0;
W_nominal=W/2;
for a=1:2
r=rem(a,2);
if r==0
xp=0; xn=0; i=1;
while (xp>=0 && xp<=Lx/2)
XPaxisl(i)=xp+(i-1)*Lx/1200; xp=XPaxisl(i);
XNaxisl(i)=xn+(i-1)*(-Lx)/1200; xn=XNaxisl(i);
Th(i)=T0+2*((T1-T0)*(XPaxisl(i)))/Lx;
TH(i)= T0+2*((T0-T1)*(XNaxisl(i)))/Lx;
Wp(i)=W_nominal/cos(Th(i));
WP(i)=W_nominal/cos(TH(i));
ypl(i)=Wp(i)+(Lx/(2*(T0-T1))*((-log(cos(T0)))+log(cos(T0+((2*(T1-T0)*XPaxisl(i))/Lx)))));
ynl(i)=WP(i)+(Lx/(2*(T1-T0))*((-log(cos(T0)))+log(cos(T0+((2*(T0-T1)*XNaxisl(i))/Lx)))));
i=i+1;
end
%figure(1)
plot(XPaxisl,ypl, '-g')
hold on
plot(XNaxisl,ynl, '-g')
hold on
x1 = XPaxisl;
y1 = ypl;
x2 = XNaxisl;
y2 = ynl;
end
%% shifting
r=rem(a,2);
if r==0
xp=0; xn=0; i=1; y=0; yn=0;
while (xp>=0 && xp<=Lx/2) && (y<=Ly/2)
XPaxiss1(i)=xp+(i-1)*Lx/1200; xp=XPaxiss1(i);
yps1(i) =(a*W_nominal)+Lx/(2*(T0-T1))*((-log(cos(T0)))+log(cos(T0+((2*(T1-T0)*XPaxiss1(i))/Lx))));
i=i+1;
end
else
xp=0; xn=0; i=1; y=0; yn=0;
while (xp>=0 && xp<=Lx/2) && (y<=Ly/2)
XPaxiss1(i)=xp+(i-1)*Lx/1200; xp=XPaxiss1(i);
XNaxiss1(i)=xn+(i-1)*(-Lx)/1200; xn=XNaxiss1(i);
yps1(i) =(a*W_nominal)+Lx/(2*(T0-T1))*((-log(cos(T0)))+log(cos(T0+((2*(T1-T0)*XPaxiss1(i))/Lx))));
yns1(i)= (a*W_nominal)+Lx/(2*(T1-T0))*((-log(cos(T0)))+log(cos(T0+((2*(T0-T1)*XNaxiss1(i))/Lx))));
i=i+1;
end
plot(XPaxiss1,yps1, '-r')
hold on
plot(XNaxiss1,yns1, '-r')
xlim([-Lx/2 Lx/2])
ylim([-Ly/2 Ly/2])
x3 = XPaxiss1;
y3 = yps1;
x4 = XNaxiss1;
y4 = yns1;
end
end
x2=x2(end:-1:1);
y2=y2(end:-1:1);
x4=x4(end:-1:1);
y4=y4(end:-1:1);
X1 = horzcat(x2(1:end-1),x1)
Y1 = horzcat(y2(1:end-1),y1);
X2 = horzcat(x4(1:end-1),x3)
Y2 = horzcat(y4(1:end-1),y3);
%figure(2)
%plot(X1,Y1)
%hold on
%plot(X2,Y2)
%xlim([-Lx/2 Lx/2])
%ylim([-Ly/2 Ly/2])
Area = trapz(X1,abs(Y1-Y2))

Más respuestas (1)

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh el 21 de Feb. de 2022
i don't think the code you include in your question content related to what you want.
you have to vector, which are values of your curves at your grid points x.
here's an example for doing this task:
x = 0:0.1:20;
y1 = exp(x/2);
y2 = (x).^3;
dY = abs(y1-y2);
plot(x,y1,'Color','r','LineWidth',2);
hold on; grid on;
plot(x,y2,'Color','g','LineWidth',2);
plot(x,dY,'Color','b','LineWidth',2);
legend('y1 = exp(x/2)','y2= x^3','|y1-y2|','Location','best');
Diff = trapz(x,dY);
disp(['Area between 2 Curves :' num2str(Diff)])
Area between 2 Curves :26164.3149
  1 comentario
Prakash Chettri
Prakash Chettri el 22 de Feb. de 2022
Thank you for the help. But i want to calculate area between curve given by "ypl(i)" and "yps1(i)" over x domain as XPaxisl(i) as noted in a code attached by me.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB 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