## Discrete-Time Proportional-Integral-Derivative (PID) Controllers

All the PID controller object types, `pid`, `pidstd`, `pid2`, and `pidstd2`, can represent PID controllers in discrete time.

### Discrete-Time PID Controller Representations

Discrete-time PID controllers are expressed by the following formulas.

FormFormula
Parallel (`pid`)

`$C={K}_{p}+{K}_{i}IF\left(z\right)+\frac{{K}_{d}}{{T}_{f}+DF\left(z\right)},$`

where:

• Kp = proportional gain

• Ki = integrator gain

• Kd = derivative gain

• Tf = derivative filter time

Standard (`pidstd`)

`$C={K}_{p}\left(1+\frac{1}{{T}_{i}}IF\left(z\right)+\frac{{T}_{d}}{\frac{{T}_{d}}{N}+DF\left(z\right)}\right),$`

where:

• Kp = proportional gain

• Ti = integrator time

• Td = derivative time

• N = derivative filter divisor

2-DOF Parallel (`pid2`)

The relationship between the 2-DOF controller’s output (u) and its two inputs (r and y) is:

`$u={K}_{p}\left(br-y\right)+{K}_{i}IF\left(z\right)\left(r-y\right)+\frac{{K}_{d}}{{T}_{f}+DF\left(z\right)}\left(cr-y\right).$`

In this representation:

• Kp = proportional gain

• Ki = integrator gain

• Kd = derivative gain

• Tf = derivative filter time

• b = setpoint weight on proportional term

• c = setpoint weight on derivative term

2-DOF Standard (`pidstd2` object)

`$u={K}_{p}\left[\left(br-y\right)+\frac{1}{{T}_{i}}IF\left(z\right)\left(r-y\right)+\frac{{T}_{d}}{\frac{{T}_{d}}{N}+DF\left(z\right)}\left(cr-y\right)\right].$`

In this representation:

• Kp = proportional gain

• Ti = integrator time

• Td = derivative time

• N = derivative filter divisor

• b = setpoint weight on proportional term

• c = setpoint weight on derivative term

In all of these expressions, IF(z) and DF(z) are the discrete integrator formulas for the integrator and derivative filter, respectively. Use the `IFormula` and `DFormula` properties of the controller objects to set the IF(z) and DF(z) formulas. The next table shows available formulas for IF(z) and DF(z). Ts is the sample time.

`IFormula` or `DFormula`IF(z) or DF(z)
`ForwardEuler` (default)

`$\frac{{T}_{s}}{z-1}$`

`BackwardEuler`

`$\frac{{T}_{s}z}{z-1}$`

`Trapezoidal`

`$\frac{{T}_{s}}{2}\frac{z+1}{z-1}$`

If you do not specify a value for `IFormula`, `DFormula`, or both when you create the controller object, `ForwardEuler` is used by default. For more information about setting and changing the discrete integrator formulas, see the reference pages for the controller objects, `pid`, `pidstd`, `pid2`, and `pidstd2`.

### Create Discrete-Time Standard-Form PID Controller

This example shows how to create a standard-form discrete-time Proportional-Integral-Derivative (PID) controller that has Kp = 29.5, Ti = 1.13, Td = 0.15 N = 2.3, and sample time Ts  0.1 :

```C = pidstd(29.5,1.13,0.15,2.3,0.1,... 'IFormula','Trapezoidal','DFormula','BackwardEuler')```

This command creates a `pidstd` model with $IF\left(z\right)=\frac{{T}_{s}}{2}\frac{z+1}{z-1}$ and $DF\left(z\right)=\frac{{T}_{s}z}{z-1}$.

You can set the discrete integrator formulas for a parallel-form controller in the same way, using `pid`.

### Discrete-Time 2-DOF PI Controller in Standard Form

Create a discrete-time 2-DOF PI controller in standard form, using the trapezoidal discretization formula. Specify the formula using `Name,Value` syntax.

```Kp = 1; Ti = 2.4; Td = 0; N = Inf; b = 0.5; c = 0; Ts = 0.1; C2 = pidstd2(Kp,Ti,Td,N,b,c,Ts,'IFormula','Trapezoidal')```
```C2 = 1 Ts*(z+1) u = Kp * [(b*r-y) + ---- * -------- * (r-y)] Ti 2*(z-1) with Kp = 1, Ti = 2.4, b = 0.5, Ts = 0.1 Sample time: 0.1 seconds Discrete-time 2-DOF PI controller in standard form ```

Setting `Td` = 0 specifies a PI controller with no derivative term. As the display shows, the values of `N` and `c` are not used in this controller. The display also shows that the trapezoidal formula is used for the integrator.