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
댓글 수: 0
채택된 답변
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
Star Strider
2022년 5월 15일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!