How can i fill the common areas of two curves?
23 views (last 30 days)
Show older comments
byungkeuk cho
on 19 Mar 2019
Commented: Akira Agata
on 15 Jun 2020
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?
0 Comments
Accepted Answer
Akira Agata
on 19 Mar 2019
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')
6 Comments
Akira Agata
on 15 Jun 2020
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.
More Answers (1)
byungkeuk cho
on 20 Mar 2019
3 Comments
Rahman
on 14 Jun 2020
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.
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!