Midterm Exam with solutions - Boston straubin/scicomp_2011/midterm2011_with_solutions. · Midterm Exam…
Post on 18-Aug-2018
Embed Size (px)
Midterm Exam with solutions
CS227-Introduction to Scientific Computation
November 8, 2011
1. The following is a transcript of a MATLAB session.
>> x=1/62.5x = 0.016000000000000>> y=(1+x)-1y = 0.016000000000000>> y==xans = 0>> xx=1/64xx = 0.015625000000000>> yy=(1+xx)-1yy = 0.015625000000000>> yy==xxans =1
There are three things for you to explain here:(a) (5 points) The expressions x and (1+x)-1 are mathematically equivalent,but in the comparison y==x, MATLAB reports that they are unequal. Explaincarefully and succinctly why this is the case.Solution. 1/62.5 is not represented exactly in binary so has an infinite binaryexpansion. Its representation by x is accurate to within 52 bits, but when weadd 1, some of those bits are lost. Thus when we subtract 1, we get the value of1/62.5 with some of the bits removed. (This is the same principle as catastrophiccancellation, but not quite as big a catastrophe.)(b) (5 points) Nonetheless, when asked to print the values of these two expressionsin long decimal format, MATLAB gives the same result 0.016000000000000.Why did this happen?
Solution. MATLAB is only displaying the results rounded to fourteen significantdecimal digits, which is about 46 bits. So even though the values are different,they round the same.(c) (5 points) When the experiment is repeated with xx=1/64 in place of 1/62.5,MATLAB now reports that xx and (1+xx)-xx are equal. What is different inthis case?Solution. 1/64 = 26 is exactly representable in binary (and most of the bits ofthe representation are zero). So no precision is lost when passing to 1 + 26 orupon subtraction.
2. This problem concerns the MATLAB function ht displayed below.
function vec = ht( c,t )
result=floor(2*rand(1,c));sumresult = sum(result);vec(sumresult+1)=vec(sumresult+1)+1;
Suppose you call this function with the command
(a) (5 points) What kind of object is v? That is, what are its dimensions, and whatis its possible range of values? (To answer this second question, you might wantto look at (b) below).
Solution. v is a vector with 6 rows (you can see this from the initialization of thevariable vec in the code). The answer below shows that every entry of v is aninteger between 0 and 30, inclusive.(b) (5 points) Suppose you then typed the command
What would the response be? (This can be answered without understanding whatthe first line within the for loop is actually doing!)
Solution. The response is 30. At each pass through the loop, one component ofvec is incremented.(c) (5 points) Suppose you then typed the command
What would you expect to see as the response?
Solution. Unless you have already used a variable called vec in the CommandWindow, you should expect an error message about an undefined variable. This isbecause the name vec is private to the function.
(d) (5 points) Finally, lets address what this function is actually doing. Yes, itcalls the function rand(), so it is performing some kind of random simulation.What is it simulating? What is the significance of the entries in v in terms of thissimulation?
Solution. It is simulating 30 tosses of 5 coins and counting, for each j between0 and 5, the number of tosses that resulted in exactly j heads. (More generally, ttosses of c coins.) In fact, the jth entry of v is the number of tosses that resultedin exactly j 1 headsthis is because we are obliged to begin the indexing at 1rather than 0.
3. (5 points) What is the result of typing the commands below?
>> m = [3 1 -1;6 4 8; 0 0 1]>> [l u p]=lu(m)
Just to be clear here, the function lu called in this fashion with a matrix M as aninput argument and with three output arguments [L U P] returns three matricessatisfying
PM = LU.
Solution. Here is what MATLAB prints.
1.0000 0 00.5000 1.0000 0
0 0 1.0000
6 4 80 -1 -50 0 1
0 1 01 0 00 0 1
A single row interchange, switching the first two rows, is performed becauseof the partial pivoting policy: always bring the largest element in a column to theupper left. This is reflected in the matrix p. A single row operation, subtracting1/2 times the first row from the second, is performed, which already gives anupper-triangular matrix. The matrix l is the inverse of this row operation.
4. You are working with a collection of student exam grades. The grades areon a scale between 0 and 100, and you want to estimate the grade distributiongiven the first, second, and third quartile grades. The first quartile is the gradeq1 such that 25% of the students scored q1 or below (i.e., it is the 25th percentilegrade); the second quartile is the grade q2 such that 50% of the students scoredq2 or below (i.e., q2 is the median grade), and, likewise, q3 is the 75th percentilegrade.
You try to estimate the number of students who score any grade x or belowby interpolating the points (0, 0), (q1, 0.25), (q2, 0.5), (q3, 0.75), (100, 1) with aspline curve. The nice result for q1 = 50, q2 = 73, q3 = 89, is shown in Figure 1.(a)(10 points) Write a sequence of MATLAB commands (not a function) that takesvalues q1, q2, q3 and plots this spline distribution, highlighting the quartile points,as shown.Solution. This sequence of commands works, although as (b) indicates, the choiceof the interpolation method is misguided.
x=[0 q1 q2 q3 100];y=linspace(0,1,5);xx=linspace(0,100,200);yy=interp1(x,y,xx,spline);plot(x,y,ro,xx,yy);plot(xx,yy2);
(b)(5 points) Unfortunately, certain choices of quartile values lead to pictures likeFigure 2. This was created using q1 = 40, q2 = 60, and q3 = 70. It makes nosense as a grade distribution, since it suggests, among other things, that 120% ofthe students received a grade of 90 or less! What is a very simple thing we can doto fix this problem?
Solution. Choosing linear or pchip interpolating gets rid of the problem. Us-ing pchip gives smoother interpolation, but both of them give strictly increasingdistributions.
5. If we use Newtons Method to compute the root of x3a, we get the followingiteration:
given some appropriate choice of x0. We try to implement this method with thefollowing function, which is supposed to return both the computed cube root andthe number of iterations performed.
function [x numiter]=cuberoot(a)x=a;numiter =0;while x3= a
(a) (10 points) The function contains several errors, one of which causes it tonever terminate for some values, even when the sequence of iterates really doesconverge, and the other of which causes it to return an incorrect number of itera-tions. Describe how to correct these errors.Solution. One problem is trying to find the root exactly. Typically this will not bepossible. One correction is to look for only an approximate match, say to get theresidual to within 1010 a. We could do this with
while abs(x3-a)> (1e-10)* a
The second problem is that numiter is incremented outside the loop, so thatit always will return with the value 1. It should be moved before the end of thewhile statement.(b) (5 points) To answer this question, you might want to consult the graph ofx3 a, which is pictured below. Assume a > 0. Does the function, as written,
always converge? What if we used a different initial guess? For how many initialguesses does the algorithm fail to converge, and where are these guesses located?Solution. For a > 0 Newtons method always converges. If a > 1 then thetangent lines approach the cube root. If a < 1, the first tangent line lands to theright of the root, and then again approaches from the left. This is clear from thedirection of concavity of the function.
If we use an initial guess of x0 = 0, then the tangent line is horizontal, so themethod does not converge. But it also fails to converge if we choose x0 = b1 sothat the tangent line passes through (0, 0). You can tell from the graph that theremust be such a point (it actually occurs at b1 = (a/2)
13 . As a result, it will fails
to converge if we choose x0 = b2 < b1 so that the tangent line at b2 passes through(b1, 0). Proceeding in this fashion we find infinitely many negative values of theinitial guess for which the method fails to converge.
Figure 1: Grade Distribution from Splines
Figure 2: Ridiculous Grade Distribution from Splines
Figure 3: Graph of f(x) = x3 3.