%GOALDEMO Demonstration of the goal-attainment method.

%   Copyright 1990-2000 The MathWorks, Inc.
%   $Revision: 1.16 $  $Date: 2000/06/16 22:24:28 $

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 

WEIGHT = abs(GOAL)

% Initialize output feedback controller

X0 = [ -1 -1; -1 -1]; 

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

UB =  4*ones(2,2)
LB = -4*ones(2,2)

pause % Strike any key to initialize optimization parameters 

OPTIONS = [];        % 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 output at each iteration:

OPTIONS = optimset('Display','iter');

pause % Strike any key to start optimization

[X,fval,attainfactor,exitflag,output,lambda] = fgoalattain('eigfun',X0,GOAL,WEIGHT,[],[],[],[],LB,UB,[],OPTIONS,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 indicates the level of goal achievement.
%  A negative attainment factor indicates over-achievement, positive
%  indicates under-achievement.

attainfactor 
%  This indicates that the objectives have been over-achieved by about 39%.
  
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.GoalsExactAchieve to the number of objectives that should be as near as possible
% to the goals (i.e., do not try to over-achieve):

OPTIONS = optimset(OPTIONS,'GoalsExactAchieve',3);

pause % Strike any key to start optimization

[X,fval,attainfactor,exitflag,output,lambda] = fgoalattain('eigfun',X0,GOAL,WEIGHT,[],[],[],[],LB,UB,[],OPTIONS,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 is the level of goal achievement.
%  A negative attainment factor indicates over-achievement, positive
%  indicates under-achievement.

attainfactor
% This indicates that the eigenvalues have almost exactly met the goals.

pause % Strike any key to return 
echo off
disp('End of demo')





