Trying to fill an area between 3 lines? help with fill command?

조회 수: 15(최근 30일)
For an assignment, I have to fill the area between 3 lines with yellow. However, when using the fill command, I find it's filling up unwanted spaces. The area is between a parabola, a line, and the positive x axis. I am able to get the figure and its restrictions (the different lines are supposed to be colored), but just can't fill up the space between them. Any help would be greatly appreciated! Code:
x=linspace(0,18);
y1=x.^2;
y2=-(x/4) +9/2;
y3=x*0;
figure plot(x,y1,'b',x,y2,'r',x,y3,'m')
axis([0,20,0,6])

답변(1개)

Star Strider
Star Strider 2015년 10월 20일
That problem is a bit more involved than it might otherwise appear. I used the patch function because I can make it do what I want:
x=linspace(0, 18);
y=x.^2;
y2=x.*(-1./4)+(9./2);
y3=x.*0;
isct_y_y2 = @(x) x.^2 - (x.*(-1./4)+(9./2));
isct_y2_y3 = @(x) x.*(-1./4)+(9./2);
isct_y_y3 = @(x) x.^2;
x_y_y2 = fzero(isct_y_y2, 1);
x_y2_y3 = fzero(isct_y2_y3, 1);
x_y_y2_ix = find(x <= x_y_y2, 1, 'last');
x_y2_y3_ix = find(x <= x_y2_y3, 1, 'last');
%Fill in the Shape
%Figure
plot(x,y, x,y2, x,y3); grid on;
axis([-1,20,-1,5])
patch([x(1:x_y_y2_ix) x(x_y_y2_ix:x_y2_y3_ix) flip(x(1:x_y2_y3_ix))], [y(1:x_y_y2_ix) y2(x_y_y2_ix:x_y2_y3_ix) flip(y3(1:x_y2_y3_ix))], 'y')

태그

Community Treasure Hunt

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

Start Hunting!

Translated by