# truncate

Class: prob.TruncatableDistribution
Package: prob

Truncate probability distribution object

## Syntax

`t = truncate(pd,lower,upper)`

## Description

`t = truncate(pd,lower,upper)` returns a probability distribution `t`, which is the probability distribution `pd` truncated to the specified interval with lower limit, `lower`, and upper limit, `upper`.

## Input Arguments

collapse all

### `pd` — Probability distributionprobability distribution object

Probability distribution, specified as a probability distribution object. Create a probability distribution object with specified parameter values using `makedist`. Alternatively, for fittable distributions, create a probability distribution object by fitting it to data using `fitdist` or the Distribution Fitting app.

### `lower` — Lower truncation limitscalar value

Lower truncation limit, specified as a scalar value.

Data Types: `single` | `double`

### `upper` — Upper truncation limitscalar value

Upper truncation limit, specified as a scalar value.

Data Types: `single` | `double`

## Output Arguments

collapse all

### `t` — Truncated distributionprobability distribution object

Truncated distribution, returned as a probability distribution object. The pdf of `t` is 0 outside the truncation interval. Inside the truncation interval, the pdf of `t` is equal to the pdf of `pd`, but divided by the probability assigned to that interval by `pd`.

## Examples

collapse all

### Truncate a Probability Distribution

Create a standard normal probability distribution object.

`pd = makedist('Normal')`
```pd = NormalDistribution Normal distribution mu = 0 sigma = 1```

Truncate the distribution to have a lower limit of -2 and an upper limit of 2.

`t = truncate(pd,-2,2)`
```t = NormalDistribution Normal distribution mu = 0 sigma = 1 Truncated to the interval [-2, 2]```

Plot the pdf of the original and truncated distributions for a visual comparison.

```x = -3:.1:3; figure; plot(x,pdf(pd,x),'Color','red','LineWidth',2) hold on; plot(x,pdf(t,x),'Color','blue','LineWidth',2) hold off;```

### Generate Random Numbers from a Truncated Distribution

Create a standard normal probability distribution object.

`pd = makedist('Normal')`
```pd = NormalDistribution Normal distribution mu = 0 sigma = 1```

Truncate the distribution by restricting it to positive values. Set the lower limit to 0 and the upper limit to infinity.

`t = truncate(pd,0,inf)`
```t = NormalDistribution Normal distribution mu = 0 sigma = 1 Truncated to the interval [0, Inf]```

Generate random numbers from the truncated distribution and visualize with a histogram.

```rng default % For reproducibility r = random(t,10000,1); histogram(r,100)```