How to plot an area limited by two functions?

Hello everybody, so i am currently trying to figure out how to color an area inside my plot.
figure;
hold on;
wavelenght = 1520:0.1:1580;
power = (cos((wavelenght-1520)/60*pi)+1)/2;
wl = 1545:0.1:1555;
p = sin((wl-1545)/10*pi);
%Plot 1 ----------------------------
subplot(1,2,1)
plot(wavelenght,power,wavelenght,1-power,wl,p);
xlabel('Wavelengh [nm]');
ylabel('Power transmission');
%Plot 2 ----------------------------
subplot(1,2,2)
hold on;
plot(wavelenght,power,wavelenght,1-power,wl,p);
xlabel('Wavelengh [nm]');
ylabel('Power transmission');
My code currently shows 2 same plots and the goal is to color the area marked green in the picture below
I've tried the fill-command but since the area is limited by a function on the x-Axis and the y-Axis it does not work for me. Any suggestions would be helpful. Thank you.

 Accepted Answer

dpb
dpb on 14 Aug 2018
Edited: dpb on 14 Aug 2018
...
i1=find(wavelength==wl(1));
i2=find(wavelength==wl(end));
minp=min([power(i1:i2);1-power(i1:i2);p]);
hold on
hA=area(wl,minp);
results in
Fortunate here the indices of the two x vectors did actually match up; if didn't would have to evaluate the function on those bounding regions to match up exactly.
NB: Being OCD-like, I corrected spelling of wavelength VBG

2 Comments

Thanks a lot. I actually needed following area:
But i only had to change
minp=min([power(i1:i2);1-power(i1:i2);p]);
to
minp=min([power(i1:i2);p]);
from your code.
Thank you very much for the quick answer. Much appreciated!
Ah! I see that was the area highlighted... :)

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 14 Aug 2018

Commented:

dpb
on 15 Aug 2018

Community Treasure Hunt

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

Start Hunting!