How to plot PDF and CDF for a normal distribution in matlab

0x90 picture 0x90 · Nov 30, 2013 · Viewed 24.2k times · Source

I couldn't find a function in matlab that implement gets mean and standard deviation of normal distribution and plot its PDF and CDF.

I am afraid the two functions I have implemented bellow are missing something, since I get maximal value for pdfNormal which is greater than 1.

function plotNormPDF(u,s,color)
    mu = u; 
    sigma = s; 
    x = (mu - 5 * sigma) : (sigma / 100) : (mu + 5 * sigma); 
    pdfNormal = normpdf(x, mu, sigma);
    string = 'the maximal pdfNormal is';
    string = sprintf('%s :%d', string,max(pdfNormal));
    disp(string)
    plot(x, pdfNormal/max(pdfNormal),color); 
end

And for the CDF norm

function plotNormCDF(u,s,color)
    mu = u; 
    sigma = s; 
    x = (mu -  5*sigma) : (sigma / 100) : (mu + 5*sigma); 
    pdfNormal = normpdf(x, mu, sigma);
    plot(x,cumsum(pdfNormal)./max(cumsum(pdfNormal)),color)
end

Here is an example for using both:

plotNormCDF(0.2, 0.1,'r')
plotNormPDF(0.2, 0.1,'r')

enter image description here

enter image description here

Answer

juliohm picture juliohm · Nov 30, 2013

You don't need all that code, look how simpler it is:

mu = 0.2; sigma = 0.1;
x = linspace (mu-4*sigma, mu+4*sigma);
plot(x, normpdf (x,mu,sigma))
plot(x, normcdf (x,mu,sigma))