You are on page 1of 10

Optimal setting for discrete PID controllers

K. Minh Vu

Indexing terms: Control theory, Process control

Abstract: An approach to obtain the optimal


gains for a discrete PID controller is presented.
The optimal gains are obtained by minimising the
variance of the output variable under feedback.
The problem is formulated similarly to the
Kalman filter problem and the gains are obtained
from the solution of a Riccati equation. The discussion is restricted to single input single output
systems.
1

Introduction

The PID or the three-mode controller is probably the


most widely used controller in the process industry. It
gained its popularity for its simplicity of having only
three parameters. But owing to its simplicity it has also
paid the price of not having an efficient and practical way
of determining optimal gains. From the time of its
infancy, control engineers have worked on methods to
obtain optimal gains. The work has been on both types
of controllers: analogue, and discrete or digital. But, in
practice, control engineers have sometimes used these
indistinguishably. Early work can be credited to Ziegler
and Nichols [l], who obtained the gains from the closedloop ultimate period, and Cohen and Coon [2] who
introduced the method of open-loop process reaction
curue to approximate the control loop by a delayed first
order system and obtained the optimal gains for
minimum offset, one quarter decay ratio and minimum
integral square error response to a load change. These
methods have been taught in undergraduate control
courses as ways to tune analogue controllers. More
advanced work on analogue PID controllers used eigenvalue assignment to place the closed-loop poles for stability. Recent work on analogue PID controllers has been
reported by Zervos et al. [3], who introduced the concept
of orthonomal series to represent a system, then tuned
the controller gains. A discrete version of the eigenvalue
assignment method was studied by StojiC and PetroviC
[4], who used a grapho-analytical pole-placement procedure to obtain the gains. By using the Box-Jenkins
model to represent a system, MacGregor et al. [SI suggested a method of obtaining the optimal gains by plotting contours of variances under feedback. The optimal
gains are obtained by extrapolation from the contours of
variances. Another approach was suggested by Meo et al.
[6] who used projective control to obtain the gains. The
adaptive or self-tuning PID controller was discussed by
Radke and Isermann [7] who used 2-stage least squares
to estimate the parameters of a system and a numerical
Paper 8457D (C8), first received 23rd March 1990 and in revised form
13th August 1991
The author is with Measurex Devron Inc., 500 Brooksbank Avenue,
North Vancouver, BC, Canada V7J 3S4
IEE PROCEEDINGS-D, Vol. 139, N O . I , J A N U A R Y 1992

optimisation procedure to estimate the controller gains.


Astrom and Hagglund [8] discussed and also compared
a number of methods in their book. So far, all the
methods mentioned suffer from either impracticality or
time consumption or both. In this paper, a method to
obtain the optimal gains for a discrete PID controller is
presented. The controller gains are obtained by minimising the variance of the output variable under feedback.

Optimal stochastic PID controller

Consider the following stochastic control system


o(z-1)

e(2-l)

Yr+r+i -- 6 ( z - 1 ) ur + 7
ar+f+l

&- 1

(1)

with a, as the generating white noise. The system is


required to have minimum variance under the feedback
PID control law
~t=kp~l+kiCyi+kd(yr-~t-1)

where k p is the proportional gain, ki is the integral gain


and kd is the derivative gain. By taking the difference on
both sides of the above equation, one obtains the velocity
form of the PID controller
ur-ut-1

=kp(Yr-yr-l)+kiYr
+ k A Y r - ~ r - l

= (kp

p Y r - 1

+Y,-z)

+ ki + k d ) y t

+(-kp

- 2kd)Yt-l

+ kdyt-Z

or
(1 - Z - ' ) u , = ( k p

+ +
ki

kd)y,

+(-kp-2kd)yt-l

= l1Yr

+l

+ kdyr-Z

~ ~ t - 1I 3 Y r - z

= l(z - 1 ) y ,

With the above form of the controller, under feedback,


eqn. 1 can be written as
Y,+f+l

--o ( 2 - I )
- qZ-i)

I &-')
$(z-i)

(z - 1) I(z - 1 ) z - f --o&-')
(1 - z - l )
-

ar+f+l

e(z - 1)

Yt+f+'

+& - l ) %+f+l

6(z- ')O(Z- ')(1 - z - I )


- ' ) z - f - 'I(z - 1)
6(z - ')# - 1 x 1 -z - ) -o ( z x a,+/+,
31

or

If one rewrites the above equation as


Y, =

0,"
=

a(z - ')
B(Z-1) - y(z-l)z-f-ll(z-l)

cT[(A - HGk[l

O])V(A- HGk[l

01)'

+ [q - p - HGk][q - p - HGklTa,']c + c,'

where

=cT AVAT-

+ HGk[l
- HGk[[l

and defines the following matrices and vectors :

AV

[:I

+ ( q - p ) a : kTGTHT

O]V[:]k'cTHT
01 VAT + (4 - P)~cT,']

+ [q - p ] [ q - plTa: + HGka2kTGTHT c + U,'


= CT

A V A T + [q - p ] [ q - p y a :

with I defined as before:

=[-a x -;][;I
1

k,

= Gk

then y, can be put into the state space model form as


below
x,+1 = A*x,

+ bat

+ a,

y, = CTX,

The right-hand side of the above equation consists of


four terms in the square brackets. The first three of these
are independent of k , but the last one, which is positive,
depends on k. The quantity on the left hand side will
obtain its minimum value when

with

A] +

A v[

zn01] - H G k [ l

A* = [ q

0 ... 0)

HGk -

(4 - P)d

=o

+ c1 01v[:]

0,'

= A

HGk[l

[q - p

Therefore the optimal gains of a PID controller are given


by

HGk]

0 ... 01 = [l

c T = [l

01
01

By taking the variance on both sides of eqn. 2

v = E{x,xT}
= A*

VA*T + bbTo,'

.,"

The variance of the output variable is given by


= E{Y, YT)
= CTVC

+ 0:

and therefore
(7,"

32

= CT[A*VA*T

+ bb=a:]c + 6,'

(3)

with Vas the solution of eqn. 3.


The solution of eqn. 3 for V can easily be established
by an iterative approach. The approach is described
below.
(i) Set up the initial condition

vo = c4

PIC4 - P I T d

IEE PROCEEDINGS-D, Vol. 139, No. I , J A N U A R Y 1992

and from this equation calculate the initial gain vector

+ (q -p)o,']

C-'[HTH]'HTpV,,[i]

k,

Ramp change to a new level


a,

YSP, = (1 - z-1)2

r.1

Exponential change to a new level


(ii) Use eqn. 3 to calculate the variance matrix V,, for
the next iteration as below

YSPr = (1 - z -

v,+,= A*V,A*T + bbTo,'


(5)
(iii) Use eqn. 4 to calculate the gain vector k, as below

"[Av,[i] +

G- 1 [ H T - ~

k, =
0,'

+ c1

a,
1 - bz-')

Examples

In this Section two examples are given to illustrate the


presented theory. First consider a stochastic control
system described by the following equation

(4- p).:]
(6)

;]

'

01vt[

y,

(iv) Iterate between eqns. 5 and 6 until convergence, i.e.

0.168
1 - 0.9082-

+ (1 - z-')(1

Vr+, = Vr

=v

1
- 0.32-' - 0 . 1 7 ~ - ~")

with a, as the generating white noise having variance


U,' = 2.37. This system gives

(v) Use the converged value of the variance matrix Y


to calculate the gain vector k.

4 z - l ) = 0.168
6(z-') = 1 - 0 . 9 0 8 ~ ~ '

Such an approach is similar to that used to calculate the


steady state Kalman filter, and eqn. 3 is a Riccati equation.

e(z-1) = 1

$(z-') = 1 - 1.3z-l
3

Optimal deterministic PID controller

f=O

In control there are two problems. In the servo problem,


the load is constant but the set point undergoes change
while in the regulator problem the set point is kept constant and the load undergoes change. Stochastic control
models the load as a time series and therefore belongs to
the regulator class. However, as there is a duality
between stochastic load changes and randomly occurring
deterministic set point changes [9], the theory presented
in Section 2 can also be applied to servo problems. Consider the deterministic system
Yr+f+l

42--

+ 0.908~-'
+ 3.5184~-' - 1.25844zY3
- 0.20632~-~
+ O.15436zY5

.(z-') = 1 - 1.9082-'

@ ( z - ' ) = 1 - 3.2082-'

~ ( z - '=
) 0.168 - 0.21842-'

+ 0.02184~-~+ 0.02856~-~
Therefore

U,

with a set point change y ~ p , + at


~ +time
~ t
above equation can be written as
Yt+f+l

and

'1

6(z-')

+ 0.13~-' + O.17zY3

+f + 1. The

3.208
- 3.5 184

P=

1.25844
0.20632
- 0.15436
- 0

0
0
0

- Y S P ~ + / +=~~ : + f + l
-4z-l)
- 6(z- 1) U, - Y S P t + f + 1
-

-0.168
0.2184
-0.02184
H=
-0.02856
0
- 0

If one can express the set point changes Y S P , + ~ + ' in the z


domain, then the above equation is equivalent to eqn. 1
and y, can be controlled to follow ysp, with minimum
variance. A few common set point changes are described
below.

Step change to a new level


YSPr =

a,

sYm
IEE PROCEEDINGS-D, Vol. 139, NO. I , J A N U A R Y 1992

0
0
-0.168
0.2184
-0.02184
-0.02856

The variance matrix V was found to be

0.0588

V=

0
-0.168
0.2184
-0.02184
- 0.02856
0

-0.0539
0.0519

-0.0022
0.0017
0.0006

-0.0016
0.0006
0.0002
0.0005

-0.0008
-0.0000
-0.0001
0.0002
0.0005

-0.0002
-0.0002
-0.ooO1
-0.oooO
0.0002

O.OOO4

33

[;I=[

In the next example, consider the system

With the optimal gains

I;;]

Yt =

with

and the feedback output variance 0,' = 2.4288.


To check the validity of the result, in the following a
few examples with known theoretical controller gains are
considered. First consider the system
0.168
yt = 1 - 0.9082-1
with

0
:

= 0.168

6(z-')

6(z- ')

o(z-1) =

This system gives

1 - 0.821 - 0.22-'

This system is known to have zero integral gain because


the disturbance is stationary in nature. Using the
approach presented in this paper, the following is
obtained

e(z-1) = 1 - 0.22-'

+
/3(z-') = 1 - 2.12-' + 1.34zY2 - 0 . 2 4 ~ - ~
y(z- ') = 0.25 - 0.0752- '
a(z-') = 1 - 2 . 0 ~ ~ '1.16z-' - 0 . 1 6 ~ - ~

4(z-') = 1 - 2 - 1
f=O
The minimum variance controller for this system
obtained by the method of Box and Jenkins [lo] is
= -

at

f=O

1 - 0.908~-'

(1 - . - ' ) U t

+ 1 - 0.32-'

+ ( z - ' ) = 1 - 0.32-'

= 2.0. This system gives

O(Z-')

= 2.0.

1 - 0.22-'

Ut-1

o ( z - ') = 0.250

1 - 0.22-'
+ 1 - z - l at

't-1

0:

0.250
1 - o.*z-'

(1 - 0.2)(1 - 0.9082- ')


0.168

Therefore
2.000

2.100

Yt

0.8
c v t - 0.908Yt- 1)
0.168

= --

-0.250
0.075

which is a PI cpntroller with the gains k , = -4.3238 and


k, = -0.4381. Using the method presented in this paper,
it is obtained as
a(z-') = 1 - 2.108z-'
P(2-l)

= 1 - 2.9082-'

~ ( 2 - l )= 0.168

+ 1 . 2 8 9 6 ~ --~ 0 . 1 8 1 6 ~ - ~
+2 . 8 1 6 ~ ~0.908zY3
~

The variance matrix V was found to be

- 0.168z-'

0.1420

Therefore
2.108

0
-0.250

v=[

2.908

sym.

-0.1375
0.0219 0
0.1343 -0.0213 0
0.0036 0
0.0002

with the optimal gains


- 0.5568

-0.168
0.168

0
-0.168
0.168
0

0
-0.1156

-0.168
1.161

The variance matrix Y was found to be

Y x 1.OE10

0.0500

- 0.0956

0.1228

0.0456
-0.0871
0.04 15

sym.
with the gains and feedback output variance as

which are identical to the solutions obtained previously.


34

and the feedback output variance 0: = 2.1420.


The results of the examples presented in this Section
were obtained from a computer program written in C
(see Appendix 7). The program converged for many cases
under testing but is known to have difficulty in cases with
a delay in the system or with a nonminimum phase.
These cases are under investigation and the results will be
submitted at a later date.
5

Conclusion

In this paper, a method of obtaining the optimal gains


for a discrete PID controller was presented. The method
can be used for both stochastic or deterministic systems.
A similar approach can also be derived for multivariable
systems. The method can be used not only for PID controllers but also for any type of controller with known
structure or orders.
I E E PROCEEDINGS-D, Vol. 139, N O . I , JANUARY 1992

References

6 MEO, J.A.C., MEDANIC, J.V., and PERKINS, W.R.: Design of

1 ZIEGLER, J.G., and NICHOLS, N.B.: Optimum setting for PID


controllers, Trans. ASME, 1942,64, pp. 759-768
2 COHEN, G.H., and COON, G.A.: Theoretical considerations of
retarded control, Trans. ASME, 1953,75, p. 827
3 ZERVOS, C., BfiLANGER, P.R.,and DUMONT, G.A.: On PID
controller tuning using orthonomd Eries identification, ~
~
matica, 1988,24, (2), pp. 165-175
4 STOJIC, M.R., and PETROVIC, T.B.: Design of digital PID
stand-alone single loop controller, Int. J . Control, 1986, 43, (4), pp.
1229-1242

5 MAcGREGOR, J.F., WRIGHT, J.D., and HUYNH, M.H.:

Optimal tuning of digital PID controllers using dynamic-stochastic


models, IEC Process Des. Dev., 1975,14, pp. 398-402

digital PI + dynamic controllers using projective control, Int. J.


Control, 1986,43, (2), pp. 539-559
7 RADKE, F., and ISERMANN, R.: A parameter-adaptive PID controller with stepwise parameter optimization, Automatica, 1987, U,
pp. 449-451
K.J., and HAGGLUND, T.: Automatic tuning of PID
t 8 ASTROM,
~
controllers (ISA, Research Triangle Park, N.C., 1988)
9 MAcGREGOR, J.F., HARRIS, T.J., and WRIGHT, J.D.: Duality
between the control of processes subject to randomly occurring
deterministic disturbances and ARIMA stochastic disturbances,
Technometrics, 1984,26, (4), pp. 389-397
10 BOX, G.E.P., and IENKINS, G.M.: Time series analysis, forecasting and control (Holden Day, San Francisco, 1970)

Appendix

.........................................
Module:

main( )

Description:

This module reads in the system parameters and set up the right system vectors and matrices, then it
solves the Riccati equation and calculate the PID controller gains.

Author:

K. M. Vu

9-May-91

.........................................
#include
#include
#include
#include
#include

(c:\tc\stdio.h)
(c:\tc\stdlib.h)
(c:\tc\math.h)
(c:\tc\malloc.h)
(c:\tc\nrutil.c)

#define NDIMS 20
int
i, j, 1;
int
ndelay ;
int
nomega;
int
ndelta;
int
nphi;
int
ntheta;
int
n;
int
inter;
int
mvflag;
int
maxiter = 500;
float omegamDIMS] ;
float delta[NDIMS] ;
float phi[NDIMS] ;
float theta[NDIMS];
float var;
float alpha[NDIMS] ;
float betamDIMS];
float gamma[NDIMS];
float ppD1MS-J;
float q[NDIMS];
float h[NDIMS][3];
float k[3];
float ht[3][NDIMS];
float hth[3][3];
float hthinv[3][3];
float hthinvht[3][NDIMS] ;
float ginvhthinvnt[3][NDIMS];
float hg[NDIMS][3];
float hgk[NDIMS];
float a[NDIMS][NDIMS];
float astar[NDIMS][NDIMS];
float astart[NDIMS][NDIMS];
float astarvmDIMS][NDIMS];
float vt[NDIMS][NDIMS];
float vtl[NDIMS][NDIMS];

/* Do loop counters */
/* Pure delay in the system */
/* Number of zeros of the transfer function */
/* Number of poles of the transfer function */
/* The autoregressive order */
/* The moving average order */
/* The resulting order of the time series e/
/* The iteration number */
/* The minimum variance controller flag */
/* The maximum iteration number allowable */
/* The polynomial of the zeros */
/* The polynomial of the poles */
/* The autoregressive polynomial */
/* The moving average polynomial */
/* The variance of the white noise */
/* The resulting time series polynomial */
/* The resulting time series polynomial */
/* The resulting time series polynomial */
/*The p vector */
/* The q vector */
/* The h matrix */
/* The controller gain vector */
/* Working matrix */
/* Working matrix */
/* Working matrix */
/* Working matrix */
/* Working matrix */
/* Working matrix */
/* Working matrix */
/* The Riccati equation matrix */
/* The Riccati equation matrix */
/* The Riccati equation matrix */
/* The Riccati equation matrix */
/* The Riccati equation matrix */
/* The Riccati equation matrix */

IEE PROCEEDINGS-D, Vol. 139, NO. I , JANUARY 1992

35

/* The Riccati equation matrix */


float av[NDIMS]~DIMS];
/* The Riccati equation matrix */
float avl [NDIMS] ;
/* The Riccati equation matrix */
float b[NDIMS] ;
/* The Riccati equation matrix */
float bbt[NDIMS][NDIMS] ;
/* The current variance matrix norm */
float vt-sq;
/* The next variance matrix norm */
float vtl-sq;
/* The b vector norm */
float b-sq;
/* The Yo change in variance matrix norm */
float pc-change ;
/* The iteration stopping criterion */
float accuracy = 1. M 6 ;
/* The difference polynomial */
float dim21 = { 1,- l} ;
float g[3][3]={{l,l,l}, { -1,0,-2}, {O,O,l}}; /* A constant matrix */
float ginv[3][3]={{0,-1,-2},
{l,l,l}, {O,O,l}}; /* A constant matrix */
float sum;
/* The running sum parameter */
main( )
{
/* Read in system parameters */
printf("Enter the number of zeroes of the transfer function : ");
scanf("%d", &nomega);
for (i=O; ienomega; i + +)
{
printf("Enter the omega's : ");
scanf("%f", &omega[i]);

printf("Enter the number of poles of the transfer function : ");


scanf("%d", &ndelta);
for (i=O; iendelta; i + +)
{
printf("Enter the delta's : ");
scanf("%f", &delta[i]);

printf("Enter the delay period not counting the discretization one : ");
scanf("%d", &delay);
printf("Enter the autoregressive order of the disturbance : ");
scanf("%d", &nphi);
for (i=O; i c n p h i ; i + +)

printf("Enter the phi's : ");


scanf("% f", &phi [i]) ;

printf("Enter the moving average order of the disturbance : ");


scanf("%d", &ntheta);
for (i=O; ientheta; i + +)

printf("Enter the theta's : ");


scanf ("%f", &theta[i]);

printf("Enter the variance of the white noise disturbance : ");


scanf("%f", &var);
/* Form the necessary vectors and parameter matrix */
i = (int) max(nde1ta+ nphi 1,nomega nphi + ndelay 3);
n =(int) max(i,ndelta + ntheta 1);
/* Form vector p */
for (i = 0; iendelta + ntheta; i +)

gamma[i] = 0;
for ( j =O; j e i ; j + +) gamma[i]

for (i =O;

+
+

+ = delta[j]*theta[i-j];
iendelta + ntheta + 1; i + +)

alpha[i] =0;
for (j =O; j e l ; j + +) alpha[i]

+ = gamma[i-j]*difflj];

for (i=O; i < n ; i + +)

36

gamma[i] = 0;
if (iendelta + ntheta + 1) p[i] = - alpha[i

+ 11; else p[i] =0;


I E E PROCEEDINGS-D, Vol. 139, N O . 1, J A N U A R Y 1992

/* Form vector q */
for (i=O; irndelta+nphi; i t +)
{
gamma[i] = 0;
for (j =O; j e i ; j + +) gammaci]

I
for(i=O;iendelta+nphi+l;i+
1

beta[i] = 0;
for (j=O; j e l ; j + +) betari]

}
for (i=O; i < n ; i + +)
{
gamma[i] =O;
if (iendelta + nphi + 1) q[i]

+ = delta[j]*phi[i-j];
+)

+ = gamma[i-j]*dim];

- - beta[i

+ I]; else q[i] = 0;

/* Form matrix h */
for (i=O; ienomega+nphi; i + +)
fI

gamma[i] = 0;
for (j =O; j e i ; j + +) gamma[i]

+ = omega[j]*phi[i-j];

for ( i = O ; i < n ; i + +)
{
h[i][O] =O;
if (i > = ndelay&&i < = n - ndelay - 2) h[i] CO] = - gamma[i - ndelay] ;

for (i=O; i t n ; i t +)
{
for (j = 1; j < 3 ; j + +)

h[i]u] =O;
if (i >0) h[i][j]

= h[i - 1][li - 13;

/*

printf("\n Print out system parameters\n") ;


for (i=O; i < n ; i + +)
{
printf(" %8Sf %8Sf ",p[i],q[i]);
for (j =O; j < 3; j + +) printf(" %8Sf ",h[i]fi]);
printf("\n") ;

*/

/* Form matrix A */
for (i=O; i < n ; i + +)
{
for ( j =O; j t n ; j +)

a[i]Q] = 0;
i f ( j = =0) a[i][j]=q[i];
i f ( j = =i+l)a[i][j]=l;
if (j ! = 0) astar[i][j] = aril[ j] ;

for (i=O; i < 3 ; i + +)


fI

for (j=O;j<n;j+ +) ht[i][j]=h[j][i];

for (i=O;i<3;i+ +)
{
foro =O;j < 3;j +)
r

hth[i][j] = O ;
for (l=O;l<n;l+ +) hth[i][j]+
hthinv[i][j] = hth[i][j];

=ht[i][l]*h[l][j];

IEE PROCEEDINGS-D, Vol. 139, N O . 1, JANUARY 1992

31

/*Calculate the inverse of hth */


for(1=0;1<3;1+ +)
{
/* Overwrite the lower half of the original matrix with its Cholesky triangle */
for(j=O;j< = I - l ; j + +)
hthinv[l][l] - = hthinv[l]Cj]*hthinv[l][j];
hthinvcl] [l] = (float)sqrt(hthinv[l] [1]) ;
for (i=l+ 1; i < 3 ; i + +)
{
for (j =O; j < =1- 1;j +)
hthinv[i][l] - = hthinv[i][j]*hthinv[l][j];
hthinv[i][l] = hthinv[i]@]/hthinv[l] [l] ;

I
I/* Now compute the inverse of the Cholesky triangle */

for (1=0; k 3 ; 1+ +)

for (j=O; j < =l; j + +)

if (j = =1)
hthinv[l][j] = l,O/hthinv[l][l] ;
else

sum=0;
for (i=j; i < l ; i + +)sum- =hthinv[l][i]*hthinv[i]Cj];
hthinv[l] [j] = sum/hthinv[l] [l] ;

/* Compute the inverse of the original matrix */


for (1=0; 1<3; 1+ +)

for (j =O; j < = l ; j + +)

sum =0;
for (i=l; i < 3 ; i + + ) s u m + =hthinv[i][l]*hthinv[i][j];
hthinv[l][j] =sum;
hthinv[j][l] = hthinv[l][j];

/* Calculate the matrices for the Riccati equation */


for (i=O;i<3;i+ +)
{
for (j=O;j<n;j+ +)
{
hthinvht[i][j] =O;
for (1 =0;1< 3;1+ +) hthinvht[i][j]
= hthinv[i][l]*ht[l][j];
}

for (i=O;i<3;i+ +)

for(j=O;j<n;j+ +)

qinvhthinvht[i][j] =0;
for (1=0;1<3;1+ +) ginvhthinvht[i][j]

+ =ginv[i][l]*hthinvht[l][j];

for (i=O;i<n;i+ +)

for(j=O;j<3;j+ +)
{
M i l [Jl= 0 ;
for (1=0;1<3;1+ +) hg[i][j]+

/* Set up initial condition


for(j=O;j<3;j+ +)
38

=h[i][l]*g[l][j];

1
*/
IEE PROCEEDINGS-D, Vol. 139, N O . 1, J A N U A R Y 1992

k[j] =O;
for (I=O;l<n;l+ +) k[j]

+ =ginvhthinvht[j][l]*(q[l]

for (i=O;i<n;i+ +)
{
hgk[i] = 0;
for (1=0;1<3;1+ +) hgk[i]

-p[I]);

+ =hg[i][I]*k[I];

for (j =O;j < n ; j + +) b[j] =q[j] -p[j] -hgk[jl;


vt-sq = 0;
for(j=O;j<n;j+ +)
{
for (l=O;l<n;l+ +)
{
vt[j][l] = b[j]*b[l]*var;
vt-sq + = vtrj][l]*vtrj][l);

mvflag = 0;

/* And start the iteration */


iter=l;
pc-change = 0.1 ;
while (pc-change > accuracy&&iter < maxiter&& !mvflag)
{

/* Calculate the controller gain vector */


for (i=O;i<n;i+ +)
{
for(j=O;j<n;j+ +)
{
av[i][j] =O;
for (I=O;l<n;l+ +) av[i][j]+ =a[i][l]*vt[l][j];

for (j =O;j <n;J


for (j =O;j < 3;j

+ +) avl[j] =av[j][O]
+ +)

+(q[j] -p[j])*var;

k[j] =O;
for (1=0;l<n;l+ +) k[j]
k[j]/=var+vt[O][O];

+ =ginvhthinvht[j][l]*avl[ll;

)* Calculate the Riccati equation */


for (i=O;i<n;i+ +)
(

hgk[i] =O;
for (1=0;1<3;1+ +) hgk[i]+ =hg[i][l]*k[l];

b-sq =0 ;
for(j=O;j<n;j+ +)
{
astar[j][Ol =q[J] - hgkCj1;
b[jl= qCjl - PCJI- hgkCj1;
b-sq
= b[j]*b[j];

if(b-sq < = 1.Oe -06&&vt-sq


for (j=O;j<n;j+ +)

-= = 1.Oe -06) mvflag = 1;

for (l=O;l<n;l+ +)

bbtfi][l] = b[j]*b[I]*var;
astart[j][l] =astar[l][j];

/*Recalculate the variance matrix */


for (i=O;i<n;i+ +)
{
for (j =O;j < n ; j + +)
IEE PROCEEDINGS-D, Vol. 139, NO. I , JANUARY 1992

39

astarv[i][j] =O;
for (1=0;l<n;l+ +) astarv[i][j]

+ =astar[i][l]*vt[l][j];

vtl-sq =o;
for (i=O;i<n;i+ +)
{
for (j =O;j < n ; j +)

vtl[i][j] = bbt[i][j];
for (1=0;l<n;l+ +) vtl[i][j] + =astarv[i][l]*astart[l][j];
vt 1-sq + = vt 1[i]fi]*vt 1[i] E] ;

/* And percent of accuracy */


pc-change

I*

=fabs(vt 1-sq

vt-sq) / vt-sq;

printf(1teration = %d kp = %8.4f ki = %8.4f kd = %8.4f Output variance = %8.4p\

*/

for (i=O;i<n;i+ +)
{
for (j =O;j <n;i +) printf(Y08.4f,vt[i]b]);
printf(\n) ;

printf(\n) ;
/* Update iteration parameters */
iter+ = 1;
vt-sq = vt 1-sq;
for(j=O;j<n;j+ +)

for (1=0;l<n;l+ +) vt[j][l]=vtl[j][l];

1
I

/* Print out results */


if (fabs(pc-change) < accuracy)
{

printf(\nConvergence at iteration %d\n,iter);


printf(\nThe variance matrix is:\n);
for (i=O;i<n;i+ +)

f o r ( j = O ; j < n ; j + +)printf(%ll.4e, vt[i][j]);


printf(\n) ;

printf(\nThe controller gains are :\n);


printf(The proportional gain : %8.4f\n,k[O]);
printf(The integral gain : %8.4f\n,k[ I]);
printf(The derivative gain : %8.4f\n,k[2]);
printf(\nThe output variance is : Y08.4f\n,var+ vt[O][O]);

else if(mvflag)

printf(\nThe PID is the minimum variance controller\n);


printf(\nThe controller gains are :\n);
printf(The proportional gain : %8.4f\n1,k[0]);
printf(The integral gain %8.4f\n,k[ I]);
printf(The derivative gain : Y08.4f\n,k[2]);
printf(\nThe output variance is : Y08.4f\n,var);

else
{
printf(\nFail to converge after %d iterations\n,iter);

IEE PROCEEDINGS-D, Vol. 139, N O . 1, J A N U A R Y 1992

You might also like