How can i fill the common areas of two curves?

25 次查看(过去 30 天)
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 个评论
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 中查找有关 2-D and 3-D Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by