You are on page 1of 7

Team:

1. Marisnelvys Cabreja Consuegra


2. Dannys García Miranda
Practice 4: Matrix norms
This practice is about the cost of calculating the standard of a matrix. In particular, we will
compare the cost of calculating the norm of some particular matrix using standard 2 and
the Frobenius standard.

1 To estimate by hand, the computational cost, in flops, of calculating the Frobenius norm
of an nxn matrix, we have used the results yielded by the matlab, from n=10 to n=163840

Time to calculate the


Values of n Comparison
Frobenius Norm
10 0,000015 0,000015
20 0,000005 0,00001
40 0,000005 0
80 0,000006 -0,000001
160 0,000006 0
320 0,000004 0,000002
640 0,000004 0
1280 0,000005 -0,000001
2560 0,000005 0
5120 0,000004 0,000001
10240 0,000004 0

% Matrix Anxn; n=10


A=magic(10);
% Frobenius norm: N2
N2 = norm(A,"fro")
% Elapsed time to calculate the Frobenius Norm
tic;N2;toc

A=magic(10);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(20);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(40);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(80);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(160);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(320);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(640);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(1280);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(2560);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(5120);
N2 = norm(A,"fro")
tic;N2;toc
A=magic(10240);
N2 = norm(A,"fro")
tic;N2;toc
As can be seen, the largest value obtained was for n=10 and then as the value of n
doubled, the Frobenius Norm decreased, then its value remained the same for the next
double of n and then increased and it stayed the same for the next double of n and so on.

2, 3

Values of n Norm 2 Frobenius Norm

1000 0,000175 0,000065


10000 0,000292 0,000071
Norm2 B/Norm2 A 1,668571 1,092308
FNorm2 B/FNorm2 A 0,000117 0,000006

% Matrix Anxn; n=1000


A=magic(1000);
% Norm 2: N1
N1=norm(A)
% tic operates with the toc function to measure elapsed time. The tic function records the
current time and the toc function uses the recorded value to calculate the elapsed time to
calculate Norm 2
tic;N1;toc
% Frobenius norm: N2
N2 = norm(A,"fro")
% Elapsed time to calculate the Frobenius Norm
tic;N2;toc
% Matrix Anxn; n=10000
B=magic(10000);
% Norm 2: N1
N1=norm(B)
% tic operates with the toc function to measure elapsed time. The tic function records the
current time and the toc function uses the recorded value to calculate the elapsed time to
calculate Norm 2
tic;N1;toc
% Frobenius norm: N2
N2 = norm(B,"fro")
% Elapsed time to calculate the Frobenius Norm
tic;N2;toc
4 The Frobenius Norm took longer to compute than Norm 2, and it took longer for matrix
B than for matrix A

Practice 5: Matrix product and ortogonal (unitary) matrix

clear all
A=[2 3 4; 4 5 6], B=[ 4 5 6 7; 5 6 7 8; 2 3 4 5]
[m,p]=size(A);
[p,n]=size(B);
for i=1:m
for j=1:n
s=0;
for k=1:p
s=s+A(i,k)*B(k,j);
end

C(i,j)=s
pause(0.5)
end
end

C = A*B
C = mtimes(A,B)
C(i,j) = A(i,:)*B(:,j)
for i=1:10
for j=1:10
A(i,j)=i
for i=1:10
for j=1:10
B(i,j)=j
C(i,j) = A(i,:)*B(:,j)
end
end
end
end

You might also like