Contents | Index |
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
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:
This representation is in parallel form.
C = pid(Kp,Ki,Kd,Tf,Ts) creates a discrete-time PID controller with sampling time Ts. The controller is:
IF(z) and DF(z) are the discrete integrator formulas for the integrator and derivative filter. By default, IF(z) = DF(z) = Tsz/(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.
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 tune a PID controller for a particular plant, use pidtune or pidtool.
Create arrays of pid controller objects by:
Specifying array values for Kp,Ki,Kd, and Tf
Specifying an array of dynamic systems sys to convert to pid controller objects
Using stack to build arrays from individual controllers or smaller arrays
In an array of pid controllers, each controller must have the same sampling 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:
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 Method | IFormula | DFormula |
---|---|---|
'zoh' | ForwardEuler | ForwardEuler |
'foh' | Trapezoidal | Trapezoidal |
'tustin' | Trapezoidal | Trapezoidal |
'impulse' | ForwardEuler | ForwardEuler |
'matched' | ForwardEuler | ForwardEuler |
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 this example.) 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.
Optional comma-separated pairs of Name,Value arguments, where Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (''). You can specify several name-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.
pid controller objects have the following 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 values must be real and finite. |
Tf |
Derivative filter time constant. The Tf property stores the derivative filter time constant of the pid controller object. Tf must be real, finite, and greater than or equal to zero. |
IFormula |
Discrete integrator formula IF(z) for the integrator of the discrete-time pid controller C:
IFormula can take the following values:
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:
DFormula can take the following values:
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 |
Sampling time. For continuous-time models, Ts = 0. For discrete-time models, Ts is a positive scalar representing the sampling period expressed in the unit specified by the TimeUnit property of the model. To denote a discrete-time model with unspecified sampling 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 sampling time of a discrete-time system. Default: 0 (continuous time) |
TimeUnit |
String representing the unit of the time variable, any time delays in the model (for continuous-time models), and the sampling time Ts (for discrete-time models). TimeUnit can take the following values:
Changing this property 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 software automatically expands the input names 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:
Default: Empty string '' for all input channels |
InputUnit |
Input channel units. Use InputUnit to keep track of input signal units. Set InputUnit to a string for single-input model, or to a cell array of strings for a multi-input model. 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 whose field names are the group names and whose field values are the input channels belong 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 software automatically expands the output names 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:
Default: Empty string '' for all input channels |
OutputUnit |
Output channel units. Use OutputUnit to keep track of output signal units. Set OutputUnit to a string for single-input model, or to a cell array of strings for a multi-input model. OutputUnit has no effect on system behavior. Default: Empty string '' for all input 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 whose field names are the group names and whose field values are the output channels belong 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 wish to associate with the system. Set Notes to a string or a cell array of strings. Default: {} |
UserData | Any type of data you wish to associate with system. Set UserData to any MATLAB data type. Default: [] |
PID Controller with Proportional and Derivative Gains, and Filter Time Constant (PDF Controller)
Create a continuous-time controller with proportional and derivative gains, and filter time constant (PDF controller).
Kp=1; Ki=0; Kd=3; Tf=0.5; C = pid(Kp,Ki,Kd,Tf);
Confirm the controller type:
getType(C)
This command produces the result:
ans = PDF
![]() |
Discrete-Time PI Controller
Create a discrete-time PI controller with trapezoidal discretization formula.
To create a discrete-time controller, set the value of Ts using Name,Value syntax.
C = pid(5,2.4,'Ts',0.1,'IFormula','Trapezoidal') % Ts = 0.1s
This command produces the result:
Discrete-time PI controller in parallel form: Ts*(z+1) Kp + Ki * -------- 2*(z-1) with Kp = 5, Ki = 2.4, Ts = 0.1
Alternatively, you can create the same discrete-time controller by supplying Ts as the fifth argument after all four PID parameters Kp, Ki, Kd, and Tf.
C = pid(5,2.4,0,0,0.1,'IFormula','Trapezoidal');
![]() |
PID Controller with Custom Input and Output Names
Create a PID controller, and set dynamic system properties InputName and OutputName.
C = pid(1,2,3,'InputName','e','OutputName','u');
![]() |
Array of PID Controllers
Create a 2-by-3 grid of PI controllers with proportional gain ranging from 1–2 and integral gain ranging from 5–9.
Create a grid of PI controllers with proportional gain varying row to row and integral gain varying column to column. To do so, start with arrays representing the gains.
Kp = [1 1 1;2 2 2]; Ki = [5:2:9;5:2:9]; pi_array = pid(Kp,Ki,'Ts',0.1,'IFormula','BackwardEuler');
These commands produce a 2-by-3 array of discrete-time pid objects. All pid objects in an array must have the same sample time, discrete integrator formulas, and dynamic system properties (such as InputName and OutputName).
Alternatively, you can use stack to build arrays of pid objects.
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
These commands produce a 1-by-2 array of controllers. Enter the command:
size(pid_array)
to see the result
1x2 array of PID controller. Each PID has 1 output and 1 input.
![]() |
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 times Ti and Td, and filter divisor N. You can convert any standard form controller to parallel form using pid.
stdsys = pidstd(2,3,4,5); % Standard-form controller parsys = pid(stdsys)
These commands produce a parallel-form controller:
Continuous-time PIDF controller in parallel form: 1 s Kp + Ki * --- + Kd * -------- s Tf*s+1 with Kp = 2, Ki = 0.66667, Kd = 8, Tf = 0.8
![]() |
Convert Dynamic System to Parallel-Form PID Controller
Convert a continuous-time dynamic system that represents a PID controller to parallel pid form.
The dynamic system
represents a PID controller. Use pid to obtain H(s) to in terms of the PID gains Kp, Ki, and Kd.
H = zpk([-1,-2],0,3); C = pid(H)
These commands produce the result:
Continuous-time PID controller in parallel form: 1 Kp + Ki * --- + Kd * s s with Kp = 9, Ki = 6, Kd = 3
![]() |
Convert Discrete-Time Zero-Pole-Gain Model to Parallel-Form PID Controller
Convert a discrete-time dynamic system that represents a PID controller with derivative filter to parallel pid form.
% PIDF controller expressed in zpk form sys = zpk([-0.5,-0.6],[1 -0.2],3,'Ts',0.1)
The resulting pid object depends upon the discrete integrator formula you specify for IFormula and DFormula. For example, if you use the default ForwardEuler for both formulas:
C = pid(sys)
returns the result
Discrete-time PIDF controller in parallel form: Ts 1 Kp + Ki * ------ + Kd * ----------- z-1 Tf+Ts/(z-1) with Kp = 2.75, Ki = 60, Kd = 0.020833, Tf = 0.083333, Ts = 0.1
Converting using the Trapezoidal formula returns different parameter values:
C = pid(sys,'IFormula','Trapezoidal','DFormula','Trapezoidal')
This command returns the result:
Discrete-time PIDF controller in parallel form: 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.020833, Tf = 0.033333, Ts = 0.1
For this particular sys, you cannot write sys in parallel PID form using the BackwardEuler formula for DFormula. Doing so would result in Tf < 0, which is not permitted. In that case, pid returns an error.
![]() |
Discretize a Continuous-time PID Controller
First, discretize the controller using the 'zoh' method of c2d.
Cc = pid(1,2,3,4) % continuous-time pidf controller Cd1 = c2d(Cc,0.1,'zoh')
c2d computes new parameters for the discrete-time controller:
Discrete-time PIDF controller in parallel form: Ts 1 Kp + Ki * ------ + Kd * ----------- z-1 Tf+Ts/(z-1) with Kp = 1, Ki = 2, Kd = 3.0377, Tf = 4.0502, Ts = 0.1
The resulting discrete-time controller uses ForwardEuler (Ts/(z–1)) for both IFormula and DFormula.
The discrete integrator formulas of the discretized controller depend upon the c2d discretization method, as described in Tips. To use a different IFormula and DFormula, directly set Ts, IFormula, and DFormula to the desired values:
Cd2 = Cc; Cd2.Ts = 0.1; Cd2.IFormula = 'BackwardEuler'; Cd2.DFormula = 'BackwardEuler';
These commands do not compute new parameter values for the discretized controller. To see this, enter:
Cd2
to obtain the result:
Discrete-time PIDF controller in parallel form: 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
piddata | pidstd | pidtool | pidtune
Was this topic helpful?
|
Learn more about resources for designing, testing, and implementing control systems.
Get free kit© 1984-2011- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |