%% Header
% Purpose:  Handles corrections to the ctrl vector to guide opt.
%           routine towards correct optimum. 
% Author:   Axel Lødemel Holene (based on work done by Martin
%           Foss (2012)).
% E-mail:   axel.holene@gmail.com
% Date:     13. February 2013
% About:    

function ctrl = correction(ctrl, i, minTune, parm) ;

modelId = parm.modelId ;
cType   = parm.cType ;
ctrlr   = parm.ctrlr ;
cNum    = parm.cNum ;
Ms_s    = parm.Ms_s ;

    switch cType
        case 1 % PO
        case 2 % SIMC
        case 3 % Smith
            switch ctrlr
                case 1 % PID
                case 2 % PI
                    switch cNum
                        case 1
                        case 2
                        case 3
                            switch modelId
                                case 1
                                    if Ms_s == 1.775
                                        ctrl = [1.4217 0.7601] ;
                                    end
                                    if Ms_s == 2 
                                        ctrl = [2.3634 0.6828] ;
                                    end
                                    if Ms_s == 2.175
                                        ctrl = [2.9980 0.7607] ; 
                                    end
                                case 2
                                    if Ms_s == 1.65
                                        ctrl = [1.0051 0.7187] ;
                                    end
                                    if Ms_s == 1.775
                                        ctrl = [1.4870 0.6024] ;
                                    end
                                    if Ms_s == 1.9
                                        ctrl = [1.8248    0.6145] ;
                                    end
                                    % if Ms_s == 1.925 ;
                                    %     ctrl = minTune(26,:) ;
                                    % end
                                    if Ms_s == 2
                                        ctrl = minTune(31,:);
                                        if min(ctrl) < 1e-3
                                            disp('Wrong i, ms=2')
                                            ctrl = [2.2196 0.5887] ;
                                        end
                                    end
                                    if Ms_s == 2.25
                                        ctrl = [3.7339 0.4853] ; %i = 41
                                    end
                                    if Ms_s == 2.275
                                        ctrl = [3.7128 0.5204];
                                    end
                                case 3
                                    if Ms_s == 1.35
                                        ctrl = [0.5393 0.7719] ;
                                    end
                                    if i == 17
                                        ctrl = 1.1*ctrl ;
                                    end
                                case 4
                                case 5
                                    if Ms_s == 1.925
                                        ctrl = [2.5310 0.5956] ;
                                    end
                                    if Ms_s == 1.95
                                        ctrl = [2.6636 0.5826] ;
                                    end
                                    if Ms_s == 2.275
                                        ctrl = [4.3567 0.5220] ;
                                    end
                                case 6
                                    if Ms_s == 1.45
                                        ctrl = [1.5764 0.9778] ;
                                    end
                                    if Ms_s == 1.525
                                        ctrl = [1.9501 0.9548] ;
                                    end
                                    if Ms_s == 1.775
                                        ctrl = [3.3515 0.9107] ;
                                    end
                                    if Ms_s == 1.85 
                                        ctrl = [3.7826 0.9194] ;
                                    end
                                    if Ms_s == 1.95
                                        ctrl = [4.1827 1.0093] ;
                                    end
                                    if Ms_s == 2.075
                                        ctrl = [5.3259 0.9017] ;
                                    end
                                    if Ms_s == 2.325
                                        ctrl = [7.6617 0.8044] ;
                                    end
                                case 7
                                    if Ms_s == 1.55
                                        ctrl = [1.7501 0.7545] ;
                                    end
                                    if Ms_s == 1.825
                                        ctrl = [2.9478 0.7543] ;
                                    end
                                    if Ms_s == 1.95
                                        ctrl = [3.7111 0.7059] ;
                                    end
                                case 8
                                    if Ms_s == 1.425
                                        ctrl = [0.9102 0.7288] ;
                                    end
                                    if Ms_s == 2.075
                                        ctrl = [3.5223 0.5936] ;
                                    end
                                case 9
                                    if Ms_s == 1.525
                                        ctrl = [2.2334 0.9423] ;
                                    end
                                    if Ms_s == 1.55
                                        ctrl = [2.3717 0.9415] ;
                                    end
                                    if Ms_s == 1.675
                                        ctrl = [3.0031 0.9880] ;
                                    end
                                    if Ms_s == 1.8
                                        ctrl = [3.9295 0.9232] ;
                                    end
                                case 13
                                    if Ms_s == 1.950
                                        ctrl = [7 0.29] ;
                                    end
                            end % End switch modelId
                        case 4
                    end % End switch cNum
            end % End switch ctrlr
    end % End switch cType
end % End function