Using basic programming (for loops, while loops, and if statements), write two MATLAB functions, both taking as input:
- dimension n;
- n x n matrix A;
- n x n matrix B;
-n × 1 vector x.
Have the first function compute ABx through (AB)x and the second compute ABx through A(Bx). Have both output:
- the number of flops used.
(a) Print out or write out the first function.
(b) Print out or write out the second function.
(c) Apply both your functions to the case with random matrices and vectors for n = 100 and print out or write out the results. Do the same for 200 x 200, 400 x 400, and 800 x 800. Which approach of computing ABx is faster?

Respuesta :

Answer:

For n = 100

  • (AB)x: 10100
  • A(Bx): 200

For n = 200

  • (AB)x: 40200
  • A(Bx): 400

For n = 400

  • (AB)x: 160400
  • A(Bx): 800

For n = 800

  • (AB)x: 640800
  • A(Bx): 1600

The faster approach is A(Bx)

Explanation step by step functions:

A(Bx) is faster because requires fewer interactions to find a result: for (AB)x you have (n*n)+n interactions while for A(Bx) you have n+n, to understand why please see the step by step:

a) Function for (AB)x:

function loopcount1 = FirstAB(A,B,x)  

 n = size(A)(1);

 AB = zeros(n,n);

 ABx = zeros(n,1);

 loopcount1 = 0;  

 for i = 1:n

   for j = 1:n

     AB(i,j) = A(i,:)*B(:,j);

     loopcount1 += 1;

   end

 end

 for k = 1:n

   ABx(k) = AB(k,:)*x;

   loopcount1 += 1;

 end

end

b) Function for A(Bx):

function loopcount2 = FirstBx(A,B,x)

 n = size(A)(1);

 Bx = zeros(n,1);

 ABx = zeros(n,1);

 loopcount2 = 0;  

 for i = 1:n

   Bx(i) = B(i,:)*x;

   loopcount2 += 1;

 end

 for j = 1:n

   ABx(j) = A(j,:)*Bx;

   loopcount2 += 1;

 end

end

In this exercise we want to use computer and python knowledge to write the code correctly, so it is necessary to add the following to the informed code:

The correct code that corresponds to the question informed is attached in the photo and we can notice that the faster approach is A(Bx).

So knowing that the information given in the text is that;

For n = 100:

  • (AB)x: 10100
  • A(Bx): 200

For n = 200:

  • (AB)x: 40200
  • A(Bx): 400

For n = 400:

  • (AB)x: 160400
  • A(Bx): 800

For n = 800:

  • (AB)x: 640800
  • A(Bx): 1600

A(Bx) exist faster cause demand hardly any interplay to find a result: for (AB)x you bear (n*n)+n interplay while for A(Bx) you bear n+n, so we have that:

a)Watching the function for (AB)x:

function loopcount1 = FirstAB(A,B,x)  

n = size(A)(1);

AB = zeros(n,n);

ABx = zeros(n,1);

loopcount1 = 0;  

for i = 1:n

  for j = 1:n

    AB(i,j) = A(i,:)*B(:,j);

    loopcount1 += 1;

  end

end

for k = 1:n

  ABx(k) = AB(k,:)*x;

  loopcount1 += 1;

end

end

b) Watching the function for A(Bx):

function loopcount2 = FirstBx(A,B,x)

n = size(A)(1);

Bx = zeros(n,1);

ABx = zeros(n,1);

loopcount2 = 0;  

for i = 1:n

  Bx(i) = B(i,:)*x;

  loopcount2 += 1;

end

for j = 1:n

  ABx(j) = A(j,:)*Bx;

  loopcount2 += 1;

end

end

See more about computer at brainly.com/question/950632

Ver imagen lhmarianateixeira
Ver imagen lhmarianateixeira