hw2: preparing for submission

This commit is contained in:
Claudio Maggioni 2020-03-20 22:01:00 +01:00
parent 5375b665c6
commit 49581b0c5f
2 changed files with 55 additions and 0 deletions

View file

@ -31,6 +31,61 @@ end
loglog(h, err) loglog(h, err)
%% Problem 6
n = 3000;
A = rand(n,n);
B = rand(n,n);
tic
[C] = matTimesMat_classical(A, B);
toc
norm(C - A * B) / norm(C)
tic
[C] = matTimesMat_column(A, B);
toc
norm(C - A * B) / norm(C)
tic
[C] = matTimesMat_outerProd(A, B);
toc
norm(C - A * B) / norm(C)
%% Problem 3 (functions)
function approx = my_diff(f, x, x_0, h) function approx = my_diff(f, x, x_0, h)
approx = (subs(f,x,x_0 + h) - subs(f, x, x_0)) / h; approx = (subs(f,x,x_0 + h) - subs(f, x, x_0)) / h;
end end
%% Problem 6 (functions)
function [C] = matTimesMat_classical(A, B)
s = size(A);
n = s(1);
C = zeros(n);
for i=1:n
for j=1:n
for k=1:n
C(i,j) = C(i,j) + A(i,k) * B(k,j);
end
end
end
end
function [C] = matTimesMat_column(A, B)
s = size(A);
n = s(1);
C = zeros(n);
for j=1:n
for k=1:n
C(:,j) = C(:,j) + B(k,j) * A(:,k);
end
end
end
function [C] = matTimesMat_outerProd(A, B)
s = size(A);
n = s(1);
C = zeros(n);
for j=1:n
C = C + A(:,j) * B(j,:);
end
end

Binary file not shown.