W5Q1: Find a percentile Description ---------------------------------------------------------- The |Statistics Toolbox| makes available a number of high-level functions, |prctile| being one of them. Note that, to compute a 50th percentile, the function |median| would do just fine (and does not require the Toolbox!). Starting today, I hope, the |Statistics Toolbox| should be enabled for your set of weekly problems. *A vector contains all the integers 1 through 100. A same-length vector contains numbers that are powers, equally spaced between 0 and 1. Calculate the value of the 37th, the 86th, and the 95th percentiles of the elements of the first vector raised to the powers of the second (in an element-by-element sense), and assign the results to the vector variable* |vp|. Solution Template ---------------------------------------------------- % Define the percentiles ahead of time prc= % Calculate the requested percentile for the requested data set vp= Reference Solution --------------------------------------------------- prc=[37 86 95]; vp=prctile([1:100].^linspace(0,1,length(v)),prc); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution') assert(isequal(vw,[1:100].^linspace(0,1,100))) assessVariableEqual('vp',prctile(vw,prc),'AbsoluteTolerance',1e-10) W5Q2: Judge a correlation Description ---------------------------------------------------------- *Use the function* |corr| *to calculate the correlation coefficient between the two variables of a given data set*, |x1| *and* |x2|. *When properly used, the* _first_ *output of* |corr| *is a simple number, the correlation coefficient that we are after. Collect this value in a variable* |r|. *The* _second_ *output of* |corr| *is another number, with the result of a* _very specific_ *significance test in. Collect this value in a variable* |p|. *It is to be interpreted as the probability that a correlation as high as observed, or even higher, might arise by chance if the variables whose correlation you are testing are _normally_ distributed (which they may not be, and which should be independently tested!) and _uncorrelated_ (that is the null hypothesis). This very particular* _p-value_ *needs to be very low, say, below 5%, for us to accept that the variables in question are _correlated_ (which is, for us to _reject_ the null hypothesis). Collect the result of the test using the p-value at this 95% significance level (does* |p| *fall below the 5% threshold?) in a logical variable that you name* |t|.* _Note: unlike for_ |corrcoef|, _the_ |Statistics Toolbox| _is required for_ |corr|, _and it should be available under Cody. You may want to compare the behavior of inputs and outputs of these two functions._ Solution Template ---------------------------------------------------- % Here are the data, do not touch the two lines below x1=[ 1 5 -6 8 9 2 0 -2 18 29 54]; x2=[-1 4 -6 4 12 -2 3 -2 33 19 47]; % Here is the significance level alfa=0.95; % Here is what we want to know about the data [r,p]= % Here you conduct the test t= Reference Solution --------------------------------------------------- x1=[ 1 5 -6 8 9 2 0 -2 18 29 54]; x2=[-1 4 -6 4 12 -2 3 -2 33 19 47]; [r,p]=corr(x1(:),x2(:)); alfa=0.95; t=p<(1-alfa); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution') assessVariableEqual('r',0.9277,'AbsoluteTolerance',1e-4) assessVariableEqual('p',3.8644e-05,'AbsoluteTolerance',1e-9) assessVariableEqual('t',logical(1)) W5Q3: Perform date conversions Description ---------------------------------------------------------- _MATLAB invented its own serial date format, as you can read about using |help datenum|. Wrap your heads around it! You will need to go back and forth often._ *A certain date and time are contained in the numeric variable* |bd1|. *Convert this number using* |datestr| *into a date string that evaluates to the format* |dd-mm-yyyy|, *and call your new variable* |bds1|. *A certain other date are giving to you as a string, in a variable named* |bds2|. *Convert this date to a numeric MATLAB date number, and store the result in a variable that you name* |bd2|. *Assuming a year has 365 days and 12 months, what is the difference between those dates, rounded to years? And that same difference, but instead, rounded to months? And that same difference, rounded to days? Call your results* |yearswiser|, |monthswiser|, *and* |dayswiser|. _Draw your own conclusions, but keep them to yourselves._ Solution Template ---------------------------------------------------- % The first date, as a MATLAB date number bd1=721206; % Convert it to a string in the requested format bds1= % The second date, as a user-generated date string bds2='Dec-30-1975'; % Convert it to a MATLAB date number bd2= % How much time between those dates to various rounded calendar units? yearswiser= monthswiser= dayswiser= Reference Solution --------------------------------------------------- bd1=721206; bds1=datestr(bd1,'dd-mm-yyyy'); bds2='Dec-30-1975'; bd2=datenum(bds2,'mmm-dd-yyyy'); yearswiser=round([bd2-bd1]/365); monthswiser=round([bd2-bd1]/365*12); dayswiser=round([bd2-bd1]); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution') assert(isequal(bds1,datestr(721206,'dd-mm-yyyy'))) assert(isequal(bd2,datenum('Dec-30-1975','mmm-dd-yyyy'))) assert(isequal(yearswiser,round([bd2-bd1]/365))) assert(isequal(monthswiser,round([bd2-bd1]/365*12))) assert(isequal(dayswiser,round([bd2-bd1]))) W5Q4: Generate a sine wave Description ---------------------------------------------------------- *A sequence of hours is contained in a vector* |h|. *Generate a sine wave that comprises the superposition of three specific periods* |P| *with three specific amplitudes* |A|, *oscillating about a specific mean value* |m|. *Collect the resulting superposition of sine waves in the vector* |v|. Solution Template ---------------------------------------------------- % Here are the hours being considered, do not change these h=0:1:100; % Here are the three amplitudes, do not change these A=[1 2 3]; % Here are the three periods, in the units of h, do not change P=[6 12 24]; % Here is the mean, do not change it m=13.5; % Now generate the superposition of sine waves v= Reference Solution --------------------------------------------------- h=0:1:100; A=[1 2 3]; P=[6 12 24]; m=13.5; v=m+A(:)'*sin(2*pi*h./P(:)); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution') assessVariableEqual('v',m+A(:)'*sin(2*pi*h./P(:)),'AbsoluteTolerance',1e-6) W5Q5: Fourier transform Description ---------------------------------------------------------- *A certain sequence of data is given to you as a set of independent and dependent variables. Using* |fft|, *perform a calculation to obtain the _periodogram_ at a set of* |nfft| *frequencies, and use the result to determine the* _period_ *at which the sequence shows the largest-amplitude periodicity. Collect that answer in the variable* |mper|. _Verify that your answer makes sense!_ Solution Template ---------------------------------------------------- % The sampling interval of a sequence, do not change xsint=1; % A certain sequence, independent variable, do not change h=1:xsint:6897; % A certain sequence, dependent variable, do not change v=[3 2 1]*sin(2*pi./[19 ; 33 ; 11]*h); % The number of points in the Fourier transform, do not change nfft=length(h); % The frequency axis, do not change selekt=[1:nfft/2+1]; fax=[selekt-1]/xsint/nfft; % The overcomplete periodogram at nfft frequencies (not windowed) vf= % The periodogram at the frequencies that matter, do not change vf=vf(selekt); % The index of the frequency with maximum periodogram value [~,ind]= % Use the index to determine the period corresponding to the maximum mper=; Reference Solution --------------------------------------------------- xsint=1; h=1:xsint:6897; v=[3 2 1]*sin(2*pi*h./[19 ; 33 ; 11]); nfft=length(h); selekt=[1:nfft/2+1]; fax=[selekt-1]/xsint/nfft; vf=abs(fft(v,nfft)).^2; vf=vf(selekt); [~,faxi]=max(vf); mper=1/fax(faxi); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution') assessVariableEqual('mper',19,'AbsoluteTolerance',1e-1)