MATLAB Answers

0

Fill the region between two lines

Asked by Shan Chu on 5 Feb 2019
Latest activity Answered by Star Strider
on 5 Feb 2019
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)

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by Star Strider
on 5 Feb 2019
 Accepted Answer

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.

  0 Comments

Sign in to comment.


Answer by YT
on 5 Feb 2019

  0 Comments

Sign in to comment.