clear echo on %Solution to Quiz #2 %We have the heights and weights (using cut and paste!) data=[70 205 71 235 73 192 74 220 75 295 74 248 73 240 74 210 75 301 77 290 73 195 72 195 74 215 76 235 76 280 77 297 78 242 75 235 68 160 75 260 74 247 75 227 75 230 70 188 74 211 71 190 74 195 76 235 75 283 75 207 71 190 70 230 73 185 70 186 77 250 74 218 72 198 74 275 71 245 72 172 76 285 71 187 76 225 75 200 76 283 69 180 77 290 79 289 74 225 76 290 76 215 74 205 74 238 76 215 73 239 74 210 74 191 74 245 72 300 80 303 75 350 77 297 76 290 74 227 73 246 71 185 72 235 72 232 76 245 69 175 74 210 71 198 72 218 77 295 69 165 77 292 78 265 73 176 70 180 76 245 70 177 74 243 74 232 72 195 75 234 76 208 74 214 73 215 74 225 77 351 72 290 78 236 72 190 74 245 69 175 71 196 75 251 75 210 75 198 72 190 75 310 76 240 76 298 73 285 74 305 72 210 70 178]; ht=data(:,1); wt=data(:,2); %Question 1: the BMI... bmi=703*wt./ht.^2; n=length(ht); avebmi=sum(bmi)/n stdevbmi=norm(bmi-avebmi)/(n-1)^.5 %Which is another way to get it... %You will note that (by clinical definition) the average bmi of a football player is %borderline obese (BMI of 30) although they are obviously in good shape. The extra %weight is still tough on the heart, though - even if it is muscle rather than fat. %Recent studies suggest that while the link between BMI and cardiovascular diseases %isn't perfect (no surprise) there are still some correlations even for athletes. %Question 2: The mean heights and weights... aveht=sum(ht)/n avewt=sum(wt)/n %Question 3: The variances and covariance. We'll do this as a matrix of covariance. %Let (1) be ht and (2) be wt... covariance=zeros(2,2); %We intialize the matrix for i=1:n covariance=covariance+[(ht(i)-aveht)^2,(ht(i)-aveht)*(wt(i)-avewt);(ht(i)-aveht)*(wt(i)-avewt),(wt(i)-avewt)^2]; echo off end echo on covariance=covariance/(n-1) %We finish it off... %Note that the covariance (off diagonal elements) is positive but not all that large. %For perfect covariance it would be the product of the square roots of the diagonal %elements. If they were negatively covarying, the off-diagonal element would have %been negative. %We can also do this one using the built in matlab command "cov": cov2=cov(ht,wt) %Which yields the same matrix... %Question 4: Using error propagation to get the bmi standard deviation... %The calculated bmi is easy: calcbmi=703*avewt/aveht^2 %The calculated standard deviation is a little trickier. We need the gradient in %the bmi with respect to wt and ht (we do it analytically): gradbmi=[-2*calcbmi/aveht,calcbmi/avewt] %and thus the standard deviation: calcstdevbmi=(gradbmi*covariance*gradbmi')^.5 %We compare to the directly computed values: ratiobmi=calcbmi/avebmi ratiostdev=calcstdevbmi/stdevbmi %Which is pretty close... They aren't exact because we had a non-linear function. %Question 5: We repeat the solution to question 4, but now we take the %covariance to be zero. This is easily done by using the "diag" command %twice. The first time it turns the diagonal of the matrix into a vector, %and the second time it turns it back into a diagonal matrix: newstdev=(gradbmi*diag(diag(covariance))*gradbmi')^.5 newratio=newstdev/stdevbmi %As you can see, you get a much larger standard deviation. That is because %you are ignoring the large covariance in heights and weights. echo off