# pid

Create PID controller in parallel form, convert to parallel-form PID controller

## Syntax

C = pid(Kp,Ki,Kd,Tf)C = pid(Kp,Ki,Kd,Tf,Ts)C = pid(sys)C = pid(Kp)C = pid(Kp,Ki)C = pid(Kp,Ki,Kd)C = pid(...,Name,Value)C = pid

## Description

C = pid(Kp,Ki,Kd,Tf) creates a continuous-time PID controller with proportional, integral, and derivative gains Kp, Ki, and Kd and first-order derivative filter time constant Tf:

$C={K}_{p}+\frac{{K}_{i}}{s}+\frac{{K}_{d}s}{{T}_{f}s+1}.$

This representation is in parallel form. If all of Kp, Ki, Kd, and Tf are real, then the resulting C is a pid controller object. If one or more of these coefficients is tunable (realp or genmat), then C is a tunable generalized state-space (genss) model object.

C = pid(Kp,Ki,Kd,Tf,Ts) creates a discrete-time PID controller with sample time Ts. The controller is:

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

IF(z) and DF(z) are the discrete integrator formulas for the integrator and derivative filter. By default,

$IF\left(z\right)=DF\left(z\right)=\frac{{T}_{s}}{z-1}.$

To choose different discrete integrator formulas, use the IFormula and DFormula properties. (See Properties for more information about IFormula and DFormula). If DFormula = 'ForwardEuler' (the default value) and Tf ≠ 0, then Ts and Tf must satisfy Tf > Ts/2. This requirement ensures a stable derivative filter pole.

C = pid(sys) converts the dynamic system sys to a parallel form pid controller object.

C = pid(Kp) creates a continuous-time proportional (P) controller with Ki = 0, Kd = 0, and Tf = 0.

C = pid(Kp,Ki) creates a proportional and integral (PI) controller with Kd = 0 and Tf = 0.

C = pid(Kp,Ki,Kd) creates a proportional, integral, and derivative (PID) controller with Tf = 0.

C = pid(...,Name,Value) creates a controller or converts a dynamic system to a pid controller object with additional options specified by one or more Name,Value pair arguments.

C = pid creates a P controller with Kp = 1.

## Input Arguments

 Kp Proportional gain. Kp can be: A real and finite value.An array of real and finite values.A tunable parameter (realp) or generalized matrix (genmat).A tunable surface for gain-scheduled tuning, created using tunableSurface (requires Robust Control Toolbox™ software). When Kp = 0, the controller has no proportional action. Default: 1 Ki Integral gain. Ki can be: A real and finite value.An array of real and finite values.A tunable parameter (realp) or generalized matrix (genmat).A tunable surface for gain-scheduled tuning, created using tunableSurface (requires Robust Control Toolbox software). When Ki = 0, the controller has no integral action. Default: 0 Kd Derivative gain. Kd can be: A real and finite value.An array of real and finite values.A tunable parameter (realp) or generalized matrix (genmat).A tunable surface for gain-scheduled tuning, created using tunableSurface (requires Robust Control Toolbox software). When Kd = 0, the controller has no derivative action. Default: 0 Tf Time constant of the first-order derivative filter. Tf can be: A real, finite, and nonnegative value.An array of real, finite, and nonnegative values.A tunable parameter (realp) or generalized matrix (genmat).A tunable surface for gain-scheduled tuning, created using tunableSurface (requires Robust Control Toolbox software). When Tf = 0, the controller has no filter on the derivative action. Default: 0 Ts Sample time. To create a discrete-time pid controller, provide a positive real value (Ts > 0). pid does not support discrete-time controller with undetermined sample time (Ts = -1). Ts must be a scalar value. In an array of pid controllers, each controller must have the same Ts. Default: 0 (continuous time) sys SISO dynamic system to convert to parallel pid form. sys must represent a valid PID controller that can be written in parallel form with Tf ≥ 0. sys can also be an array of SISO dynamic systems.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Use Name,Value syntax to set the numerical integration formulas IFormula and DFormula of a discrete-time pid controller, or to set other object properties such as InputName and OutputName. For information about available properties of pid controller objects, see Properties.

## Output Arguments

 C PID controller, represented as a pid controller object, an array of pid controller objects, a genss object, or a genss array. If all the gains Kp, Ki, Kd, and Tf have numeric values, then C is a pid controller object. When the gains are numeric arrays, C is an array of pid controller objects. The controller type (P, I, PI, PD, PDF, PID, PIDF) depends upon the values of the gains. For example, when Kd = 0, but Kp and Ki are nonzero, C is a PI controller. If one or more gains is a tunable parameter (realp), generalized matrix (genmat), or tunable gain surface (tunableSurface), then C is a generalized state-space model (genss).

## Properties

 Kp, Ki, Kd PID controller gains. The Kp, Ki, and Kd properties store the proportional, integral, and derivative gains, respectively. Kp, Ki, and Kd are real and finite. Tf Derivative filter time constant. The Tf property stores the derivative filter time constant of the pid controller object. Tf is real, finite, and nonnegative. IFormula Discrete integrator formula IF(z) for the integrator of the discrete-time pid controller C: $C={K}_{p}+{K}_{i}IF\left(z\right)+\frac{{K}_{d}}{{T}_{f}+DF\left(z\right)}.$ IFormula can take the following values: 'ForwardEuler' — IF(z) = $\frac{{T}_{s}}{z-1}.$This formula is best for small sample time, where the Nyquist limit is large compared to the bandwidth of the controller. For larger sample time, the ForwardEuler formula can result in instability, even when discretizing a system that is stable in continuous time.'BackwardEuler' — IF(z) = $\frac{{T}_{s}z}{z-1}.$An advantage of the BackwardEuler formula is that discretizing a stable continuous-time system using this formula always yields a stable discrete-time result.'Trapezoidal' — IF(z) = $\frac{{T}_{s}}{2}\frac{z+1}{z-1}.$An advantage of the Trapezoidal formula is that discretizing a stable continuous-time system using this formula always yields a stable discrete-time result. Of all available integration formulas, the Trapezoidal formula yields the closest match between frequency-domain properties of the discretized system and the corresponding continuous-time system. When C is a continuous-time controller, IFormula is ''. Default: 'ForwardEuler' DFormula Discrete integrator formula DF(z) for the derivative filter of the discrete-time pid controller C: $C={K}_{p}+{K}_{i}IF\left(z\right)+\frac{{K}_{d}}{{T}_{f}+DF\left(z\right)}.$ DFormula can take the following values: 'ForwardEuler' — DF(z) = $\frac{{T}_{s}}{z-1}.$This formula is best for small sample time, where the Nyquist limit is large compared to the bandwidth of the controller. For larger sample time, the ForwardEuler formula can result in instability, even when discretizing a system that is stable in continuous time.'BackwardEuler' — DF(z) = $\frac{{T}_{s}z}{z-1}.$An advantage of the BackwardEuler formula is that discretizing a stable continuous-time system using this formula always yields a stable discrete-time result.'Trapezoidal' — DF(z) = $\frac{{T}_{s}}{2}\frac{z+1}{z-1}.$An advantage of the Trapezoidal formula is that discretizing a stable continuous-time system using this formula always yields a stable discrete-time result. Of all available integration formulas, the Trapezoidal formula yields the closest match between frequency-domain properties of the discretized system and the corresponding continuous-time system.The Trapezoidal value for DFormula is not available for a pid controller with no derivative filter (Tf = 0). When C is a continuous-time controller, DFormula is ''. Default: 'ForwardEuler' InputDelay Time delay on the system input. InputDelay is always 0 for a pid controller object. OutputDelay Time delay on the system Output. OutputDelay is always 0 for a pid controller object. Ts Sample time. For continuous-time models, Ts = 0. For discrete-time models, Ts is a positive scalar representing the sampling period. This value is expressed in the unit specified by the TimeUnit property of the model. To denote a discrete-time model with unspecified sample time, set Ts = -1. Changing this property does not discretize or resample the model. Use c2d and d2c to convert between continuous- and discrete-time representations. Use d2d to change the sample time of a discrete-time system. Default: 0 (continuous time) TimeUnit String representing the unit of the time variable. This property specifies the units for the time variable, the sample time Ts, and any time delays in the model. Use any of the following values: 'nanoseconds''microseconds''milliseconds''seconds' 'minutes''hours''days''weeks''months''years' Changing this property has no effect on other properties, and therefore changes the overall system behavior. Use chgTimeUnit to convert between time units without modifying system behavior. Default: 'seconds' InputName Input channel names. Set InputName to a string for single-input model. For a multi-input model, set InputName to a cell array of strings. Alternatively, use automatic vector expansion to assign input names for multi-input models. For example, if sys is a two-input model, enter: sys.InputName = 'controls'; The input names automatically expand to {'controls(1)';'controls(2)'}. You can use the shorthand notation u to refer to the InputName property. For example, sys.u is equivalent to sys.InputName. Input channel names have several uses, including: Identifying channels on model display and plotsExtracting subsystems of MIMO systemsSpecifying connection points when interconnecting models Default: Empty string '' for all input channels InputUnit Input channel units. Use InputUnit to keep track of input signal units. For a single-input model, set InputUnit to a string. For a multi-input model, set InputUnit to a cell array of strings. InputUnit has no effect on system behavior. Default: Empty string '' for all input channels InputGroup Input channel groups. The InputGroup property lets you assign the input channels of MIMO systems into groups and refer to each group by name. Specify input groups as a structure. In this structure, field names are the group names, and field values are the input channels belonging to each group. For example: sys.InputGroup.controls = [1 2]; sys.InputGroup.noise = [3 5]; creates input groups named controls and noise that include input channels 1, 2 and 3, 5, respectively. You can then extract the subsystem from the controls inputs to all outputs using: sys(:,'controls') Default: Struct with no fields OutputName Output channel names. Set OutputName to a string for single-output model. For a multi-output model, set OutputName to a cell array of strings. Alternatively, use automatic vector expansion to assign output names for multi-output models. For example, if sys is a two-output model, enter: sys.OutputName = 'measurements'; The output names automatically expand to {'measurements(1)';'measurements(2)'}. You can use the shorthand notation y to refer to the OutputName property. For example, sys.y is equivalent to sys.OutputName. Output channel names have several uses, including: Identifying channels on model display and plotsExtracting subsystems of MIMO systemsSpecifying connection points when interconnecting models Default: Empty string '' for all output channels OutputUnit Output channel units. Use OutputUnit to keep track of output signal units. For a single-output model, set OutputUnit to a string. For a multi-output model, set OutputUnit to a cell array of strings. OutputUnit has no effect on system behavior. Default: Empty string '' for all output channels OutputGroup Output channel groups. The OutputGroup property lets you assign the output channels of MIMO systems into groups and refer to each group by name. Specify output groups as a structure. In this structure, field names are the group names, and field values are the output channels belonging to each group. For example: sys.OutputGroup.temperature = [1]; sys.InputGroup.measurement = [3 5]; creates output groups named temperature and measurement that include output channels 1, and 3, 5, respectively. You can then extract the subsystem from all inputs to the measurement outputs using: sys('measurement',:) Default: Struct with no fields Name System name. Set Name to a string to label the system. Default: '' Notes Any text that you want to associate with the system. Set Notes to a string or a cell array of strings. Default: {} UserData Any type of data you want to associate with system. Set UserData to any MATLAB® data type. Default: [] SamplingGrid Sampling grid for model arrays, specified as a data structure. For model arrays that are derived by sampling one or more independent variables, this property tracks the variable values associated with each model in the array. This information appears when you display or plot the model array. Use this information to trace results back to the independent variables. Set the field names of the data structure to the names of the sampling variables. Set the field values to the sampled variable values associated with each model in the array. All sampling variables should be numeric and scalar valued, and all arrays of sampled values should match the dimensions of the model array. For example, suppose you create a 11-by-1 array of linear models, sysarr, by taking snapshots of a linear time-varying system at times t = 0:10. The following code stores the time samples with the linear models.  sysarr.SamplingGrid = struct('time',0:10) Similarly, suppose you create a 6-by-9 model array, M, by independently sampling two variables, zeta and w. The following code attaches the (zeta,w) values to M. [zeta,w] = ndgrid(<6 values of zeta>,<9 values of w>) M.SamplingGrid = struct('zeta',zeta,'w',w) When you display M, each entry in the array includes the corresponding zeta and w values. M M(:,:,1,1) [zeta=0.3, w=5] = 25 -------------- s^2 + 3 s + 25 M(:,:,2,1) [zeta=0.35, w=5] = 25 ---------------- s^2 + 3.5 s + 25 ... For model arrays generated by linearizing a Simulink® model at multiple parameter values or operating points, the software populates SamplingGrid automatically with the variable values that correspond to each entry in the array. For example, the Simulink Control Design™ commands linearize and slLinearizer populate SamplingGrid in this way. Default: []

## Examples

collapse all

### PDF Controller

Create a continuous-time controller with proportional and derivative gains and a filter on the derivative term. To do so, set the integral gain to zero. Set the other gains and the filter time constant to the desired values.

Kp = 1; Ki = 0; % No integrator Kd = 3; Tf = 0.5; C = pid(Kp,Ki,Kd,Tf) 
C = s Kp + Kd * -------- Tf*s+1 with Kp = 1, Kd = 3, Tf = 0.5 Continuous-time PDF controller in parallel form. 

The display shows the controller type, formula, and parameter values, and verifies that the controller has no integrator term.

### Discrete-Time PI Controller

Create a discrete-time PI controller with trapezoidal discretization formula.

To create a discrete-time PI controller, set the value of Ts and the discretization formula using Name,Value syntax.

C1 = pid(5,2.4,'Ts',0.1,'IFormula','Trapezoidal') % Ts = 0.1s 
C1 = Ts*(z+1) Kp + Ki * -------- 2*(z-1) with Kp = 5, Ki = 2.4, Ts = 0.1 Sample time: 0.1 seconds Discrete-time PI controller in parallel form. 

Alternatively, you can create the same discrete-time controller by supplying Ts as the fifth input argument after all four PID parameters, Kp, Ki, Kd, and Tf. Since you only want a PI controller, set Kd and Tf to zero.

C2 = pid(5,2.4,0,0,0.1,'IFormula','Trapezoidal') 
C2 = Ts*(z+1) Kp + Ki * -------- 2*(z-1) with Kp = 5, Ki = 2.4, Ts = 0.1 Sample time: 0.1 seconds Discrete-time PI controller in parallel form. 

The display shows that C1 and C2 are the same.

### PID Controller with Named Input and Output

When you create a PID controller, set the dynamic system properties InputName and OutputName. This is useful, for example, when you interconnect the PID controller with other dynamic system models using the connect command.

C = pid(1,2,3,'InputName','e','OutputName','u') 
C = 1 Kp + Ki * --- + Kd * s s with Kp = 1, Ki = 2, Kd = 3 Continuous-time PID controller in parallel form. 

The display does not show the input and output names for the PID controller, but you can examine the property values. For instance, verify the input name of the controller.

C.InputName 
ans = 'e' 

### Array of PID Controllers

Create a 2-by-3 grid of PI controllers with proportional gain ranging from 1–2 across the array rows and integral gain ranging from 5–9 across columns.

To build the array of PID controllers, start with arrays representing the gains.

Kp = [1 1 1;2 2 2]; Ki = [5:2:9;5:2:9]; 

When you pass these arrays to the pid command, the command returns the array.

pi_array = pid(Kp,Ki,'Ts',0.1,'IFormula','BackwardEuler'); size(pi_array) 
2x3 array of PID controller. Each PID has 1 output and 1 input. 

Alternatively, use the stack command to build an array of PID controllers.

C = pid(1,5,0.1) % PID controller Cf = pid(1,5,0.1,0.5) % PID controller with filter pid_array = stack(2,C,Cf); % stack along 2nd array dimension 
C = 1 Kp + Ki * --- + Kd * s s with Kp = 1, Ki = 5, Kd = 0.1 Continuous-time PID controller in parallel form. Cf = 1 s Kp + Ki * --- + Kd * -------- s Tf*s+1 with Kp = 1, Ki = 5, Kd = 0.1, Tf = 0.5 Continuous-time PIDF controller in parallel form. 

These commands return a 1-by-2 array of controllers.

size(pid_array) 
1x2 array of PID controller. Each PID has 1 output and 1 input. 

All PID controllers in an array must have the same sample time, discrete integrator formulas, and dynamic system properties such as InputName and OutputName.

### Convert PID Controller from Standard to Parallel Form

Convert a standard form pidstd controller to parallel form.

Standard PID form expresses the controller actions in terms of an overall proportional gain Kp, integral and derivative time constants Ti and Td, and filter divisor N. You can convert any standard-form controller to parallel form using the pid command. For example, consider the following standard-form controller.

Kp = 2; Ti = 3; Td = 4; N = 50; C_std = pidstd(Kp,Ti,Td,N) 
C_std = 1 1 s Kp * (1 + ---- * --- + Td * ------------) Ti s (Td/N)*s+1 with Kp = 2, Ti = 3, Td = 4, N = 50 Continuous-time PIDF controller in standard form 

Convert this controller to parallel form using pid.

C_par = pid(C_std) 
C_par = 1 s Kp + Ki * --- + Kd * -------- s Tf*s+1 with Kp = 2, Ki = 0.667, Kd = 8, Tf = 0.08 Continuous-time PIDF controller in parallel form. 

### Convert Dynamic System to Parallel-Form PID Controller

Convert a continuous-time dynamic system that represents a PID controller to parallel pid form.

The following dynamic system, with an integrator and two zeros, is equivalent to a PID controller.

Create a zpk model of H. Then use the pid command to obtain H in terms of the PID gains Kp, Ki, and Kd.

H = zpk([-1,-2],0,3); C = pid(H) 
C = 1 Kp + Ki * --- + Kd * s s with Kp = 9, Ki = 6, Kd = 3 Continuous-time PID controller in parallel form. 

### Convert Discrete-Time Dynamic System to Parallel-Form PID Controller

Convert a discrete-time dynamic system that represents a PID controller with derivative filter to parallel pid form.

Create a discrete-time zpk model that represents a PIDF controller (two zeros and two poles, including the integrator pole at z = 1).

sys = zpk([-0.5,-0.6],[1 -0.2],3,'Ts',0.1); 

When you convert sys to PID form, the result depends on which discrete integrator formulas you specify for the conversion. For instance, use the default, ForwardEuler, for both the integrator and the derivative.

Cfe = pid(sys) 
Cfe = Ts 1 Kp + Ki * ------ + Kd * ----------- z-1 Tf+Ts/(z-1) with Kp = 2.75, Ki = 60, Kd = 0.0208, Tf = 0.0833, Ts = 0.1 Sample time: 0.1 seconds Discrete-time PIDF controller in parallel form. 

Now convert using the Trapezoidal formula.

Ctrap = pid(sys,'IFormula','Trapezoidal','DFormula','Trapezoidal') 
Ctrap = Ts*(z+1) 1 Kp + Ki * -------- + Kd * ------------------- 2*(z-1) Tf+Ts/2*(z+1)/(z-1) with Kp = -0.25, Ki = 60, Kd = 0.0208, Tf = 0.0333, Ts = 0.1 Sample time: 0.1 seconds Discrete-time PIDF controller in parallel form. 

The displays show the difference in resulting coefficient values and functional form.

For this particular dynamic system, you cannot write sys in parallel PID form using the BackwardEuler formula for the derivative filter. Doing so would result in Tf < 0, which is not permitted. In that case, pid returns an error.

### Discretize a Continuous-Time PID Controller

Discretize a continuous-time PID controller and set integral and derivative filter formulas.

Create a continuous-time controller and discretize it using the zero-order-hold method of the c2d command.

Ccon = pid(1,2,3,4); % continuous-time PIDF controller Cdis1 = c2d(Ccon,0.1,'zoh') 
Cdis1 = Ts 1 Kp + Ki * ------ + Kd * ----------- z-1 Tf+Ts/(z-1) with Kp = 1, Ki = 2, Kd = 3.04, Tf = 4.05, Ts = 0.1 Sample time: 0.1 seconds Discrete-time PIDF controller in parallel form. 

The display shows that c2d computes new PID gains for the discrete-time controller.

The discrete integrator formulas of the discretized controller depend on the c2d discretization method, as described in Tips. For the zoh method, both IFormula and DFormula are ForwardEuler.

Cdis1.IFormula Cdis1.DFormula 
ans = ForwardEuler ans = ForwardEuler 

If you want to use different formulas from the ones returned by c2d, then you can directly set the Ts, IFormula, and DFormula properties of the controller to the desired values.

Cdis2 = Ccon; Cdis2.Ts = 0.1; Cdis2.IFormula = 'BackwardEuler'; Cdis2.DFormula = 'BackwardEuler'; 

However, these commands do not compute new PID gains for the discretized controller. To see this, examine Cdis2 and compare the coefficients to Ccon and Cdis1.

Cdis2 
Cdis2 = Ts*z 1 Kp + Ki * ------ + Kd * ------------- z-1 Tf+Ts*z/(z-1) with Kp = 1, Ki = 2, Kd = 3, Tf = 4, Ts = 0.1 Sample time: 0.1 seconds Discrete-time PIDF controller in parallel form. 

expand all

### Tips

• Use pid either to create a pid controller object from known PID gains and filter time constant, or to convert a dynamic system model to a pid object.

• To deisgn a PID controller for a particular plant, use pidtune or pidTuner. To create a tunable PID controller as a control design block, use ltiblock.pid.

• Create arrays of pid controller objects by:

In an array of pid controllers, each controller must have the same sample time Ts and discrete integrator formulas IFormula and DFormula.

• To create or convert to a standard-form controller, use pidstd. Standard form expresses the controller actions in terms of an overall proportional gain Kp, integral and derivative times Ti and Td, and filter divisor N:

$C={K}_{p}\left(1+\frac{1}{{T}_{i}}\frac{1}{s}+\frac{{T}_{d}s}{\frac{{T}_{d}}{N}s+1}\right).$

• There are two ways to discretize a continuous-time pid controller:

• Use the c2d command. c2d computes new parameter values for the discretized controller. The discrete integrator formulas of the discretized controller depend upon the c2d discretization method you use, as shown in the following table.

c2d Discretization MethodIFormulaDFormula
'zoh'ForwardEulerForwardEuler
'foh'TrapezoidalTrapezoidal
'tustin'TrapezoidalTrapezoidal
'impulse'ForwardEulerForwardEuler
'matched'ForwardEulerForwardEuler

For more information about c2d discretization methods, See the c2d reference page. For more information about IFormula and DFormula, see Properties .

• If you require different discrete integrator formulas, you can discretize the controller by directly setting Ts, IFormula, and DFormula to the desired values. (See Discretize a Continuous-Time PID Controller.) However, this method does not compute new gain and filter-constant values for the discretized controller. Therefore, this method might yield a poorer match between the continuous- and discrete-time pid controllers than using c2d.