This preview shows pages 1–6. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: E77Midtenn2/Name: Page 1 off) UNIVERSITY OF CALIFORNIA, COLLEGE OF ENGINEERING E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS Second Midterm Exam — April 7, 2004 Question Points B 10 CONFIDENTIAL . Notes: 1.Write your name below and on the top left comer of every page. 2.Verify that you have all 6 pages of the test. 3.Please give all your answers only in the spaces provided.
4.You may NOT ask any questions during the exam. 5.Please no cell phones, calculators, or talking during the exam.
6.Please do not leave your seat before the end of the exam. Your NAME: Your STUDENT ID: Your SIGNATURE: SECTION 10r2 (circle your Lecture Section #) Circle your Lab Section (where the graded midterms will be returned next week) #lleW 810 #122MW1012 #13:MW24 #14:MW46
Etcheveny Etcheverry Etcheverry Etcheverry . #15:TuTh810 #16: TuTh 1012 #17zTuTh122 , #18:TuTh2~4
Etchéverry Etcheverry Etcheven'y ‘ Etcheverry F #19: TuTh 46 #20: MW 1012 #2]: MW 24 #22: MW 46
. Etcheverry Latimer Latimer Latimer E77Midterm2/N ame: Pag620f6
A.l. (5 Points) Determine the values of variables 5 and t after execution of the following MATLAB
statements:
>> A = [1 O 2];
>> B = [3 4];
>> s = O; t = 0;
>> for m = 1:2
8 = s + t;
for n = 1:3
t = t + A(n)*B(m);
end
end
>> s
S ::
>> t
t = A2. (5 Points) Given the following MATLAB code >> i = 1; j = >> while i < 5
i = i + 1;
while j < 3 1; 2:1; 2 = [z i+j] % Note: Output from this line is not
% suppressed by a semicolon
3 = J + 1,
end
end When this code is run, what is printed to the screen? Record your answer below. Add more lines as
needed. E77Midterm2/Name: Page 3 of 6 B. (10 Points) Given a set of 3 linear algebraic equations with 3 unknowns, —6x1+x2 +2.7C3—l 1 =0 . x1—5x2+2x3=0 2x1+2x2 —7X3 =0
(a) Write the above equations in matrix form Ax = b: (b) Write MATLAB statements to create the necessary arrays:
>> A = >>b (c) Write a MATLAB statement that solves the matrix equation that you have developed in part (b): o —»—— ll E77Midterm2/Name: Page 4 of 6 Cl. (4 Points) Two relationships to evaluate the sum of the ﬁrst n odd integers are: Sn=l+3+5+...+(2n—l) and 8,, = Sm} + (2n1) . Using the recursive relationship above, ﬁnish the recursive function TOT below so that it calculates the
sum of the ﬁrst n odd integers. function total = TOT(n) if C2. (4 Points) The Tribonacci numbers are deﬁned using the recurrence relation Tn: Tnl + Til2 + Tit3 . where T1 = 1, T2 = 1, T3 = 2. Use the above relationship to complete the iterative function TRIBO so that
it returns an array of the ﬁrst n Tribonacci numbers. The function should work for n 2 4. function t = TRIBO(n) E77Midterm2/Name: Page 5 of 6 C3. (6 Points) Consider the following tree structure of letters: B/LN
E/ \F / G (a) Write the sequence of letters that results from the preorder traversal of this tree (b) Write the sequence of letters that results from the postorder traversal of this tree (0) List the leaves in this tree E77Midterm2/Name: Page 6 of 6
D.l. (2 Points) Check the boxes of all those MATLAB statements below that ﬁnd one of more roots of the 3 equation x + x =1 as written: ;_]>> find(x"3 + x = 1)
[3» roots([l 0 1 —1])
Li>> fzero('x“3 + x — l',[O.l 1]) J» eval('x"3 + x — 1',0.5) D2. (9 Points) Complete the following MATLAB function that solves the equation x3
given tolerance using Newton’s method: function root midterm2_newton(x0,tol) %This function calculates the root of the equation x“3 + x = 1
%using Newton’s method % Inputs: x0 — the initial guess % tol — the error tolerance % Output: root — the calculated root of the equation xold = delta_x = while delta_x xold = end root = +x=1towithina ...
View Full
Document
 Spring '07
 Hutchings

Click to edit the document details