Skip to main content
v2026.11,858 entries · CC-BY 4.0

MATLAB Normal Distribution: normpdf, normcdf, normrnd

How to use MATLAB normpdf, normcdf, normrnd and normfit as one workflow, with documented outputs, tail-precision and sigma pitfalls.

Written and maintained by CASRAI Editorial Board

Last updated

MATLAB handles the normal distribution through a family of functions in the Statistics and Machine Learning Toolbox: normpdf gives the density, normcdf the cumulative probability, norminv the quantile, normrnd random draws, and normfit or fitdist estimate the parameters from data. This guide walks them as one workflow (simulate, fit, evaluate, read the output) using the examples and outputs published in the MathWorks documentation, and flags the places where the documented behavior is easy to misread.

What it is used for in research

The normal distribution has two parameters: the mean μ (any real number) and the standard deviation σ (which must be greater than zero). Its density is f(x | μ, σ) = 1 / (σ√(2π)) · exp(−(x − μ)² / (2σ²)). In MATLAB you reach for these functions to simulate measurement noise, to compute the probability that a measurement falls below or above a cutoff, to fit a normal model to a sample and report confidence intervals for its parameters, and to supply a reference distribution to a goodness-of-fit test. Simulation-heavy work such as a Monte Carlo simulation usually starts with normrnd. If you are still deciding between tools, see what MATLAB is used for and the MATLAB vs R comparison.

Install or access

The normal distribution functions are documented as part of the Statistics and Machine Learning Toolbox, which includes these functions. The reference pages for each function are under mathworks.com/help/stats. Check the product requirements and your institution’s license on the MathWorks site; this guide does not cover licensing.

Step-by-step: simulate, fit, evaluate

1. Generate normal random numbers with normrnd

The syntax is r = normrnd(mu,sigma) for one value, or r = normrnd(mu,sigma,sz1,...,szN) and r = normrnd(mu,sigma,sz) for arrays. Called as normrnd(mu,sigma,n) it returns an n-by-n matrix. Call rng first if you need repeatable results.

rng('default')
r = normrnd(0,1)          % 0.5377 in the MathWorks example

s = rng;
r = normrnd(3,10,[1,5])   % 8.3767  21.3389  -19.5885  11.6217  6.1877
rng(s);                   % restore the generator state
r = normrnd(3,10,[1,5])   % same five values again

2. Fit a normal distribution with normfit or fitdist

[muHat,sigmaHat,muCI,sigmaCI] = normfit(x,alpha) returns the estimates and confidence intervals; alpha defaults to 0.05, which gives 95% intervals. MathWorks’ example draws 1,000 values from a normal with μ = 3 and σ = 5 and requests 99% intervals:

rng('default')
x = normrnd(3,5,[1000,1]);
[muHat,sigmaHat,muCI,sigmaCI] = normfit(x,0.01)
% muHat    = 2.8368
% sigmaHat = 4.9948
% muCI     = [2.4292; 3.2445]
% sigmaCI  = [4.7218; 5.2989]

To work with an object instead, pd = fitdist(x,'Normal') returns a NormalDistribution object with mu and sigma properties. pd = makedist('Normal') creates the standard normal (μ = 0, σ = 1) by default.

3. Evaluate the density with normpdf

y = normpdf(x,mu,sigma) evaluates the density at each x. The defaults are μ = 0 and σ = 1, σ must be positive, and array arguments must be the same size (scalars expand).

x = [-2,-1,0,1,2];
y = normpdf(x)        % 0.0540  0.2420  0.3989  0.2420  0.0540
y = normpdf(x,2,1)    % 0.0001  0.0044  0.0540  0.2420  0.3989

4. Evaluate probabilities with normcdf

p = normcdf(x,mu,sigma) returns P(X ≤ x). It is equivalent to normcdf((x-mu)/sigma,0,1).

p = normcdf([-1 1]);
p(2) - p(1)                       % 0.6827, the share of a normal within one σ of the mean

normcdf([-2,-1,0,1,2],2,1)        % 0.0000  0.0013  0.0228  0.1587  0.5000

Output and how to read it

Question you are asking Function What the number means
How dense is the distribution at x? normpdf A density, not a probability. Values such as 0.3989 at the mean are heights of the curve and can exceed 1 when σ is small.
What is the probability of a value at or below x? normcdf P(X ≤ x), between 0 and 1.
What is the probability above x? normcdf(x,mu,sigma,'upper') The complement of the cdf, computed to stay accurate in the extreme upper tail.
Which x has cumulative probability p? norminv The quantile; see inverse normal.
Draw simulated data normrnd Random values with the given μ and σ.
Estimate μ and σ from a sample normfit, fitdist Parameter estimates, with confidence intervals from normfit.

Tail probabilities: use ‘upper’, not 1 – normcdf

The documentation shows why. 1-normcdf(10) evaluates to 0, because the cdf rounds to 1 in double precision, while normcdf(10,'upper') returns 7.6199e-24. For p-values and extreme-event probabilities, request the upper tail directly.

Confidence intervals on a probability

If you want an interval around a cdf value, pass the covariance of the parameter estimates as pCov: [p,pLo,pUp] = normcdf(x,mu,sigma,pCov,alpha). With pCov, x, mu and sigma must be scalars. MathWorks’ example simulates x = normrnd(5,2,1000,1) (after rng('default')), estimates phat = mle(x) as 4.9347 and 1.9969, gets pCov from normlike, and finds normcdf(0,muHat,sigmaHat,pCov) gives p = 0.0067 with bounds 0.0047 and 0.0095.

Checking fit

The normcdf page also shows a chi-square goodness-of-fit test against a fitted normal: chi2gof(x,'cdf',{@normcdf,mean(x),std(x)}) with x = normrnd(50,5,100,1) returns h = 0, meaning the test does not reject normality at the 5% level. For a normality test outside MATLAB, see the Shapiro-Wilk test in R, and for the small-sample counterpart of this distribution see t-distribution vs normal.

Troubleshooting

  • An error about σ. σ must be positive in normpdf and the other functions. In normrnd, σ = 0 is allowed and every result equals μ.
  • Size mismatch. If x, mu and sigma are all arrays, they must be the same size; only scalars expand.
  • Empty output from normrnd. A size argument of 0 or a negative number returns an empty array.
  • The sigma from normfit is not the maximum likelihood estimate. Without censoring, muHat is the sample mean and sigmaHat is the square root of the unbiased variance estimator. Use mle if you need the MLE of σ. With censoring, both outputs are maximum likelihood estimates.
  • pCov errors. The normcdf form with pCov accepts scalar x, mu and sigma only.
  • A tail probability of exactly 0. Use the 'upper' option as described above.
  • Results change between runs. Set the generator with rng('default'), or save and restore it with s = rng; ... rng(s).
  • Do not copy the “clone size” one-liner on the normrnd page. It reads normrnd(1,0,size(A)), which is μ = 1, σ = 0 and returns all ones. For random values of the same size as A, use normrnd(0,1,size(A)).

Free alternatives

The same four operations exist in free tools. The argument conventions differ, so check the order when translating code.

Operation MATLAB R (stats package) Python (scipy.stats.norm)
Density normpdf(x,mu,sigma) dnorm(x, mean = 0, sd = 1) norm.pdf(x, loc=0, scale=1)
Cumulative probability normcdf(x,mu,sigma) pnorm(q, mean = 0, sd = 1, lower.tail = TRUE) norm.cdf(x, loc=0, scale=1)
Quantile norminv(p,mu,sigma) qnorm(p, mean = 0, sd = 1) norm.ppf(q, loc=0, scale=1)
Random draws normrnd(mu,sigma,sz) rnorm(n, mean = 0, sd = 1) norm.rvs(loc=0, scale=1, size=1)

In SciPy, loc is the mean and scale is the standard deviation. In R, pnorm(..., lower.tail = FALSE) returns the probability above x, the counterpart of MATLAB’s 'upper' option.

FAQ

What is the difference between normpdf and normcdf in MATLAB?

normpdf returns the height of the density curve at each point; normcdf returns the cumulative probability P(X ≤ x). The pdf is a density, so it is not itself a probability.

How do I generate normally distributed random numbers in MATLAB?

Use normrnd(mu,sigma,sz), for example normrnd(3,10,[1,5]) for five values with mean 3 and standard deviation 10. Call rng beforehand for reproducible output.

How do I fit a normal distribution to data?

Use normfit(x) for estimates and confidence intervals, or fitdist(x,'Normal') for a NormalDistribution object with mu and sigma properties.

What are the default parameters?

normpdf uses μ = 0 and σ = 1 when you omit them, and makedist('Normal') returns the same standard normal.

Is the sigma from normfit the maximum likelihood estimate?

Not without censoring. It uses the unbiased variance estimator; use mle for the maximum likelihood estimate.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about MATLAB Normal Distribution: normpdf, normcdf, normrnd

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.