% Purpose:  Calculation and graphical representation of pareto 
%           optimal PID controller tuning.
% Author:   Axel Lodemel Holene
% E-mail:   axel.holene@gmail.com
% Date:     May 2013
% About:    

clear all

mDir = fullfile(pwd) ;
shared = fullfile(mDir,'commonFiles') ;     addpath(shared)
m2tkz  = fullfile(mDir,'matlab2tikz/src') ; addpath(m2tkz)
varclr = fullfile(mDir,'varycolor') ;       addpath(varclr)

parm.cType   = 1 ;                           % Optimisation type
parm.ctrlr   = 1 ;                     % Controller (PID, PI, P)
parm.cNum    = 1 ;                            % Parameterisation
parm.dFilter = 1 ;                          % PID with D-filter?
parm.alph = 0.1 ;                       % Derivative filter size
parm.simCost = 1 ;            % CostFun: step(0) or Simulink(1)?
parm.dispErrors = 1 ;
parm.simTime = 50 ;
parm.Jweights = [0.5 0.5] ;
parm.IAEweights = [1 ; 1] ;
opt = optimset('Display','off','UseParallel','Always') ;
modelId = 1 ;
parm.modelId = modelId ;
parm.G = model(modelId) ;
Dspan = 0:0.01:1 ;
Ispan = Dspan ;
K = 0.1 ;
J = zeros(numel(Dspan)) ;
j = 1;

fprintf('Producing triplot. \n')
tic ;
for D = Dspan
    i = 1 ;
    for I = Ispan
        if I + D > 1
            J(i,j) = NaN;
        else
            MsFun = @(K) 1.59 - Ms([K I D],parm) ;
            K = fzero(MsFun,K,opt) ;
            J(i,j) = costFun([K I D], parm) ;
        end
        i = i + 1 ;
        fprintf('*')
    end
    j = j + 1 ;
    fprintf('\n')
end
t = toc ;
fprintf('Finished in %.2f seconds! \n', t)
surf(J)
colormap winter
xlabel('\mathcal{I}','interpreter','latex')
ylabel('\mathcal{D}','interpreter','latex')
zlabel('J(\mathcal{I},\mathcal{D}','interpreter','latex')
dlmwrite('triplot.csv',J,'precision','%.10f')
matlab2tikz('triplot.tex')
