tl;dr: Multiple ways to do this both with and without the Statistics toolbox.
The methods eliminate the need for a large enough sample size to empirically estimate the PDF with a histogram.
Method 1: Use probability distribution objects (requires Statistics toolbox)
You can plot the probability density functions (PDFs) directly using the probability distribution objects which lets you exploit the makedist()
and pdf()
functions.
The PDF for X ~ Uniform(a,b) is obtained from pdf(pdX,x)
where x
contains the values from the domain (support) for the random variable X where you want to evaluate the PDF.
% MATLAB R2019a
% Setup
a = 0; % lowerbound for X ~ Uniform(a,b)
b = 3; % upperbound for X
mu = 1.5; % mean for Y ~ Normal(mu,sigma) E[Y] = mu
sigma = 0.75; % standard dev sqrt(Var(Y)) = sigma
% Create distributions
pdX = makedist('Uniform',a,b); X ~ Uniform(a,b)
pdY = makedist('Normal',mu,sigma); Y ~ Normal(mu,sigma)
step = 0.1;
Domain = -2:step:5; % Useful for plotting
% Plot
figure, hold on
plot(Domain,pdf(pdX,Domain),'b-','DisplayName','Uniform(0,3)') % Plot pdf for X
plot(Domain,pdf(pdY,Domain),'k-','DisplayName','Normal(1.5,0.75)') % Plot pdf for Y
legend('show')

Method 2: Use normpdf()
(requires Statistics toolbox)
You can easily implement the pdf for the Uniform distribution with
fXh =@(x) 0*(x<a) + (1/(b-a))*(a<=x & x<=b) + 0*(b<x); % pdf for Uniform(a,b)
which will require no toolbox.
Then use the built-in normpdf()
function (requires Statistics toolbox) with syntax normpdf(x,mu,sigma)
.
You get the same result.
figure, hold on
plot(Domain,fXh(Domain),'b-','DisplayName','Uniform(0,3)')
plot(Domain,normpdf(Domain,mu,sigma),'k-','DisplayName','Normal(1.5,0.75)')
legend('show')
Method 3: No toolboxes required
Implement the PDF for the Normal distribution (Gaussian) which will only have one term since it is unbounded. This avoids the need for the Statistics toolbox.
The PDFs for X and Y are obtained from fXh(Domain)
and fYh(Domain)
, respectively.
fYh =@(x) (1/(sigma*sqrt(2*pi)))*exp(-0.5*((x-mu)/sigma).^2); % pdf for Normal(mu,sigma)
% Plot
figure, hold on
plot(Domain,fXh(Domain),'b-','DisplayName','Uniform(0,3)')
plot(Domain,fYh(Domain),'k-','DisplayName','Normal(1.5,0.75)')
legend('show')