






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Practice Exam 1 This exam is open book and open notes. There are 5 problems on the exam worth a total of 100 points. The number of points for each problem is shown below. Try to do something on every problem, and show all of your work. Good luck! Name _________________________________ Problem 1 ______________ (15/100 points) Problem 2 ______________ (20/100 points) Problem 3 ______________ (20/100 points) Problem 4 ______________ (20/100 points) Problem 5 ______________ (25/100 points) TOTAL ______________
Problem 1: Debugging (20 points) The code below contains exactly five errors. For each error: (1) circle the error, (2) describe the nature of the error in your own words, and (3) write a modification of the code that “fixes” the error (there are multiple ways to fix each error – just make a change that allows the code to run without generating an error). You do not need to indicate the error message that MATLAB prints. Please keep in mind that you will lose credit for missing an existing error, and for marking something as an error that is not a real error, so make your choices carefully! n = input('What''s your ''''favorite'''' number? ') twiceYourFavoritePlusOne = 2n + 1 myFavorites = linspace(2,8,3) yourFavorites = 2:3: myFavorites(yourFavorites) yourFavorites(myFavorites) = 6 myFavoritesSquared = myFavorites * myFavorites if (rem(n,2) = 0) disp('even') else disp('odd') end ages4review = [19 7 4 2; 14 89 62; 2 3 2 12 ]
Problem 3: Displaying Plots (20 points) A MATLAB figure window is shown below. Write MATLAB code that would create the figure exactly as it is shown. Write your MATLAB code here:
Problem 4: Creating Pictures (20 points) You are given a MATLAB script called makeBlock.m that creates the 32x32 picture shown below on the left and stores it in a variable called block. You do not need to write the code to create block , you can simply use the block variable. Assume that the block matrix contains the values 0 (black), 0.5 (gray) and 1.0 (white). block bigBlock Write MATLAB code to produce the 128x128 image (above right), using block. Store the image in a variable called bigBlock. Use comments to explain your strategy as you write your code.
Part c: Computing Class Averages (11 points) Wendy Wellesley has a theory that computer science professors become easier graders as they get older. She has collected statistics from five introductory courses taught at Wellesley College by five different professors into the table shown below. 1 Course A B C NC Prof’s age CS100 8 7 1 0 55 CS110 25 57 33 2 40 CS111 9 12 4 1 33 CS112 12 2 0 0 55 CS114 3 6 3 0 38 Suppose the following statements are executed to create variables that store this information (note that ages is a column vector): gradesGiven = [ 8 7 1 0; 25 57 33 2; 9 12 4 1; 12 2 0 0; 3 6 3 0 ]; ages = [55; 40 ; 33 ; 55 ; 38]; courses = {'CS100' 'CS110' 'CS111' 'CS112' 'CS114'}; grades = {'A' 'B' 'C' 'NC'}; (1) In order to test her theory, Wendy would like to calculate the weighted average of grades assigned in each class, where an ‘A’ has a weight of 4, a ‘B’ has a weight of 3, a ‘C’ has a weight of 2, and an ‘NC’ has a weight of 0. For example, in the case of CS100, this weighted average would be 3.4, based on the following calculation: ((8 * 4) + (7 * 3) + (1 * 2) + (0 * 0))/16 (the weighted sum of the grades is divided by the total number of students in the class). Write a single MATLAB statement that assigns to the variable classAverages, a column vector containing the weighted average of the grades given in each of the five classes. (2) Write a single MATLAB expression that has the value “true” if there is at least one class with the highest grade point average that is taught by one of the oldest professor(s). Your expression may use the variable classAverages assigned in (1). 1 These data are, of course, totally fictitious
CS112 Computation for the Sciences
Problem 1: Debugging The following five errors occur in this code, and there are multiple ways to fix each one: (1) twiceYourFavoritePlusOne = 2n + 1 The multiplication operator in the expression 2n is missing – it should be written as 2n. (2) myFavorites(yourFavorites) The variable myFavorites, created by the expression linspace(2,8,3) is assigned to the vector [2 5 8]. The variable yourFavorites, created by the expression 2:3:6 is assigned to the vector [2 5]. When the vector [2 5] is supplied as indices for myFavorites, the index 5 is out of bounds for the myFavorites vector, which has only three elements. The following is one way to fix this error: myFavorites(yourFavorites(1)) (3) myFavoritesSquared = myFavorites * myFavorites This statement uses the matrix multiplication operator, but we cannot multiply two row vectors in this way, so the * operator should be replaced with the element-by-element multiplication operator, . (4) if (rem(n,2) = 0) When comparing two values in a conditional expression, the == operator must be used to test for equality, as in (rem(n,2) == 0). (5) ages4review = [19 7 4 2; 14 89 62; 2 3 2 12 ] The first and third rows of this matrix have 4 elements, whereas the middle row has only 3 elements, so there are inconsistent dimensions in the rows of the matrix. A fourth number should be added to the middle row. Problem 2: Simulating MATLAB The following printout is produced by the execution of these statements: nums = 3 6 9 2 4 6 1 2 3 mySums = 18 12 6 nums = 3 18 9 2 12 6 1 6 3 nums = 3 18 9 0 2 12 20 20 1 6 20 20 0 0 20 20
bigBlock(33:64,65:96) = block; % copy block with brightness flipped to upper right corner bigBlock(1:32,97:128) = 1 - block; Problem 5: Writing MATLAB Code Part a: Solving a Quadratic Equation x1 = (-cf(2) + sqrt(cf(2)^2 - 4cf(1)cf(3)))/(2cf(1)) x2 = (-cf(2) - sqrt(cf(2)^2 - 4cf(1)cf(3)))/(2cf(1)) Part b: Calculating Vapor Pressure T = 0:2: P = 10 .^(7.9622 + 0.05223*34.172./T) Part c: Calculating Class Averages (1) The following code statements show two ways to construct classAverages:
2gradesGiven(:,3)) ./ sum(gradesGiven,2) classAverages = (gradesGiven[4; 3; 2; 0]) ./ sum(gradesGiven,2) (2) sum((max(classAverages) == classAverages) & (max(ages) == ages)) > 0