MATLAB Answers

0

How can i fill the common areas of two curves?

Asked by byungkeuk cho on 19 Mar 2019
Latest activity Commented on by byungkeuk cho on 20 Mar 2019
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

Sign in to comment.

Tags

2 Answers

Answer by Akira Agata
on 19 Mar 2019
 Accepted Answer

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

  5 Comments

Same approach works. The following is an example.
y1 = @(x) 3*x.^2 + 2*x +1;
y2 = @(x) -5*x.^2 + x + 20;
y3 = @(x) -10*x.^2 + 20*x + 30;
x = -5:0.1:5;
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')
pgon2.png
One more question plz.
How can i change the colar of the shade?

Sign in to comment.


Answer by byungkeuk cho on 20 Mar 2019

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

  2 Comments

You need to make a polyshape object correctly. In this case, target area shall be lower than y1, lower than y3 and higher than y2. Also, since these shapes are line, you would better setting 'Simplify' option to false.
Regarding how to change the color of the shade, you should set 'FaceColor' option when plotting.
Here is an example.
y1 = @(x) x;
y2 = @(x) sqrt(3)/2*x;
y3 = @(x) -x + 1;
x = 0:0.1:2;
pgon1 = polyshape([x(1) x x(end)],[-10 y1(x) -10],'Simplify',false);
pgon2 = polyshape([x(1) x x(end)],[10 y2(x) 10],'Simplify',false);
pgon3 = polyshape([x(1) x x(end)],[-10 y3(x) -10],'Simplify',false);
figure
plot(x,y1(x),x,y2(x),x,y3(x))
hold on
plot(intersect([pgon1,pgon2,pgon3]),...
'EdgeColor', 'none',...
'FaceColor', 'c') % change shade color to cyan
pgon3.png

Sign in to comment.