⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testjacobisamematrices.m

📁 jacobi algorithm matlab code
💻 M
字号:
%testJacobiSameMatrices calls jacobi() for ten random matrices and uses 
%the same matrices with jacobiNoMax() for ten random matrices as well. 
%We then graph the results. We also display the original matrix and the 
%final diagonalized matrix on the command window.

%NOTE:
    %**We will reference off() several times during this function for
    %  simplicity, where off() is the sum of the square of each off
    %  diagonal element.

function [avgSort, avgNoSort]= testJacobiSameMatrices()

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%Testing the Jacobi Algorithm with Sorting[START]%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    %randomMatricesStruct is a Structure that will house the random 
    %matrices generated by jacobi() to be used for jacobiNoMax()
    randomMatricesStruct = {};
    
    %Used for calculating the average iterations necessary to diagonalize
    kTotal = 0;
    
    %Technically we want to make 10 random matrices, but Matlab's subplot
    %function is a little strange when it comes to graphing two lines on one
    %plot and for graphing in a loop. So I had to fix this problem by
    %iterating 11 times but breaking before we graph the 11th matrix.   
    for (i=1:11)
        %kValues is the vector containing the k values we want to plot,
        %where the final k is equal to the number of iteration it took to
        %diagonalize the matrix.
        kValues = [];
        %bkValues is the vector containing the bk values we want to plot,
        %where bk is the off() of the more diagonalized random matrix
        bkValues = [];
        %bk is also the off() of the more diagonalized random matrix, but we
        %use this variable to compare with the error 10^-9.
        bk = 1;
        %k tracks the number of iterations it takes us to diagonalize the
        %random matrix
        k = 0;
        %D is the progressively more diagonalized matrix
        D=[];
        %counter is a counter that tracks the iterations it takes to
        %diagonalize the matrix, but it is mainly used as a signal so that
        %we know when the jacobi() returns its random matrix that has not
        %been diagonalized at all
        counter = 1;
        %ORGmat will house the original random matrix that has not been
        %diagonalized at all
        ORGmat =[];
        
        A = [];
        %We'll keep iterating until the off() of the diagonal matrix is less
        %than 10^-9.
        while (bk > 10^-9)
            %We call jacobi() as according the specifications of the
            %function in terms of the inputs and outputs
            [D, bk, k, A] = jacobi(D, k, A);
            %If this is the first we're doing this, then the A jacobi
            %returns is the original random matrix
            if (counter==1)
                %Here we calculate the off(A) and the value will be stored
                %in off.
                randomMatricesStruct{i} = A;
                ORGmat = A;
                off = 0;
                for (row = 2:5)
                    for (col = 1:row-1)
                        off = off + 2*(A(row, col))^2;
                    end
                end
            end
            %Add the returned k value the vector
            kValues = [kValues k];
            %Add the returned bk values to the vector
            bkValues = [bkValues bk];
            %Increment counter
            counter=counter+1;
        end
        %Printing to the command window
        if (i ~= 11)
            disp(['Jacobi Test ' num2str(i)]);
            disp('Original Random Matrix');
            disp(ORGmat);
            disp('Diagonalized Matrix');
            disp(D);
            disp('Number of iterations');
            disp(k);
            %adds k to the running sum of all the k's
            if (i ~= 11)
                kTotal = kTotal + k;
            end
            disp('Off(DiagonalizedMatrix)');
            disp(bk);
        end
        %Plot (k,ln(bk)) and connect the points with a green line.
        plot(kValues, log(bkValues), 'g');
        
        %Title the plots as Jacobi Test and then the iteration
        title(['Jacobi Test ' num2str(i-1)]);
        
        %Calling hold on allows me to plot more lines on the same graph
        hold on;
        
        %Calculate the upper bound as according to the formula provided:
        %   y = x * ln(9/10) + ln(off), x = k
        y = kValues.* log(9/10) + log(off);
        
        %Plot x, y and connect the points with a blue line
        plot(kValues, y, 'b');
        
        %This is part of my debug where I break before the program plots
        %the 11th graph
        if (i==11)
           break;
        end
        %subplot(m,n,p) allows us to break the figure into a matrix whose
        %dimensions are m by n. p is similar to the index of this matrix we
        %wish to graph on and are numbered like so:
        %       |1  2  3  4|
        %       |5  6  7  8|
        %       |9 10 11 12|
        %This is also evident from the titles of the graphs
        subplot(4,3,i);
    end
    %Labels the graph
    legend('experimental', 'theoretical', 'location', 'Best');
    hold off;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%Testing the Jacobi Algorithm with Sorting[END]%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%Testing the Jacobi Algorithm without Sorting[START]%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %The comments here are basically the same as the comments above. With
    %only a few minor additions.

    %Technically we want to make 10 random matrices, but Matlab's subplot
    %function is a little strange when it comes to graphing two lines on one
    %plot and for graphing in a loop. So I had to fix this problem by
    %iterating 11 times but breaking before we graph the 11th matrix.
    
    %Calling figure will graph all of this portion of the function in a
    %different window.
    figure;
    
    %Used for calculating the average iterations necessary to diagonalize
    kTotal2=0;
    
    for (i=1:11)
        %kValues is the vector containing the k values we want to plot,
        %where the final k is equal to the number of iteration it took to
        %diagonalize the matrix.
        kValues = [];
        %bkValues is the vector containing the bk values we want to plot,
        %where bk is the off() of the more diagonalized random matrix
        bkValues = [];
        %bk is also the off() of the more diagonalized random matrix, but we
        %use this variable to compare with the error 10^-9.
        bk = 1;
        %k tracks the number of iterations it takes us to diagonalize the
        %random matrix
        k = 0;
        %D is the progressively more diagonalized matrix
        D=[];
        %counter is a counter that tracks the iterations it takes to
        %diagonalize the matrix, but it is mainly used as a signal so that
        %we know when the jacobi() returns its random matrix that has not
        %been diagonalized at all
        counter = 1;
        %ORGmat will house the original random matrix that has not been
        %diagonalized at all
        ORGmat =[];
        
        A =[];
        %We'll keep iterating until the off() of the diagonal matrix is less
        %than 10^-9.
        while (bk > 10^-9)
           
            %If this is the first we're doing this, then the A jacobi
            %returns is the original random matrix
            
            if (counter==1)
                %We call jacobiNoMax() as according the specifications of the
                %function in terms of the inputs and outputs.
                [D, bk, k, A] = jacobiNoMax(randomMatricesStruct{i}, k, A);
                
                %Here we calculate the off(A) and the value will be stored
                %in off.
                off = 0;
                ORGmat = A;
                for (row = 2:5)
                    for (col = 1:row-1)
                        off = off + 2*(A(row, col))^2;
                    end
                end
            else
                %We call jacobiNoMax() as according the specifications of the
                %function in terms of the inputs and outputs.
                [D, bk, k, A] = jacobiNoMax(D, k, A);  
            end
            %Add the returned k value the vector
            kValues = [kValues k];
            %Add the returned bk values to the vector
            bkValues = [bkValues bk];
            %Increment counter
            counter=counter+1;
        end

        %Printing to the command window
        if (i ~= 11)
            disp(['Jacobi (No Sorting) Test ' num2str(i)]);
            disp('Original Random Matrix');
            disp(randomMatricesStruct{i});
            disp('Diagonalized Matrix');
            disp(D);
            disp('Number of iterations');
            disp(k);
            %adds k to the running sum of all the k's
            if (i ~= 11)
                kTotal2 = kTotal2 + k;
            end
            disp('Off(DiagonalizedMatrix)');
            disp(bk);
        end
          
        %Plot (k,ln(bk)) and connect the points with a green line.
        plot(kValues, log(bkValues), 'g');
        
        %Title the plots as Jacobi Test and then the iteration
        title(['No Sort Test ' num2str(i-1)]);
        
        %Calling hold on allows me to plot more lines on the same graph
        hold on;
        
        %Calculate the upper bound as according to the formula provided:
        %   y = x * ln(9/10) + ln(off), x = k
        y = kValues.* log(9/10) + log(off);
        
        %Plot x, y and connect the points with a blue line
        plot(kValues, y, 'b');
        
        %This is part of my debug where I break before the program plots
        %the 11th graph
        if (i==11)
           break;
        end
        %subplot(m,n,p) allows us to break the figure into a matrix whose
        %dimensions are m by n. p is similar to the index of this matrix we
        %wish to graph on and are numbered like so:
        %       |1  2  3  4|
        %       |5  6  7  8|
        %       |9 10 11 12|
        %This is also evident from the titles of the graphs
        subplot(4,3,i);
    end
    %Labels the graph
    legend('experimental', 'theoretical', 'location', 'Best');
    hold off;
    
    disp('average number of iterations for WITH sorting: ');
    disp(kTotal/10);
    %average number of iterations for WITH sorting
    avgSort = kTotal/10;
    disp('average number of iterations for WITHOUT sorting: ');
    disp(kTotal2/10);
    %average number of iterations for WITHOUT sorting
    avgNoSort = kTotal2/10;
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%Testing the Jacobi Algorithm without Sorting[END]%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -