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 dis- cussion 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 closed- loop 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 eigen- value assignment to place the closed-loop poles for stabil- ity. 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 pro- cedure to obtain the gains. By using the Box-Jenkins model to represent a system, MacGregor et al. [SI sug- gested a method of obtaining the optimal gains by plot- ting 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, NO. I, JANUARY 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 mini- mising the variance of the output variable under feed- back. 2 Optimal stochastic PID controller Consider the following stochastic control system (1) o(z-1) e(2-l) &- 1 -- 6(z-1) ur + 7 ar+f+l Yr+r+i - 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 kp 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 +kAYr-~r-l pYr-1 +Y,-z) = (kp + ki + kd)yt + (-kp - 2kd)Yt-l + kdyt-Z or (1 - Z-')u, = (kp + ki + kd)y, +(-kp-2kd)yt-l + kdyr-Z = l1Yr + l ~ ~ t - 1 + I3Yr-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) I &-') - qZ-i) $(z-i) ar+f+l o(z - 1) I(z - 1)z-f- e(z - 1) -- &-') (1 - z-l) Yt+f+' + - &-l) %+f+l - - 6(z- ')O(Z- ')(1 - z - I) - 6(z - ')# - 1x1 - z - ) - o(z - - ')z - f - 'I(z - 1) x a,+/+, 31

00119089.pdf

Embed Size (px)

Citation preview

Page 1: 00119089.pdf

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 dis- cussion 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 closed- loop 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 eigen- value assignment to place the closed-loop poles for stabil- ity. 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 pro- cedure to obtain the gains. By using the Box-Jenkins model to represent a system, MacGregor et al. [SI sug- gested a method of obtaining the optimal gains by plot- ting 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, NO. 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 mini- mising the variance of the output variable under feed- back.

2 Optimal stochastic PID controller

Consider the following stochastic control system

(1) o(z-1) e ( 2 - l )

&- 1 --

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

with a, as the generating white noise. The system is required to have minimum variance under the feedback PID control law

~ t = k p ~ l + k i C y i + k d ( y r - ~ 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 = k p ( Y r - y r - l ) + k i Y r

+ k A Y r - ~ r - l p Y r - 1 + Y , - z )

= ( k p + ki + k d ) y t

+ ( - k p - 2 k d ) Y t - l + k d y t - Z

or

(1 - Z - ' ) u , = ( k p + ki + k d ) y ,

+ ( - k p - 2 k d ) y t - l + k d y r - Z

= l 1 Y r + l ~ ~ t - 1 + I 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) I &-') - q Z - i ) $(z-i) ar+f+l

o(z - 1) I(z - 1)z- f - e(z - 1) -- &-') (1 - z - l ) Y t + f + ' + - & - l ) % + f + l -

- 6(z- ')O(Z- ')(1 - z - I ) - 6(z - ')# - 1 x 1 - z - ) - o ( z - - ' ) z - f - 'I(z - 1)

x a,+/+,

31

Page 2: 00119089.pdf

If one rewrites the above equation as

a(z - ') B(Z-1) - y(z- l )z-f- l l (z- l ) Y , =

where

and defines the following matrices and vectors :

with I defined as before:

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

= Gk

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

x,+1 = A*x, + bat

y, = CTX, + a,

with

A* = [ q zn01] - H G k [ l 0 ... 0)

= A - HGk[l 0 01

b = [q - p - HGk]

c T = [ l 0 ... 01 = [l 01

v= E{x,xT}

By taking the variance on both sides of eqn. 2

= A* VA*T + bbTo,' (3) The variance of the output variable is given by .," = E{Y, YT)

= CTVC + 0: and therefore

(7," = CT[A*VA*T + bb=a:]c + 6,' 32

or

0," = cT[(A - HGk[l O])V(A - HGk[l 01)'

+ [q - p - HGk][q - p - HGklTa,']c + c,' = c T A V A T - AV + ( q - p ) a : kTGTHT [ [ [:I 1

+ HGk[l O]V[:]k'cTHT

- HGk[[l 01 VAT + (4 - P)~cT, ']

+ [q - p][q - plTa: + HGka2kTGTHT c + U,' 1 = CT A V A T + [q -p] [q - p y a : I

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

A v[ A ] + (4 - P)d

0,' + c1 01v[:] HGk - = o

Therefore the optimal gains of a PID controller are given by

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

Page 3: 00119089.pdf

and from this equation calculate the initial gain vector

C - ' [ H T H ] ' H T p V , , [ i ] + (q -p)o,'] k , = r.1

- -0.168 0 0

0.2184 -0.168 0 -0.02184 0.2184 -0.168 -0.02856 -0.02184 0.2184

0 - 0.02856 - 0.021 84 - 0 0 - 0.02856

(ii) Use eqn. 3 to calculate the variance matrix V,, for

( 5 )

the next iteration as below

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

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

G- 1 [ H T ~ - "[A v,[ i] + (4 - p).:] k, = (6)

0,' + c1 01 vt[ ;] (iv) Iterate between eqns. 5 and 6 until convergence, i.e.

Vr+, = Vr = v

(v) Use the converged value of the variance matrix Y to calculate the gain vector k.

Such an approach is similar to that used to calculate the steady state Kalman filter, and eqn. 3 is a Riccati equa- tion.

3 Optimal deterministic PID controller

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 con- stant 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. Con- sider the deterministic system

4 2 - '1 -- 6(z-') U, Y r + f + l -

with a set point change y ~ p , + ~ + ~ at time t + f + 1. The above equation can be written as

Y t + f + l - Y S P ~ + / + ~ = ~ : + f + l

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

Ramp change to a new level

a, YSP, = (1 - z-1)2

Exponential change to a new level

a, YSPr = (1 - z- ' 1 - bz-')

4 Examples

In this Section two examples are given to illustrate the presented theory. First consider a stochastic control system described by the following equation

0.168 y , = 1 - 0.9082- 1 1

1 + (1 - z-')(1 - 0.32-' - 0 . 1 7 ~ - ~ ) "

with a, as the generating white noise having variance U,' = 2.37. This system gives

4 z - l ) = 0.168

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

e(z-1) = 1

f = O

$(z-') = 1 - 1.3z-l + 0.13~-' + O.17zY3

and

.(z-') = 1 - 1.9082-' + 0.908~-'

@(z-') = 1 - 3.2082-' + 3.5184~-' - 1.25844zY3

- 0.20632~-~ + O.15436zY5

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

+ 0.02184~-~ + 0.02856~-~

Therefore

P =

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

below.

Step change to a new level

variance. A few common set point changes are described H =

1

0 0 0

- 3.208

1.25844 0.20632

- 3.5 184

- 0.15436 - 0

The variance matrix V was found to be a, YSPr =

V = 1 0.0588 -0.0539 -0.0022 -0.0016 -0.0008 -0.0002 0.0519 0.0017 0.0006 -0.0000 -0.0002

0.0006 0.0002 -0.0001 -0.ooO1 0.0005 0.0002 -0.oooO

0.0005 0.0002 sYm O.OOO4

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

Page 4: 00119089.pdf

With the optimal gains [;I=[ I;;;;] 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 1 - 0.22-' yt = 1 - 0.9082-1 't-1 + 1 - z - l at

with 0: = 2.0. This system gives

O ( Z - ' ) = 0.168

6(z-') = 1 - 0.908~-'

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

4(z- ' ) = 1 - 2 - 1

f = O

The minimum variance controller for this system obtained by the method of Box and Jenkins [lo] is

In the next example, consider the system

1 - 0.32-' at

0.250 1 - 0.22-' Yt = 1 - o.*z-' U t - 1 +

with 0: = 2.0. This system gives

o(z- ') = 0.250

6(z- ') = 1 - 0.82-

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

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

f = O

(1 - 0.2)(1 - 0.9082- ') (1 - . - ' ) U t = - Yt 0.168

0.8 0.168 c v t - 0.908Yt- 1) = --

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-' + 1 .2896~-~ - 0 .1816~-~

P(2-l) = 1 - 2.9082-' + 2 . 8 1 6 ~ ~ ~ - 0.908zY3

~ ( 2 - l ) = 0.168 - 0.168z-'

Therefore

2.108 2.908

-0.168 0 0 0.168 -0.168

0.168 -0.168 x 0 1 .161

The variance matrix Y was found to be

Y x 1.OE10

0.0500 - 0.0956 0.0456 0.1228 -0.0871

0.04 15 sym.

with the gains and feedback output variance as

which are identical to the solutions obtained previously.

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

a(z-') = 1 - 2 . 0 ~ ~ ' + 1.16z-' - 0 . 1 6 ~ - ~

/3(z-') = 1 - 2.12-' + 1.34zY2 - 0 . 2 4 ~ - ~

y(z- ') = 0.25 - 0.0752- ' Therefore

2.000 2.100

-0.250 0 0 0.075 -0.250

The variance matrix V was found to be

0.1420 -0.1375 0.0219 0 0.1343 -0.0213 0

v = [ 0.0036 0 sym. 0.0002

with the optimal gains

- 0.5568

-0.1156

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 con- trollers but also for any type of controller with known structure or orders.

34 I E E PROCEEDINGS-D, Vol. 139, N O . I , JANUARY 1992

Page 5: 00119089.pdf

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

-

6 1

2

3

4

5

7

ZIEGLER, J.G., and NICHOLS, N.B.: ‘Optimum setting for PID controllers’, Trans. ASME, 1942,64, pp. 759-768 COHEN, G.H., and COON, G.A.: ‘Theoretical considerations of retarded control’, Trans. ASME, 1953,75, p. 827

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 con- troller with stepwise parameter optimization’, Automatica, 1987, U,

ZERVOS, C., BfiLANGER, P.R., and DUMONT, G.A.: ‘On PID controller tuning using orthonomd Eries identification’, ~ ~ t ~ - matica, 1988,24, (2), pp. 165-175 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 MAcGREGOR, J.F., WRIGHT, J.D., and HUYNH, M.H.: Technometrics, 1984,26, (4), pp. 389-397 ‘Optimal tuning of digital PID controllers using dynamic-stochastic models’, IEC Process Des. Dev., 1975,14, pp. 398-402

pp. 449-451 8 ASTROM, K.J., and HAGGLUND, T.: ‘Automatic tuning of PID

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’,

10 BOX, G.E.P., and IENKINS, G.M.: ‘Time series analysis, fore- casting and control’ (Holden Day, San Francisco, 1970)

Appendix

.........................................

Module: main( )

Description:

Author: K. M. Vu 9-May-91

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.

......................................... #include (c:\tc\stdio.h) #include (c:\tc\stdlib.h) #include (c:\tc\math.h) #include (c:\tc\malloc.h) #include (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

Page 6: 00119089.pdf

float float float float float float float float float

av[NDIMS]~DIMS]; avl [NDIMS] ; b[NDIMS] ; bbt[NDIMS][NDIMS] ; vt-sq; vtl-sq; b-sq; pc-change ; accuracy = 1 . M 6 ;

/* The Riccati equation matrix */ /* The Riccati equation matrix */ /* The Riccati equation matrix */ /* The Riccati equation matrix */ /* The current variance matrix norm */ /* The next variance matrix norm */ /* The b vector norm */ /* The Yo change in variance matrix norm */ /* The iteration stopping criterion */

float dim21 = { 1, - l} ; /* The difference polynomial */ 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]);

{

1 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]);

{

1 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; icnphi ; i + +)

printf("Enter the phi's : "); scanf("% f", &phi [i]) ;

{

1 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]);

{

1 printf("Enter the variance of the white noise disturbance : "); scanf("%f", &var);

i = (int) max(nde1ta + nphi + 1,nomega + nphi + ndelay + 3); n =(int) max(i,ndelta + ntheta + 1);

for (i = 0; iendelta + ntheta; i + +)

/* Form the necessary vectors and parameter matrix */

/* Form vector p */

gamma[i] = 0; for ( j = O ; j e i ; j + +) gamma[i] + = delta[j]*theta[i-j];

{

1 {

1 l

for (i =O; 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 + +)

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

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

Page 7: 00119089.pdf

/* Form vector q */ I

{

I 1

}

{

I

for (i=O; irndelta+nphi; i t +)

gamma[i] = 0; for (j =O; j e i ; j + +) gammaci] + = delta[j]*phi[i-j];

for(i=O;iendelta+nphi+l;i+ +)

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

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

gamma[i] = O ; if (iendelta + nphi + 1) q[i] - - beta[i + I]; else q[i] = 0;

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

gamma[i] = 0; for (j =O; j e i ; j + +) gamma[i] + = omega[j]*phi[i-j];

1 {

I {

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 + +) I

h[i]u] = O ; if (i > 0) h[i][j] = h[i - 1][li - 13 ;

} 1 /* 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") ;

I */

{

/* 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] ;

I 1 for (i=O; i<3; i+ +) f I

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

foro =O;j < 3;j + +) r I

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

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

Page 8: 00119089.pdf

/*Calculate the inverse of hth */ I

{ 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+ +)

hthinvcl] [l] = (float)sqrt(hthinv[l] [1]) ; for (i=l+ 1; i<3; i + +)

hthinv[l][l] - = hthinv[l]Cj]*hthinv[l][j];

for (j =O; j < =1- 1; j + +)

hthinv[i][l] = hthinv[i]@]/hthinv[l] [l] ;

{

hthinv[i][l] - = hthinv[i][j]*hthinv[l][j];

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)

else

{

hthinv[l][j] = l,O/hthinv[l][l] ;

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

{

I I I

{

/* 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 + +)sum+ =hthinv[i][l]*hthinv[i][j]; hthinv[l][j] =sum; hthinv[j][l] = hthinv[l][j];

{

1 /* Calculate the matrices for the Riccati equation */

I

{ 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];

{

} I { 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];

{

1 I { 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]+ =h[i][l]*g[l][j];

1 /* Set up initial condition */

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

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

Page 9: 00119089.pdf

k[j] =O; for (I=O;l<n;l+ +) k[j] + =ginvhthinvht[j][l]*(q[l] -p[I]);

{

1

hgk[i] = 0; {

for (1=0;1<3;1+ +) hgk[i] + =hg[i][I]*k[I]; f for (j =O;j <n; j + +) b[j] =q[j] -p[j] -hgk[jl; vt-sq = 0; for(j=O;j<n;j+ +) {

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

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

vt[j][l] = b[j]*b[l]*var; {

1 vt-sq + = vtrj][l]*vtrj][l);

1 mvflag = 0;

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

/* And start the iteration */

/* 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];

{

1 1

{

for (j =O;j <n;J + +) avl[j] =av[j][O] +(q[j] -p[j])*var; for (j =O;j < 3;j + +)

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

1 )* Calculate the Riccati equation */ for (i=O;i<n;i+ +) ( I

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

1

{

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];

1

{

if(b-sq < = 1 .Oe - 06&&vt-sq -= = 1 .Oe - 06) mvflag = 1 ; for (j=O;j<n;j+ +)

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

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

{

1 1

{

/*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

Page 10: 00119089.pdf

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

{

1 1 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] ;

{

1 1 /* And percent of accuracy */ pc-change = fabs(vt 1-sq - vt-sq) / vt-sq; I*

*/ for (i=O;i<n;i+ +) {

1

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

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

printf(“\n”) ; /* Update iteration parameters */ iter+ = 1;

for(j=O;j<n;j+ +) vt-sq = vt 1-sq;

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+ +)

{

for ( j=O; j<n; j+ +)printf(“%ll.4e”, vt[i][j]); printf(“\n”) ;

{

1 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]);

1 { 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);

1 { else

printf(“\nFail to converge after %d iterations\n”,iter); 1

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