Fill the region between two lines

1,053 views (last 30 days)
Hello all,
I plot two functions and then I want to fill the region between them in red (for example).
Could you please suggest me the function to do this?
Thanks
x=0:0.1:10;
y1=exp(-x/2);
y2=exp(-x/3);
figure
hold on
plot(x,y1)
plot(x,y2)

Accepted Answer

Star Strider
Star Strider on 5 Feb 2019
Use the patch (link) function:
x=0:0.1:10;
y1=exp(-x/2);
y2=exp(-x/3);
figure
hold all
plot(x,y1)
plot(x,y2)
patch([x fliplr(x)], [y1 fliplr(y2)], 'g')
hold off
To use it, create a closed area (the reason for the fliplr calls, since they create the closed area), and choose the color.

More Answers (1)

YT
YT on 5 Feb 2019
  1 Comment
Mike Ravicz
Mike Ravicz on 31 Mar 2022
NOTE: x and y's must be row vectors.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!