%NOTE: Ver 3 searches with paramidcp41f_auto.m
%Ver 3 is for servos
clear SSRspace
if (exist('modsearch')==0)
    modsearch=menu('Select the model space to search','ARX','ARMAX','BJ');
end
switch modsearch
    case 1
        %Search space for ARX model
        ARXnum=4;
        ARXden=4;
        ARXd=3;
        model=1;
        for it=1:ARXnum
            for jt=1:ARXden
                for kt=1:ARXd
                    orders=[it jt kt];
                    fprintf(['Searching orders [' num2str(orders(1)) ' ' num2str(orders(2))...
                            ' ' num2str(orders(3)) ']\n'])
                    paramidcp41f_auto
                    SSRspace(it,jt,kt)=sqrt(mean(SSR));
                end
            end
        end
        save ARXsearchSERVO SSRspace
        clear ARXnum ARXden ARXd model orders
    case 2
        %Search space for ARMAX model
        ARMAXnum=4;
        ARMAXden=4;
        ARMAXnonum=4;
        ARMAXd=3;
        model=2;
        for it=1:ARMAXnum
            for jt=1:ARMAXden
                for kt=1:ARMAXnonum
                    for mt=1:ARMAXd
                        orders=[it jt kt mt];
                        fprintf(['Searching orders [' num2str(orders(1)) ' ' num2str(orders(2))...
                                ' ' num2str(orders(3)) ' ' num2str(orders(4)) ']\n'])
                        paramidcp41f_auto
                        SSRspace(it,jt,kt,mt)=sqrt(mean(SSR));
                    end
                end
            end
        end
        save ARMAXsearchSERVO SSRspace
        clear ARMAXnum ARMAXden ARMAXnonum ARMAXd model orders
    case 3
        %Search space for BJ model
        BJnum=4;
        BJden=4;
        BJnonum=4;
        BJnoden=4;
        BJd=3;
        model=3;
        for it=1:BJnum
            for jt=1:BJden
                for kt=1:BJnonum
                    for mt=1:BJnoden
                        for nt=1:BJd
                            orders=[it jt kt mt nt];
                            fprintf(['Searching orders [' num2str(orders(1)) ' ' num2str(orders(2))...
                                    ' ' num2str(orders(3)) ' ' num2str(orders(4)) ...
                                    ' ' num2str(orders(5)) ']\n'])
                            paramidcp41f_auto
                            SSRspace(it,jt,kt,mt,nt)=sqrt(mean(SSR));
                        end
                    end
                end
            end
        end
        save BJsearchSERVO SSRspace
        clear BJnum BJden BJnonum BJnoden BJd model orders
    otherwise
        disp('ERR: No model input - enter ''modsearch'' value')
    clear modsearch        
end
