How to find area enclosed between two curves?
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Torsten
2022-2-22
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))
0 个评论
更多回答(1 个)
Abolfazl Chaman Motlagh
2022-2-21
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)])
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!