W4Q1: Find a median Description ---------------------------------------------------------- *A vector* |v| *contains all the integers 1 through 100. A same-length vector* |w| *contains numbers that are equally spaced between 0 and 1. The element-by-element powers*, _v-to-the-wth-power_, *are contained in the vector* |vw|. *Calculate the value of the 50th percentile of* |vw|, *and assign the result to the variable* |vp|. Solution Template ---------------------------------------------------- % Make the first vector, the integers v % Make the second vector, the powers w % Make the third vector, the integers raised to the powers vw= % Calculate the requested percentile vp= Reference Solution --------------------------------------------------- v=1:100; w=linspace(0,1,length(v)); vw=v.^w; vp=median(vw); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution') assert(isequal(vw,[1:100].^linspace(0,1,100))) assessVariableEqual('vp',median(vw),'AbsoluteTolerance',1e-10) W4Q2: Make a histogram Description ---------------------------------------------------------- *The entire uppercase alphabet is contained in the positions 65 through 90 of the ASCII table, as can be verified by typing* |char(65:90)|. *A character string can be turned into its equivalent numeric positions in the ASCII table by using* |abs|. *For a specific character string* |v|, *calculate and assign its ASCII-positions to a variable named* |va|. *Use* |hist| *to count the numbers of characters whose ASCII-positions fall in each of 4 equally spaced containers, or* _bins,_ *assigning the result to a variable named* |vh|. *Return the largest number of counts thus obtained, and assign the result to the variable* |vl|. Solution Template ---------------------------------------------------- % The given character string, do not change the line below v='FREDERIK'; % The corresponding numeric entries into the ASCII table va=; % The counts of the ASCII values falling as divided over the requested bins vh=hist(); % The largest number of counts under this subdivision vl=; Reference Solution --------------------------------------------------- % The given character string, do not change the line below v='FREDERIK'; % The corresponding numeric entries into the ASCII table va=abs(v); % The counts of the ASCII values falling as divided over 4 bins vh=hist(va,4); % The largest number of counts under this binned subdivision vl=max(vh); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution'); assert(isequal(va,[70 82 69 68 69 82 73 75])) assert(isequal(vh,[4 2 0 2])) assert(isequal(vl,4)) W4Q3: Calculate a correlation Description ---------------------------------------------------------- *Use the function* |corrcoef| *to calculate the correlation coefficient between the two variables of a given data set, |x1| *and* |x2|. The output of* |corrcoef| *is a matrix, whose* _second_ *entry is the correlation coefficient that we are after, which you collect in a variable* |r|. _Note: in class we used_ |corr| _but this function is likely not available under Cody._ 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 what we want to know about the data R=; r=R(2); 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=corrcoef(x1,x2); r=R(2); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution') assessVariableEqual('r',0.9277,'AbsoluteTolerance',1e-4) W4Q4: Fit a polynomial Description ---------------------------------------------------------- *A data set of length* |N| *is given to you as two vectors* |x| *and* |y|. *Use* |polyfit| *to determine the coefficients of the polynomial of degree* |N-1| *that goes through the data points. Collect these coefficients in the vector* |P|, *in the standard ordering with the high-degree coefficients first.* _Do NOT use_ |polyval| _but, rather, explicitly_ *evaluate the polynomial for the _first_ data point, and collect the result in a variable named* |yval|. *How large is the error, defined as the absolute difference between what you were given at* |x(1)|, *namely* |y(1)|, *and what you have now predicted based on the polynomial fitting, i.e.* |yval|? *Calculate this error, the* _residual_, *and assign it to a variable* |r|. Solution Template ---------------------------------------------------- % Here are the data, do not touch the two lines below x=[ 1 2 3 4 5 6 7 8]; y=[70 82 69 68 69 82 73 75]; % Determine the coefficients of the polynomial going through x and y N=; P=polyfit(); % Evaluate the solution explicitly by completing/adjusting the below yval=P(1)*x(1)^7+P(2)*x(1)^6+ +P(8)*x(1)^0; % Calculate the absolute prediction error, the residual, at x(1), by % comparing with y(1) r=; Reference Solution --------------------------------------------------- x=[ 1 2 3 4 5 6 7 8]; y=[70 82 69 68 69 82 73 75]; N=length(x); P=polyfit(x,y,length(x)-1); yval=P(1)*x(1)^7+... P(2)*x(1)^6+... P(3)*x(1)^5+... P(4)*x(1)^4+... P(5)*x(1)^3+... P(6)*x(1)^2+... P(7)*x(1)^1+... P(8)*x(1)^0; r=abs(y(1)-yval); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution') assessVariableEqual('yval',70,'AbsoluteTolerance',1e-8) assessVariableEqual('r',0,'AbsoluteTolerance',1e-8) W4Q5: Evaluate a polynomial Description ---------------------------------------------------------- *A certain polynomial has coefficients arranged in the vector* |P|, *ordered with descending powers. Assign the polynomial degree to the variable* |Pd|. _Work with the variables! Do not just write this value explicitly._ *Use* |polyval| * to evaluate this polynomial at the integers 1 through 8, and collect the result in the vector* |Px|. *Round this number, add 64, and apply the function* |char| *to the result, which you assign to a variable* |Pc|.* _Look at the result before you submit!_ Solution Template ---------------------------------------------------- % Here are the polynomial coefficients - do not change them P=[17/280 -271/144 5743/240 -23209/144 49433/80 -12028/9 622691/420 -619]; % The polynomial degree Pd= % Evaluation points x= % The value of the polynomial at the requested points Px=polyval(); % The characters generated upon rounding Px and adding 64 Pc= Reference Solution --------------------------------------------------- P=[17/280 -271/144 5743/240 -23209/144 49433/80 -12028/9 622691/420 -619]; Pd=length(P)-1; x=1:8; Px=polyval(P,x); Pc=char(round(Px)+64); Visible Tests -------------------------------------------------------- Hidden Tests --------------------------------------------------------- %% run('solution'); assert(isequal(Pc,'FREDERIK'))