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.

 채택된 답변

dpb
dpb 2018년 8월 14일
편집: dpb 2018년 8월 14일

0 개 추천

...
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

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!
dpb
dpb 2018년 8월 15일
Ah! I see that was the area highlighted... :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

질문:

2018년 8월 14일

댓글:

dpb
2018년 8월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by