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

3 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Star Strider
Star Strider el 15 de Mayo de 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 comentarios
Nico Degabriele
Nico Degabriele el 15 de Mayo de 2022
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 el 15 de Mayo de 2022
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Discrete Data Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by