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

7 views (last 30 days)
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])

Answers (1)

Star Strider
Star Strider on 20 Oct 2015
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')

Tags

Community Treasure Hunt

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

Start Hunting!