function [ctrl J optFlag] = manOpt(ctrl, i, parm)
    ctrl0 = ctrl ; %saving for convenience
    parm.Ms_s = Ms_s ;               % Updating Ms setpoint value
    Jfun      = @(ctrl)costFun(ctrl,parm) ;       % Updating cost
    cFun      = @(ctrl)conFun(ctrl,parm);  % Updating constraints
    runOpt = true ;
    while runOpt
        while sNum < 3
            try
                tic ;
                [ctrl J optFlag] = fmincon(Jfun,ctrl,[],[],[],...
                                           [],lb,[],cFun,opt); 
                             % Optimizing from last optimal point
                sTime = toc ;    % Ending CPU time measurement
                if optFlag < sFlags{sNum}
                    fprintf('SQP failed.' )
                    sNum = sNum + 1 ;
                elseif min(ctrl) < 1e-3
                    name = {'Kc' 'I' 'D'} ;
                    fprintf('SQP failed. %s approx zero.\n', ...
                            name{find(ctrl==min(ctrl))})
                    % Hot start from previous working values
                    ctrl = ctrl0 ;
                    sNum = sNum + 1 ;
                else
                    runOpt = false ;  % Opt success. Ending while
                    break
                end
            catch
                fprintf('SQP failed. ',)
                sNum = sNum + 1 ;
            end
        end
        if sNum == 3
            fprintf('Using fminsearch... ',)
            Kc = ctrl(1); ID = ctrl(2:end);
            Jfun = @(ID)noGrad([Kc ID], parm) ;
            try
                tic ;
                [ID J optFlag] = ...
                                fminsearch(Jfun, ID, sOpt) ;
                extrat = toc ;
                sTime = sTime + extrat ;
            catch
                pFlag = false ;
                fprintf('fminsearch failed.\nGiving up.\n')
                fprintf('Error message:\n'); disp(lasterr)
                runOpt = false ;
                exitFlag = false ;
                break
            end
            if pFlag
                fprintf('Success! (%.2f s)\n', extrat)
                sTime = sTime + extrat ;
            else
                pFlag = true ;
            end
            MsFun = @(Kc) Ms_s - Ms([Kc ID], parm) ;
            Kc = fzero(MsFun, Kc) ;
            ctrl = [Kc ID] ;
            Jfun = @(ctrl)costFun(ctrl, parm) ;   % Updating cost
            cFun = @(ctrl)conFun(ctrl, parm);    % Updating cons. 
            try
                fprintf('Second run SQP start... ')
                tic ;
                [ctrl J optFlag] = fmincon(Jfun, ctrl, [], ...
                                            [], [], [], lb,...
                                            [], cFun, opt) ;
                extrat = toc ;
            catch
                pFlag = false ;
                fprintf('Second run SQP failed\n')
                ctrl = [Kc ID];
            end
            if pFlag
                fprintf('Success! (%.2f s)\n', extrat)
                sTime = sTime + extrat ;
            else
                pFlag = true ;
            end
            C = controller(ctrl, parm) ;
            MsCalc = Ms(ctrl, parm) ;
            if (MsCalc > Ms_s + 1e-4)||(MsCalc < Ms_s - 1e-4)
                fprintf('Ms values differ at control calculation.\n')
                fprintf('Ms_s = %0.5f, Ms = %.5f\n',Ms_s, MsCalc)
            end
            runOpt = false ; % Opt success. Ending while.
        end
        end
    end
end