Accelerating the pace of engineering and science

pidtune

PID tuning algorithm for linear plant model

Syntax

C = pidtune(sys,type)
C = pidtune(sys,C0)
C = pidtune(sys,type,wc)
C = pidtune(sys,C0,wc)
C = pidtune(sys,...,opts)
[C,info] = pidtune(...)

Description

C = pidtune(sys,type) designs a PID controller of type type for the plant sys in the unit feedback loop

pidtune tunes the parameters of the PID controller C to balance performance (response time) and robustness (stability margins).

C = pidtune(sys,C0) designs a controller of the same type and form as the controller C0. If sys and C0 are discrete-time models, C has the same discrete integrator formulas as C0.

C = pidtune(sys,type,wc) and C = pidtune(sys,C0,wc) specify a target value wc for the first 0 dB gain crossover frequency of the open-loop response L = sys*C.

C = pidtune(sys,...,opts) uses additional tuning options, such as the target phase margin. Use pidtuneOptions to specify the option set opts.

[C,info] = pidtune(...) returns the data structure info, which contains information about closed-loop stability, the selected open-loop gain crossover frequency, and the actual phase margin.

Input Arguments

 sys Single-input, single-output dynamic system model of the plant for controller design. sys can be: Any type of SISO dynamic system model, including Numeric LTI models and identified models. If sys is a tunable or uncertain model, pidtune designs a controller for the current or nominal value of sys.A continuous- or discrete-time model.Stable, unstable, or integrating. A plant with unstable poles, however, might not be stabilizable under PID control.A model that includes any type of time delay. A plant with long time delays, however, might not achieve adequate performance under PID control.An array of plant models. If sys is an array, pidtune designs a separate controller for each plant in the array. If the plant has unstable poles, and sys is one of the following: A frd modelA ss model with internal time delays that cannot be converted to I/O delays you must use pidtuneOptions to specify the number of unstable poles in the plant, if any.

type

Controller type (actions) of the controller to design, specified as one of the following strings.

StringTypeContinuous-Time Controller Formula (parallel form)Discrete-Time Controller Formula (parallel form, ForwardEuler integration method)
'p'Proportional onlyKpKp
'i'Integral only

$\frac{{K}_{i}}{s}$

${K}_{i}\frac{{T}_{s}}{z-1}$

'pi'Proportional and integral

${K}_{p}+\frac{{K}_{i}}{s}$

${K}_{p}+{K}_{i}\frac{{T}_{s}}{z-1}$

'pd'Proportional and derivative

${K}_{p}+{K}_{d}s$

${K}_{p}+{K}_{d}\frac{z-1}{{T}_{s}}$

'pdf'Proportional and derivative with first-order filter on derivative term

${K}_{p}+\frac{{K}_{d}s}{{T}_{f}s+1}$

${K}_{p}+{K}_{d}\frac{1}{{T}_{f}+\frac{{T}_{s}}{z-1}}$

'pid'Proportional, integral, and derivative

${K}_{p}+\frac{{K}_{i}}{s}+{K}_{d}s$

${K}_{p}+{K}_{i}\frac{{T}_{s}}{z-1}+{K}_{d}\frac{z-1}{{T}_{s}}$

'pidf'Proportional, integral, and derivative with first-order filter on derivative term

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

${K}_{p}+{K}_{i}\frac{{T}_{s}}{z-1}+{K}_{d}\frac{1}{{T}_{f}+\frac{{T}_{s}}{z-1}}$

When you use the type input, pidtune designs a controller in parallel (pid) form. Use the input C0 instead of type if you want to design a controller in standard (pidstd) form.

If sys is a discrete-time model with sampling time Ts, pidtune designs a discrete-time controller with the same Ts. The controller has the ForwardEuler discrete integrator formula for both integral and derivative actions. Use the input C0 instead of type if you want to design a controller having a different discrete integrator formula.

C0

pid or pidstd controller specifying properties of the designed controller. If you provide C0, pidtune:

• Designs a controller of the type represented by C0.

• Returns a pid controller, if C0 is a pid controller.

• Returns a pidstd controller, if C0 is a pidstd controller.

• Returns a controller with the same Iformula and Dformula values as C0, if sys is a discrete-time system. See the pid and pidstd reference pages for more information about Iformula and Dformula.

wc

Target value for the 0 dB gain crossover frequency of the tuned open-loop response L = sys*C. Specify wc in units of radians/TimeUnit, where TimeUnit is the time unit of sys. The crossover frequency wc roughly sets the control bandwidth. The closed-loop response time is approximately 1/wc.

Increase wc to speed up the response. Decrease wc to improve stability. When you omit wc, pidtune automatically chooses a value, based on the plant dynamics, that achieves a balance between response and stability.

opts

Option set specifying additional tuning options for the pidtune design algorithm, such as target phase margin. Use pidtuneOptions to create opts.

Output Arguments

 C Controller designed for sys. If sys is an array of linear models, pidtune designs a controller for each linear model and returns an array of PID controllers. Controller form: If the second argument to pidtune is type, C is a pid controller. If the second argument to pidtune is C0: C is a pid controller, if C0 is a pid object.C is a pidstd controller, if C0 is a pidstd object. Controller type: If the second argument to pidtune is type, C generally has the specified type. If the second argument to pidtune is C0, C generally has the same type as C0. In either case, however, where the algorithm can achieve adequate performance and robustness using a lower-order controller than specified with type or C0, pidtune returns a C having fewer actions than specified. For example, C can be a PI controller even though type is 'pidf'. Time domain: C has the same time domain as sys.If sys is a discrete-time model, C has the same sampling time as sys. If you specify C0, C has the same Iformula and Dformula as C0. If no C0 is specified, both Iformula and Dformula are Forward Euler. See the pid and pidstd reference pages for more information about Iformula and Dformula. If you specify C0, C also obtains model properties such as InputName and OutputName from C0. For more information about model properties, see the reference pages for each type of dynamic system model. info Data structure containing information about performance and robustness of the tuned PID loop. The fields of info are: Stable — Boolean value indicating closed-loop stability. Stable is 1 if the closed loop is stable, and 0 otherwise.CrossoverFrequency — First 0 dB crossover frequency of the open-loop system C*sys, in rad/TimeUnit, where TimeUnit is the time units specified in the TimeUnit property of sys. PhaseMargin — Phase margin of the tuned PID loop, in degrees. If sys is an array of plant models, info is an array of data structures containing information about each tuned PID loop.

Examples

PID Controller Design at the Command Line

This example shows how to design a PID controller for the plant given by:

As a first pass, create a model of the plant and design a simple PI controller for it.

sys = zpk([],[-1 -1 -1],1);
[C_pi,info] = pidtune(sys,'pi')

C_pi =

1
Kp + Ki * ---
s

with Kp = 1.14, Ki = 0.454

Continuous-time PI controller in parallel form.

info =

Stable: 1
CrossoverFrequency: 0.5205
PhaseMargin: 60.0000



C_pi is a pid controller object that represents a PI controller. The fields of info show that the tuning algorithm chooses an open-loop crossover frequency of about 0.52 rad/s.

Examine the closed-loop step response (reference tracking) of the controlled system.

T_pi = feedback(C_pi*sys, 1);
step(T_pi)


To improve the response time, you can set a higher target crossover frequency than the result that pidtune automatically selects, 0.52. Increase the crossover frequency to 1.0.

[C_pi_fast,info] = pidtune(sys,'pi',1.0)

C_pi_fast =

1
Kp + Ki * ---
s

with Kp = 2.83, Ki = 0.0495

Continuous-time PI controller in parallel form.

info =

Stable: 1
CrossoverFrequency: 1
PhaseMargin: 43.9973



The new controller achieves the higher crossover frequency, but at the cost of a reduced phase margin.

Compare the closed-loop step response with the two controllers.

T_pi_fast = feedback(C_pi_fast*sys,1);
step(T_pi,T_pi_fast)
axis([0 30 0 1.4])
legend('PI','PI,fast')


This reduction in performance results because the PI controller does not have enough degrees of freedom to achieve a good phase margin at a crossover frequency of 1.0 rad/s. Adding a derivative action improves the response.

Design a PIDF controller for Gc with the target crossover frequency of 1.0 rad/s.

[C_pidf_fast,info] = pidtune(sys,'pidf',1.0)

C_pidf_fast =

1            s
Kp + Ki * --- + Kd * --------
s          Tf*s+1

with Kp = 2.72, Ki = 1.03, Kd = 1.76, Tf = 0.00875

Continuous-time PIDF controller in parallel form.

info =

Stable: 1
CrossoverFrequency: 1
PhaseMargin: 60.0000



The fields of info show that the derivative action in the controller allows the tuning algorithm to design a more aggressive controller that achieves the target crossover frequency with a good phase margin.

Compare the closed-loop step response and disturbance rejection for the fast PI and PIDF controllers.

T_pidf_fast =  feedback(C_pidf_fast*sys,1);
step(T_pi_fast, T_pidf_fast);
axis([0 30 0 1.4]);
legend('PI,fast','PIDF,fast');


You can compare the input (load) disturbance rejection of the controlled system with the fast PI and PIDF controllers. To do so, plot the response of the closed-loop transfer function from the plant input to the plant output.

S_pi_fast = feedback(sys,C_pi_fast);
S_pidf_fast = feedback(sys,C_pidf_fast);
step(S_pi_fast,S_pidf_fast);
axis([0 50 0 0.4]);
legend('PI,fast','PIDF,fast');


This plot shows that the PIDF controller also provides faster disturbance rejection.

Tune Standard-Form PID Controller

This example shows how to design a PID controller in standard form for the plant defined by

$sys=\frac{1}{{\left(s+1\right)}^{3}}.$

To design a controller in standard form, use a standard-form controller as the C0 argument to pidtune.

sys = zpk([],[-1 -1 -1],1);
C0 = pidstd(1,1,1);
C = pidtune(sys,C0)
C =

1      1
Kp * (1 + ---- * --- + Td * s)
Ti     s

with Kp = 2.18, Ti = 2.36, Td = 0.591

Continuous-time PID controller in standard form


Specify Integrator Discretization Method

This example shows how to design a discrete-time PI controller using a specified method to discretize the integrator.

If your plant is in discrete time, pidtune automatically returns a discrete-time controller using the default Forward Euler integration method. To specify a different integration method, use pid or pidstd to create a discrete-time controller having the desired integration method.

sys = c2d(tf([1 1],[1 5 6]),0.1);
C0 = pid(1,1,'Ts',0.1,'IFormula','BackwardEuler');
C = pidtune(sys,C0)
C =

Ts*z
Kp + Ki * ------
z-1

with Kp = -0.518, Ki = 10.4, Ts = 0.1

Sample time: 0.1 seconds
Discrete-time PI controller in parallel form.


Using C0 as an input causes pidtune to design a controller C of the same form, type, and discretization method as C0. The display shows that the integral term of C uses the Backward Euler integration method.

Specify a Trapezoidal integrator and compare the resulting controller.

C0_tr = pid(1,1,'Ts',0.1,'IFormula','Trapezoidal');
Ctr = pidtune(sys,C_tr)
Ctr =

Ts*(z+1)
Ki * --------
2*(z-1)

with Ki = 10.4, Ts = 0.1

Sample time: 0.1 seconds
Discrete-time I-only controller.

Alternatives

For interactive PID tuning, use the PID Tuner GUI (pidTuner). See PID Controller Design for Fast Reference Tracking for an example of designing a controller using the PID Tuner GUI.

The PID Tuner GUI cannot design controllers for multiple plants at once.

expand all

Tips

By default, pidtune with the type input returns a pid controller in parallel form. To design a controller in standard form, use a pidstd controller as input argument C0. For more information about parallel and standard controller forms, see the pid and pidstd reference pages.

Algorithms

Typical PID tuning objectives include:

• Closed-loop stability — The closed-loop system output remains bounded for bounded input.

• Adequate performance — The closed-loop system tracks reference changes and suppresses disturbances as rapidly as possible. The larger the loop bandwidth (the first frequency at which the open-loop gain is unity), the faster the controller responds to changes in the reference or disturbances in the loop.

• Adequate robustness — The loop design has enough phase margin and gain margin to allow for modeling errors or variations in system dynamics.

The MathWorks algorithm for tuning PID controllers helps you meet these objectives by automatically tuning the PID gains to balance performance (response time) and robustness (stability margins).

By default, the algorithm chooses a crossover frequency (loop bandwidth) based upon the plant dynamics, and designs for a target phase margin of 60°. If you specify the crossover frequency using wc or the phase margin using pidtuneOptions, the algorithm computes PID gains that best meet those targets.

References

Åström, K. J. and Hägglund, T. Advanced PID Control, Research Triangle Park, NC: Instrumentation, Systems, and Automation Society, 2006.