How do I add a range and shade the area between these two graphs?
2 views (last 30 days)
Show older comments
Nico Degabriele
on 15 May 2022
Commented: Star Strider
on 15 May 2022
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 Comments
Accepted Answer
Star Strider
on 15 May 2022
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 Comments
Star Strider
on 15 May 2022
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
More Answers (0)
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!