How do I add a range and shade the area between these two graphs?

조회 수: 2 (최근 30일)
Hi all. I was asked to use the vector random('exp',4,100,1) to create an empirical distribution function and compare it to an exponential distribution function for which I used the code below:
rng('default')
y = random('exp',4,100,1);
figure (1)
cdfplot(y)
hold on
x = 0:0.01:10;
plot(x,expcdf(x,4))
legend( 'Empirical CDF' , 'Exponential CDF' , 'Location' , 'best' )
hold off
The problem here is that I am required to make both graphs in the range [0,10] which I only managed to do for the exponential cdf. How could I do this for the empirical cdf please? I was then asked to shade the area between both graphs. How can this be done please? Thanks in advance

채택된 답변

Star Strider
Star Strider 2022년 5월 15일
I will leave the cdf calculations to you.
However the shading is not straightforward because the cdfplot contains two Inf values, and the patch function only works with finite values. That requires getting the values from the plot handles and a further addtional step to resolve —
rng('default')
y = random('exp',4,100,1);
figure (1)
h1 = cdfplot(y);
hold on
x = 0:0.01:10;
h2 = plot(x,expcdf(x,4));
cdfx = h1.XData;
ix1 = isfinite(cdfx);
cdfy = h1.YData;
px = h2.XData;
py = h2.YData;
patch([cdfx(ix1) flip(px)], [cdfy(ix1) flip(py)], 'g', 'FaceAlpha',0.5, 'EdgeColor','none')
hold off
legend( 'Empirical CDF' , 'Exponential CDF' , 'Location' , 'best' )
.
  댓글 수: 2
Nico Degabriele
Nico Degabriele 2022년 5월 15일
I see. I had searched for similar solutions but hadn't been able to come up with a way to adapt them for the two graphs. Thanks so much for your help!
Star Strider
Star Strider 2022년 5월 15일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Discrete Data Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by