clear all; N = 1e6; t=linspace(0.0,8.0,N); % Note that linspace produces a row v
ID: 3855768 • Letter: C
Question
clear all; N = 1e6; t=linspace(0.0,8.0,N); % Note that linspace produces a row vector
What is the result of the following set of Matlab statements? Describe your answer in sentences. In addition to a functional description, give the sizes of any matrices produced. Do not attempt to perform the numerical computations in detail. If the command(s) results in an error, state what the error is. It is not necessary to find any subsequent errors after the first one is encountered. a) y = 7*exp(-t/2.0).*sin((2*pi/4.0)*t) + 25; figure; plot(t,y)
b) for ii=0:(N-1) z(ii) = t(ii)/2.0; end
c) s = zeros(size(t)) – 4.0; r = t + s;
d) ta = t(1:(N/2)); tb = t((N/2 + 1):end); tc = (ta’)*tb; td = ta*(tb’);
e) ii = N; A = eye(N,N); tic while (ii>0) A(ii,:)= t(ii); ii = ii – 2; end toc
Explanation / Answer
a) y = 7*exp(-t/2.0).*sin((2*pi/4.0)*t) + 25;
figure;
plot(t,y)
this statement will calculate the value of y according to the expression,then produce
a figure of plot of calculated y values on y axis and t on x axis
b) for ii=0:(N-1)
z(ii) = t(ii)/2.0;
end
This throws an error "Subscript indices must either be real positive integers or logicals"
which means inde can not be 0 ,which is in the case above as ii = 0
c) s = zeros(size(t)) – 4.0;
r = t + s;
This will produe an array s of size same as that of t and all elements are -4.Then it produces a matrix
r which is a sum of t and s.
d) ta = t(1:(N/2));
ta is a matrix whic has first (n/2) elements of t
tb = t((N/2 + 1):end);
tb is a matrix which has last N/2 elements of t
tc = (ta')*tb;
tc is a matrix which is a product of transpose of ta(denoted by ')and tb
Matlab gave an error in mine saying very large arrays demanded(memory requirement)
td = ta*(tb');
tc is a matrix which is a product of transpose of tb(denoted by ')and ta
Matlab gave an error in mine saying very large arrays demanded(memory requirement)
e) ii = N;
A = eye(N,N);
tic
while (ii>0)
A(ii,:)= t(ii);
ii = ii - 2;
end
toc
A is an identity matrix of size N x N
The while loop runs untill ii is positive and inside it, the loop replaces
every row of A with the element of t at the index told by ii
The tic toc commands prints the time taken to execute code in the format
Elapsed time is ____ seconds.
It gave a memory requirement error as the matrix was too big in size
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.