How to fill the area between two curves?

17 views (last 30 days)

I want to shade the portion between red curve and green curve, but im not getting the proper way of doing it. Can anyone help me with your idea? I have attached a code and the figure for your reference.
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))

Accepted Answer

KSSV
KSSV on 21 Mar 2022
clear all %clear all variables
clc % clear output screen
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)
X1 = 1×71
-78.7500 -74.3750 -70.1250 -66.0000 -62.0000 -58.1250 -54.3750 -50.7500 -47.2500 -43.8750 -40.6250 -37.5000 -34.5000 -31.6250 -28.8750 -26.2500 -23.7500 -21.3750 -19.1250 -17.0000 -15.0000 -13.1250 -11.3750 -9.7500 -8.2500 -6.8750 -5.6250 -4.5000 -3.5000 -2.6250
Y1 = horzcat(y2(1:end-1),y1);
X2 = horzcat(x4(1:end-1),x3)
X2 = 1×71
-78.7500 -74.3750 -70.1250 -66.0000 -62.0000 -58.1250 -54.3750 -50.7500 -47.2500 -43.8750 -40.6250 -37.5000 -34.5000 -31.6250 -28.8750 -26.2500 -23.7500 -21.3750 -19.1250 -17.0000 -15.0000 -13.1250 -11.3750 -9.7500 -8.2500 -6.8750 -5.6250 -4.5000 -3.5000 -2.6250
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)) ;
% DRaw color across the area
X = [X1 flip(X1)] ;
Y = [Y1 flip(Y2)] ;
patch(X,Y,'r')

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 21 Mar 2022

Tags

Products

Community Treasure Hunt

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

Start Hunting!