You are on page 1of 4

Developing MATLAB functions (Plant, Controller and Closed Loop)

a. Launch MATLAB software.


b. From the Home tab, select New -> Function.
c. Write down the generic plant function as shown in the following snippet:

function [ Wp ] = CreatePlant( num,den )


%CreatePlant Creates plant transfer function.
% The returned value is the system in numerator/denomerator format
%% Parameters
% num : Numerator vector (starting from highest order of coefficients)
% den : Denomerator vector (starting from highest order of
coefficients)
% plant : Plant transfer function
%% EXAMPLE
% num=[1];
% den=[1 0 1];
% sys=CreatePlant(num,den)
%% Result is
% 1
% sys= ---------------
% S^2+1
%% Function implementation
syms s;
Wp=tf(num,den);
End

Repeat steps b-e for creating the following snippet for Ziegler-Nicholas generic
function:
function Wc = ZieglerNicholasPID( Kc,Ti,Td )
% ZieglerNicholasPID function to generate the PID controller transfer
%% Parameters
% Kc : Critical gain
% Ti : Reset time (minutes)
% Td : Derivative time (minutes)
%% Function implementation
s=tf('s');
Wc=Kc*(1+(1/(Ti*s))+Td*s);
End
g. The final function bonds the two functions (plant and controller) to build the
closed loop system:

function sys = CLS( Wp,Wc )


%CLS Closed loop system function
%% Parameters
% Wp : Plant transfer function
% Wc : Controller transfer function
% sys : Closed Loop transfer function with assuming unity feedback.
%% Function implementation
CLS=feedback(series(Wp,Wc),1);
End

To plot the open loop response, perform the following steps:


a. From MATLAB command window, we will call the function CreatePlant to
create the transfer function mentioned in shown:

sys=CreatePlant(1,[1000 300 30 1]);


step(sys)
Finding the critical gain (Kc) via Nyquist plot
a. To plot the Nyquist of frequency response of the plant, write down the
following code:

Wp=CreatePlant(1,[1000 300 30 1]);


nyquist(Wp);
b. Right click on the plot and select characteristics -> Minimum Stability
Margins as shown in figure
Write down the gain margin Gm (in dB) and convert it to magnitude. Write down
the margin frequency Wc .

d. Calculate = = 8.0011 , and = 2 = 2 0.173 = 36.32 and


consequently

Calculating P, PI and PID control gains


After obtaining the critical gain from the previous step, we are able to calculate
the P,I and D parameters and perform comparison of each controller type.
According to Ziegler Nicholas table:
Kp Ti (sec) Td (sec)
Controller Type
P 0.5*Kc = 0.5*8=4 100000 0
PI 0.45*Kc = 0.83*Pu=0.83*36.3 0
0.45*8=3.6 2=30.1
PID 0.59* Kc = 0.5*Pu=0.5*36.32 0.12*Pu
0.59*8=4.7 =18.2 =0.12*36.32=4.4

Kc tI tD
P control Ku/2
PI control Ku/2.2 Pu/1.2
PID control Ku/1.7 Pu/2 Pu/8