'MATLAB find the average time using tic toc

Construct an experiment to study the performance of the Cramer rule (with two implementations determinants) in relation to Gauss's algorithm. In each iteration 10 random arrays A (NxN), and vectors b (Nx1) will be created. The 10 linear systems will be solved using the Cramer rule ("cramer.m") using of rec_det (A) and using det (A), and the Gaussian algorithm (“GaussianElimination.m”), and the time for each technique will be the average of 10 values. Repeat the above for N = 2 to 10 and make a graph of the average time in relation to the dimension N.

This is my task. I dont know if the way that I calculate the average time is correct and the graphic is not displayed.

  T1=0;
 T2=0;
 T3=0;

for N=2:10

for i=1:10

    A=rand(N,N);
    b=rand(N,1);


    t1=[1,i];
    t2=[1,i];
    t3=[1,i];

    tic;
    crammer(A,b);
    t1(i)=toc;

    tic
    crammer_rec(A,b);
    t2(i)=toc;

    tic
    gaussianElimination(A,b);
    t3(i)=toc;

    T1=T1+t1(i);
    T2=T2+t2(i);
    T3=T3+t3(i);
end
avT1=T1/10;
avT2=T2/10;
avT3=T3/10;

end

plot(2:10 , avT1 , 2:10 , avT2 , 2:10 , avT3);


   function x = cramer(A, b)
    n = length(b);
d = det(A); 
% d = rec_det(A);
x = zeros(n, 1);
for j = 1:n
    x(j) = det([A(:,1:j-1) b A(:,j+1:end)]) / d;
    % x(j) = rec_det([A(:,1:j-1) b A(:,j+1:end)]) / d;
end
 end


function x = cramer(A, b)
n = length(b);
d = rec_det(A);
x = zeros(n, 1);
for j = 1:n
    x(j) = rec_det([A(:,1:j-1) b A(:,j+1:end)]) / d;
end
end

             function deta = rec_det(R)
                  if size(R,1)~=size(R,2)
                     error('Error.Matrix must be square.')
                   else
                     n = size(R,1);
                    if ( n == 2 )
                       deta=(R(1,1)*R(2,2))-(R(1,2)*R(2,1));
                      else
      for i=1:n
          deta_temp=R;
          deta_temp(1,:)=[ ];
          deta_temp(:,i)=[ ];
          if i==1
              deta=(R(1,i)*((-1)^(i+1))*rec_det(deta_temp));
          else
              deta=deta+(R(1,i)*((-1)^(i+1))*rec_det(deta_temp));
          end
      end
  end
end 
 end


    function x = gaussianElimination(A, b)
    [m, n] = size(A);
       if m ~= n
         error('Matrix A must be square!');
     end
n1 = length(b);
if n1 ~= n
    error('Vector b should be equal to the number of rows and columns of A!');
end
Aug = [A b]; % build the augmented matrix
C = zeros(1, n + 1);

% elimination phase
for k = 1:n - 1
    % ensure that the pivoting point is the largest in its column
    [pivot, j] = max(abs(Aug(k:n, k)));
    C = Aug(k, :);
    Aug(k, :) = Aug(j + k - 1, :);
    Aug(j + k - 1, :) = C;
    if Aug(k, k) == 0
        error('Matrix A is singular');
    end
    for i = k + 1:n
        r = Aug(i, k) / Aug(k, k);
        Aug(i, k:n + 1) = Aug(i, k:n + 1) - r * Aug(k, k: n + 1);
    end
end

% back substitution phase
x = zeros(n, 1);
x(n) = Aug(n, n + 1) / Aug(n, n);
for k = n - 1:-1:1
    x(k) = (Aug(k, n + 1) - Aug(k, k + 1:n) * x(k + 1:n)) / Aug(k, k);
end

end



Solution 1:[1]

I think the easiest way to do this is by creating a 9 * 3 dimensional matrix to contain all the total times, and then take the average at the end.

allTimes = zeros(9, 3);

for N=2:10

for ii=1:10

    A=rand(N,N);
    b=rand(N,1);

    tic;
    crammer(A,b);
    temp = toc;
    allTimes(N-1,1) = allTimes(N-1,1) + temp;

    tic
    crammer_rec(A,b);
    temp = toc;
    allTimes(N-1,2) = allTimes(N-1,2) + temp;

    tic
    gaussianElimination(A,b);
    temp = toc;
    allTimes(N-1,3) = allTimes(N-1,3) + temp;

end

end

allTimes = allTimes/10;

figure; plot(2:10, allTimes);

You can use this approach because the numbers are quite straightforward and simple. If you had a more complicated setup, the way to store the times/calculate the averages would have to be tweaked.

If you had more functions you could also use function handles and create a third inner loop, but this is a little more advanced.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1