Department of Computer Science
Computer Science 101: Mid Term Exam I (60 minutes)
![]()
Name: NetID:
Lab Section: Date: October 2, 2008
![]()
No questions
will be answered during this examination.
If you do not understand a question, read it again. If you still do not understand it, make reasonable assumptions and write them
down. (Points will be deducted if unreasonable assumptions are made.)
DO NOT CHEAT: Cheating includes not only copying from another person but
also allowing someone to copy from you.
Anyone who copies or allows someone to copy will receive a score of
zero. So be defensive and protect your
work.
This examination contains 9 pages including this page. Check that your copy is complete, and ask for a replacement if it is not. Do all your work on these pages. For your own protection, in case pages come apart, write your NetID at the TOP of each page before beginning work.
Do not forget to sign the attendance list. If your exam is
misplaced and you did not sign the attendance list then you will receive a zero
score for the exam.
You may not use any electronic devices, book, notes or other
references during this examination.
Section |
Possible Score |
Deduction |
Grader |
|
10 |
|
|
|
|
2 |
6 |
|
|
|
3 |
6 |
|
|
|
4 |
8 |
|
|
|
5 |
6 |
|
|
|
6 |
8 |
|
|
|
7 |
8 |
|
|
|
8 |
8 |
|
|
|
9 |
6 |
|
|
|
10 |
8 |
|
|
|
11 |
6 |
|
|
|
12 |
6 |
|
|
|
13 |
9 |
|
|
|
14 |
8 |
|
|
|
15 |
9 |
|
|
|
16 |
13 |
|
|
|
Total |
125 |
|
|
|
|
|
Exam Scoreà |
|
A
1. Write the results Matlab returns for the sequence of commands typed in the Matlab command window:
a)
>> v = 3:2:8
v =
________________________________________________________________
b)
>> w = [3, 6, 9];
>> w2 = w + [5, 5, 5]
w2 =
_______________________________________________________________
c)
>> x = [8, 5, 9];
>> Dx = diff(x)
Dx =
_______________________________________________________________
d)
>> vec = [4, 5, 6, 7, 8, 9];
>> vec(5:end)
ans =
_______________________________________________________________
e)
>> z = linspace(1,11,3)
z =
_______________________________________________________________
2. The following commands are typed in a Matlab command window. Write the results Matlab returns:
a) >> vec =
[3, 6, 9];
>> mat
= [vec’ , vec’]
mat =
_____________________________________________________
b) >> mat = [ 1, 2, 3; 4, 5, 6];
>> sum(mat)
ans =
____________________________________________________
3. A Lissajous figure could be represented by the following equations.
x = cos(3t)
y = sin(2t)
Please fill each blank with one Matlab command to plot this figure using the Matlab plot function.
t = linspace(0, 2*pi, 1000);
x = ______________________________________________________________________
y = ______________________________________________________________________
_________________________ % plot in the x-y
plane
4. Using the built-in function quadl write a single Matlab command to compute the
definite integral of the function
![]()
on the interval
. (Don't write the answer, just the Matlab code that
gives the answer. Of course, quadl computes an approximate answer.)
_________________________________________________________________________
5. Write a single Matlab command using the built-in function linspace to get the same result as the following Matlab command:
-4 : 2 : 4
____________________________________________________________________
6.
Let A be a 3 x 4 matrix and B be a 4 x 3 matrix
that contain numerical values. Which of the following are valid operations,
that is, will not produce Matlab errors?
Circle the correct answers. There may be more than one correct answer.
A*B
A*B' A'*B
A'*B'
A.*B
A.*B' A'.*B A'.*B'
7.
Assume x is a
row vector with at least three values. Write a single Matlab command
that produces a row vector y which is the reverse of x except for the first and last values, which
are to remain in the same positions. For example, if x = [10, 4, 5, 8, 9, 7], then your command should produce y = [10, 9, 8, 5, 4, 7]. As a second example,
if x = [1, 2, 3, 4], then your command should
produce y = [1, 3, 2, 4]. Your code must work for any x that has three
or more values.
___________________________________________________________
8.
Assume that file fnc.m has the following
content:
function b = fnc(a)
c = 2;
b = (2 * a) + (4
* c);
and that the file scr.m has the
following content:
c = 2;
b = (2 * a) + (4
* c);
Answer both questions a) and b) below. Write down the output that Matlab displays after you type each of the following commands.
a)
>> clear
>> a = 2;
>> c = 4;
>> scr;
>> b
b =
________________________________________________________________
b)
>> clear
>> a = 2;
>> c = 4;
>> b = fnc(c);
>> b
b =
________________________________________________________________
9. Answer both questions a) and b) below.
a) Write a single Matlab command which creates a column vector called cool_vec that contains 100 equally spaced points between 0 and 2 (including both 0 and 2) .
>> cool_vec = _______________________________________________________
b) Write a single Matlab command that assigns to cool_vec the square of each element of cool_vec .
>> cool_vec = _______________________________________________________
10. Answer
both questions a) and b) below. Write down the output that Matlab displays
after you type each of the following commands.
>> Q = [1,2,3,4,5; 6,7,8,9,10;
11,12,13,14,15; 16,17,18,19,20; 21,22,23,24,25]
Q =
1 2
3 4 5
6 7
8 9 10
11 12
13 14
15
16 17
18 19 20
21 22
23 24 25
a)
>> v = [1 , 5];
>> Q(v, :)
ans =
__________________________________________________________
b) >> v = [2 , 4];
>> Q(:, v)
ans =
__________________________________________________________
11. Fill in the blanks to create the following matrix named bigmat.
>> bigmat = [ones(___) zeros(___ , ___); zeros(___, ___)
4*eye(___)]
bigmat =
1 1 1
0 0
1 1 1
0 0
1 1
1 0 0
0 0 0
4 0
0 0 0
0 4
12. Assume that the file dummy.m has the following content:
function res =
dummy(x)
global z
z = z + 1;
res = x + z;
Assume you type in the following commands at the Matlab prompt.
>> global z
>> z =
3;
>> x =
z;
>> x =
dummy(x);
>> x = dummy(x);
What is the value of x after these commands are executed? Please circle the correct answer. There is only one correct answer. (Note that the above is not a typo -- the function dummy is indeed called twice.)
a) 5
b) 7
c) 11
d) 12
e) 'res'
13. Write
down the output that Matlab displays after you type each of the following
commands.
>> a = [1, 2, 3];
>> b = [-1, -2];
>> c = [1, 1];
>> D = diag(a,0)+
diag(b,1)+ diag(c,-1)
D =
________________________________________________________
14. Given
that the file named myfunc.m contains the following code:
function y = myfunc(x)
y = x-7;
Fill in the blanks below with the correct Matlab responses. Assume that fzero finds the correct root.
>> global x
>> x = 0;
>> y = fzero(@myfunc, x)
y =
_______________________________________________________________________
>> x
x =
________________________________________________________________________
15. Transform
the following set of linear equations into a Matlab matrix and vector so that
you can solve the system of equations. Matrix "A" should contain the
coefficients of the equations and column vector "c" should
contain the right hand side of the equations.
2x - y + 2z = 6
x + 3y + z = 10
-4z + 4y + 3x = -1
>>A =
___________________________________________
>>c = ___________________________________________
Now write a single Matlab command to solve
the above system of equations. Do NOT write the solution, just the Matlab
command that would give the solution.
>>_________________________________________
16. Write a complete Matlab function named mysh where the function is described mathematically as,
The function mysh has one input parameter x which contains either a row or column vector or a matrix of numerical values. Hint: you must use the Matlab function exp. Do not include comments.