Question
please answer
Write a short MATLAB Program that generates a plot: Use MATLAB's help, to research the MATLAB time functions "tic" and "toc" and how to use them to measure the passage of time Write a MATLAB program a loop, counter, tic and toc that will measure the time it takes to do the following tasks: a) Create an array of random numbers (rand command) between 0 and 1 b) Create an array of the sums (sum command) of the random numbers. c) Create an array of the averages (mean command) of the random numbers. d) Create an array of the standard deviations (std command) of the random number. Set the counter so this takes approximately .03 seconds. Then create a plot with the elapsed time in seconds on the horizontal axis, and the random number on the vertical axis. Label your plot axes and plot title
Explanation / Answer
%we create a for loop with a large control variable n
%and exit the loop when counter is greater then 0.03
%start%
n=100000;%increase n if loop does not take 0.03 seconds on your computer.
tic;
for i=1:n
r(i)=rand;% random number array
s(i)=sum(r);%sum array
m(i)=mean(r);% mean array
std(i)=std(r);% standard deviation array
counter=toc;% elapsed time
t(i)=counter;% elapsed time array
if(counter>0.03)
break;
end
end
plot(t,r)
title('Time measure in MATLAB');
xlabel('elapsed time in second');
ylabel('Random Number');
%end%