Unformatted text preview: University of California, Berkeley Summer Session 2010 College of Engineering George Anwar E7 Midterm Examination II Name : SID : Friday July 30, 2010 Lab Section: 1 11:00 – 1:00 2 1:00 – 3:00 (Please mark the session you attend) Problem Points Grade 1 3 2 2 3 5 4 10 5 10 6 15 7 20 8 25 9 20 10 35 Total 145 1. Write your name on each page 2. Open notes and Open Book exam 3. No computers or cell phones allowed (Please turn off your cell phone) E7 Midterm I, Summer 2010 NAME : 1. The following MATLAB statements are executed: Begin code >> p=0; q=0; r=0; >>for s=1:2:3, p=p+1; for j=1:s, q=q+1; end,end; >>disp([p,q]); End code Write the displayed output that the last command will produce. 2 4 . 2. Given wt = [2, 5, 8, 3, 6, 9, 10, ‐3, ‐5, 4] ; What will the result of num after MATLAB executes the following statements: Begin code End code Write the displayed output that the last command will produce. 6 . num = 0; for I = 0:length(wt) if (wt <= 5 ) num = num + 1; end end disp(num) Page 2 of 9 E7 Midterm I, Summer 2010 NAME : 3. Write a single MATLAB statement that does exactly the same as the code in Problem 2. Begin code disp(sum(wt<=5)) or disp(length(find(wt<=5)) ) End code 4. A MATLAB programmer needed to write a program to compute B = sum(A) and C = sum(A’). Where A is a predefined mxn matrix. The programmer’s result is as follows: Begin code [ m,n] = size(A); for j = 1:1:n B(j) = sum(A(j,:); end for j = 1:1:n C(j) = sum(A(:,j); end End code Comment on the code, did the programmer succeed? no If not correct the code [ m,n] = size(A); for j = 1:1:n B(j) = sum(A(:,j)); end for j = 1:1:m C(j) = sum(A(:,j)); end Page 3 of 9 E7 Midterm I, Summer 2010 NAME : 5. a. From the figure and information above, which model fits the data the best (Circle the correct choice) (1) A Linear Model (3) A quadratic Model (2) A Cubic Model (4) A non‐polynomial Model b. Assume that the data set above is defined by x_data and y_data. Both of these vectors are of equal length. You decided that a 5th order polynomial may be a best fit. Write a single line MATLAB statement to find the coefficients of the polynomial and assign it to a vector P Page 4 of 9 Begin code P=polyfit(x_data,y_data,5); End code c. What would be the result of length(P) given P from b. 6 E7 Midterm I, Summer 2010 NAME : 6. Recall that the total squared error of a model is defined as: where is the y value estimated by your polynomial model for the data points and . Given the data is stored in arrays x_data and y_data, where a single line code to calculate E defined above. Begin code E = sum((y_data – polyval(polyfit(x_data,y_data,n),x_data)) ^2); 7. Given the function: 12 2 Write a MATLAB script to plot as a function of , for ranging from m to n with an increment of s. Plot each data point as a red circle and the line connecting the points as a blue dash line. Title the plot as ‘Midterm Problem 7’. Label the horizontal axis as ‘X‐ data’ and label the vertical axis as ‘Y‐data’. Bound both the x and y axis to be minimum value – p and maximum value + q. ( You do not need to generate the plot, just write the script) Begin code X = m:s:n; Y = ‐2.*X.^7+12.*X.^9; plot(x,y,’ro’,x,y,’b—‘); title(‘Midterm Problem 7’); xlabel(‘X‐data’); ylabel(‘Y‐data’); axis([m‐p n+q min(Y)‐p max(Y)+q]); End code Page 5 of 9 End code E7 Midterm I, Summer 2010 NAME : 8. Complete the recursive MATLAB function sfrac below, which evaluates from the following equation: 1 1 2 … where is an array of length , containing only positive elements. Begin code function y = sfrac(x) n = length(x); if n == 2 %complete the condition for n y = x(2); %fill in the code else y = x(n) + 1/sfrac(x(1:n‐1)) %fill in the code end Page 6 of 9 1 1 1 2 E7 Midterm I, Summer 2010 NAME : 9. a. Write a single line MATLAB code to generate a vector y of size m with a standard deviation of sigma and a mean of p. Begin code y = sigma.*randm(m,1) + p; End code b. Write a single line MATLAB code to generate a vector y of size m between –p and q. Begin code y = abs(p+q).*rand(m,1) – p; End code Page 7 of 9 E7 Midterm I, Summer 2010 NAME : 10. Write a MATLAB function to generate an array of size n with a random sequence of non repeating integer ranging from 1 to n. Example: n=5 n=9 result = [5 3 4 1 2] result = [8 7 2 5 4 1 9 6 3] (Note the sequence is random) function sequence = nonrepeatsequence(n) x = rand(1,n); [B sequence] = sort(x); end Page 8 of 9 E7 Midterm I, Summer 2010 NAME : Page 9 of 9 ...
View Full Document
- Summer '08