Find the number of multiplications x ones1 n t linspace0 1
Find the number of multiplications x = ones(1, n); t = linspace(0, 1, 100); y = ones(size(t)); for k = 1:n x(k:n) = k* x(k:n); end for k=n-1:-1:1 y = y .* t + x(k); end
Solution
x = ones(1, n) %x is an array of n 1\'s.
t = linspace(0, 1, 100); %100 values between 0, and 1 inclusively.
y = ones(size(t)); %y is an array of 100 1\'s.
%No multiplications till now.
for k = 1 : n
x(k:n) = k * x(k:n) %Multiplications are happening here.
%When k = 1, n multiplications.
%When k = 2, n-1 multiplications.
%.....
%When k = n, 1 multiplication.
end
%So, as of now, the number of multiplications is: 1 + 2 + ... + n =
%n*(n+1)/2.
for k = n-1 : -1 : 1
y = y .* t + x(k) %More multiplications here.
%When k = n-1, 100 multiplications.
%When k = n-2, 100 multiplications.
%.....
%When k = 1, 100 multiplications.
end
%So, for this loop, the number of multiplications is: (n-1) * 100.
%Therefore, total number of multiplications is: 100(n-1) + n(n+1)/2
