How can i fill the common areas of two curves?

조회 수: 25 (최근 30일)
byungkeuk cho
byungkeuk cho 2019년 3월 19일
댓글: Akira Agata 2020년 6월 15일
I can draw two cureves as below.
y1 = @(x) 3*x.^2 + 2*x +1;
y2 = @(x) -5*x.^2 + x + 20;
x=-5:0.1:5;
figure(3),plot(x,y1(x),x,y2(x));
Then, now i would like to fill the common areas with red color but i have no idea.
How can i do that?
And what about if i would like to fill the common areas of three different curves?

채택된 답변

Akira Agata
Akira Agata 2019년 3월 19일
Another possible solution:
y1 = @(x) 3*x.^2 + 2*x +1;
y2 = @(x) -5*x.^2 + x + 20;
x = -5:0.1:5;
pgon1 = polyshape(x,y1(x));
pgon2 = polyshape(x,y2(x));
figure
plot(x,y1(x),x,y2(x))
hold on
plot(intersect(pgon1,pgon2),'EdgeColor','none')
pgon.png
  댓글 수: 6
byungkeuk cho
byungkeuk cho 2019년 3월 20일
One more question plz.
How can i change the colar of the shade?
Akira Agata
Akira Agata 2020년 6월 15일
You can change the color by setting FaceColor option, like:
...
plot(intersect([pgon1,pgon2,pgon3]),'EdgeColor','none','FaceColor','m')
More details can be foun in the documentation page.

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

추가 답변 (1개)

byungkeuk cho
byungkeuk cho 2019년 3월 20일
I am trying to shade the triangle between three lines.
but it is not working.
y1 = @(x) x ;
y2 = @(x) sqrt(3)/2*x ;
y3 = @(x) -x + 1 ;
x = 0:0.1:2;
pgon1 = polyshape(x,y1(x));
pgon2 = polyshape(x,y2(x));
pgon3 = polyshape(x,y3(x));
figure
plot(x,y1(x),x,y2(x),x,y3(x))
hold on
plot(intersect([pgon1,pgon2,pgon3]),'EdgeColor','none ')
1.bmp
  댓글 수: 3
byungkeuk cho
byungkeuk cho 2019년 3월 20일
I really appreciate it.
Rahman
Rahman 2020년 6월 14일
Mr. Akira Agata, I've a code that generates 2 curves and have an intersection point. How can I show the area on the right of the intersection as 'hatched grey'?
M = [0.54, 0.7, 0.74, 0.78];
X1 = [0.007856, 0.008394, 0.008607, 0.012584]; %X1 values
X2 = [0.007901, 0.008461, 0.008739, 0.011302]; %X2 values
xq = linspace(M(1), M(end), 1000); %adding intervals to make line smoother
X3 = interpn(M,X1,xq,'pchip'); %other type: pchip, makima, spline etc.
X4 = interpn(M,X2,xq,'pchip'); %other type: pchip, makima, spline etc.
set(0,'defaulttextinterpreter','latex')
figure(1)
hold all;
plot(xq, X3,'r-','LineWidth',2);
plot(xq, X4,'b--','LineWidth',2);
X=[xq,fliplr(xq)]; %create continuous x value array for plotting
Y=[X3,fliplr(X4)]; %create y values for out and then back
fill(X,Y,'g'); %plot filled area
xlabel('X','FontSize',12); % Label the x axis
ylabel('Y','FontSize',12); % Label the y axis
The code above generates following figure: (the whole area between 2 curves is shaded as green)
However, I'd like to have the right area after intersection (roughly from X = 0.75 to 0.78) shaded only. Also, in the plot above, I need to change the shaded area on the right after intersection (roughly from X = 0.75 to 0.78) as 'hatched grey'.
Any help will be appreciated.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by