




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!





Page 1 of 7 E7 Spring 2009 Midterm I SID:_________________
UNIVERSITY OF CALIFORNIA, BERKELEY Department of Civil and Environmental Engineering
50 minutes – 50 pts
Question Points Grade
1 4
2 5
3 6
4 16
5 7
(^6) 12
Total 50
Notes (a) Write your name and your SID on the top right corner of EVERY page (including the first page) (b) Full credit will be awarded only if the correct answer is given in the box provided. Partial credit may be awarded with respect to the work you show outside the boxes (c) You may not leave the exam room before the exam ends (d) No calculator / computer is allowed (e) You may have one sheet of notes written on both sides
Please circle your LAB Section:
UNIVERSITY OF CALIFORNIA, BERKELEY Engineering 7 – Spring 2008
Consider the following equation:
2
1 2
Where G is a constant equal to 6.673x10 -11, and m1, m2, and r are all arrays of size n by 1. Circle the answer to the following questions about writing this equation in MatLab.
(a) What is the correct way to calculate the array F?
A) F = Gm1m2/r^ B) F = Gm1.m2/rr C) F = Gm1.m2./r^ D) F = Gm1.m2./r.^* E) F = G(m1.m2)./r.*r
(b) Now assume that F is calculated in the command window. What is true about the dimensions (size) of F?
A) F will be a column vector B) F will be a row array C) F will be a scalar value D) F will be a square matrix E) Not enough information to tell
(c) Assuming that G, m1, m2, and r are scalar numbers, then class of F is:
A) char B) string C) double D) struct E) cell
(d) If G, m1, m2, and r are now only scalar numbers, then the following code will correctly calculate the value of F:
F = Gm1m2/r^
TRUE FALSE
UNIVERSITY OF CALIFORNIA, BERKELEY Engineering 7 – Spring 2008
The following MATLAB commands are saved in a script file called examQuestion3a.m and examQuestion3b.m What is the value of x when the script file examQuestion3a.m is executed? What is the value of n and counter when examQuestion3b.m is executed? Do not worry about the exact format of the output.
Script file examQuestion3a.m
x = 0; for i = 1: for j = 1: if i == j x = x + 1; end end end
x = 1000
Script file examQuestion3b.m
n = 256; counter = 1;
while n ~= 1 if rem(counter,2) == 0 n = n/2; else n = n/4; end
counter = counter + 1 end
n = 1
counter = 6
UNIVERSITY OF CALIFORNIA, BERKELEY Engineering 7 – Spring 2008
Consider the following 5 lines of MatLab code entered in the MATLAB command window:
X = [7 7 7 7]; Y = 6:9; W = 3; Z = 2; V = 8;
What is the output when the following commands are typed in sequence from the MATLAB command window? Do not worry about the exact format of the output.
V ~= X
ans = 1 1 1 1
ans = 0 0 1 0
ans = 10.0000 10.5000 11.0000 11.
ans = 12
ans = 5
ans = 2 6 10
a = [W,Z,V,Z] [a’,X’]
UNIVERSITY OF CALIFORNIA, BERKELEY Engineering 7 – Spring 2008
Create a function in MatLab called myButterfly which accepts an array of numbers (t) as input. The corresponding array of X and Y coordinates should be the output, as described by the equations above. The declaration line of the function should look like:
function [X,Y] = myButterfly(t)
Write the remainder of the myButterfly.m here:
function [X,Y] = myButterfly(t)
X = sin(t).(exp(cos(t)) – 2cos(4*t) – sin(7/12).^5);
Y = cos(t).(exp(cos(t)) – 2cos(4*t) – sin(7/12).^5);
Plot your function for t = 0 to 12π with an increment of 0.01π. No additional plotting parameters are needed (e.g. axis, grid, line style, plot color). This plot should go in the bottom right corner of a [2 x 2] subplot. You may use up to 3 lines of code.
[X,Y] = myButterfly(t);
subplot(2,2,4), plot(X,Y)
UNIVERSITY OF CALIFORNIA, BERKELEY Engineering 7 – Spring 2008
Consider the following arrays defined in MatLab’s workspace:
Retrieve the following sub-matrices from W and Z in a single line of MATLAB code:
Retrieve or create from W the following sub-arrays:
W([2,4],[1,3])
Retrieve or create from Z the following sub-arrays:
[Z(2,3:-1:1);Z(1,:)]
Retrieve or create from W and Z the following sub-arrays:
[Z(:,1:2), W(2:3,2:3)]