%GOALDEMO Demonstration of the goal-attainment method.
clc
echo on
%   We demonstrate here the use of the Goal Attainment method
%   for pole-placement. It follows the example in the reference manual. 
%   Consider here a 2-input 2-output  unstable plant.
pause % Strike any key to enter plant state-space matrices

A =  [ -0.5   0   0    
        0    -2  10
        0     1  -2];

B =  [  1  0
       -2  2
        0  1];

C =  [  1  0  0
        0  0  1];

%  Suppose we wish to design an output feedback controller(X) to have poles
%  to the left of the location [-5,-3,-1] in the complex plane.
%  The controller must not have any gain element exceeding an absolute
%  value of 4.

pause % Strike any key to enter goal pole locations

GOAL=[-5,-3,-1]

% Set the weights equal to the goals to ensure same percentage 
% under- or over-attainment in the goals.

pause % Strike any key to enter weights 

W=abs(GOAL)

% Initialize output feedback controller

X = zeros(2,2); 

pause % Strike any key to set upper and lower bounds for X

VUB =  4*ones(2,2)
VLB = -4*ones(2,2)

pause % Strike any key to initialize optimization parameters 

OPTIONS = 0; 		% Use default termination and other criteria

pause % An M-file to return eigenvalues of closed loop system:

type eigfun

pause % Set optimization display parameter to give tabular display:

OPTIONS(1) = 1;

pause % Strike any key to start optimization

[X,OPTIONS] = attgoal('eigfun',X,GOAL,W,OPTIONS,VLB,VUB,[],A,B,C); 

pause % Strike any key to see results

%  The value of the control parameters at the solution is as follows:
X
%  The eigenvalues of the closed loop system are as follows:
eigfun(X,A,B,C)
%  The attainment factor (OPTIONS(8)) is as follows:
OPTIONS(8)
%  This indicates that the objectives have been over-achieved by 38.63%.
  
pause % Strike any key for next problem
clc

% Suppose we require the eigenvalues to be as near as possible
% to the goal values, [-5, -3, -1]. 

% Set OPTIONS(15) to the number of objectives that should be as near as possible
% to the goals:

OPTIONS(15) = 3;

pause % Strike any key to start optimization

[X,OPTIONS] = attgoal('eigfun',X,GOAL,W,OPTIONS,VLB,VUB,[],A,B,C); 

pause % Strike any key to see results

%  This time the eigenvalues of the closed loop system are as follows:
eigfun(X,A,B,C)
%  The attainment factor (OPTIONS(8)) is as follows:
OPTIONS(8)
% which indicates that the eigenvalues have almost exactly met the goals.

pause % Strike any key to return 
