EECS1560 test 1_Sample (1)
EECS1560 test 1_Sample (1)
York University
Department of Electrical Engineering & Computer Science
Instructions:
Print your name and student ID# at the top of each other page.
Show your work to receive maximum credit. Partial credit will be given, but only if work is
shown.
If we cannot read your hand writing, we cannot give you credit.
Circle, mark, or otherwise indicate your answer to each question.
Please keep your written answers brief and to the point. Incorrect or rambling statements
can hurt your score on a question.
If the question requires you to provide code, you must use correct MATLAB syntax and
semantics to receive full credit. We should be able to type your answer into MATLAB and
verify that it works.
No calculator, electronic devices, books, notes, or cheat sheets are allowed in the test
Total: 40
1
Name:____________________________ Student #:______________________
1. Determine the output for each of the following MATLAB instructions: [10]
a) round(-3.1) [1]
c) x = 10:-2:3; [2]
x(3) = [];
find(x(rem(x,4)==0))
d) n = 3; [2]
vec = n:2^n;
vec(vec > n+1)
[2]
2
Name:____________________________ Student #:______________________
3 4 5 6 7
A
13 14 15 16 17
c) Choose the correct answer choice that, when replacing the blank below, produces the values for
the variable named result shown below. Hint: note that result displays the x values that
correspond to the smallest value in y. [1]
3
Name:____________________________ Student #:______________________
d) You wish to write a function that will plot the following function: [4]
Write a m-file for the function that is named fcn1.m. Choose a proper time-step. Include the labels.
10 1 9 18
A 6 2 0 8
3 24 12 4
a) Determine the output for each of the following 2 cases. Explain each case. [4]
I. A(:,1)*A(:,2)
II. A(:,1).*A(:,1)
b) Using a single MATLAB statement, show how to interchange the first and third columns
in A. [2]
c) Using a single MATLAB statement, show how to determine the total number of integers
that are divisible by 4 in A. [2]
4
Name:____________________________ Student #:______________________
4. Write the screen display in the command window in the space provided for the following script
and associated functions. Show all the steps. Credit only given for work shown. [5]
% script
clc; clear;
x = 5
y = 2
z = [0 6 5 3 1];
for i = 1:2:5
if (i <= 2)
back1 = fcn1(i,x,y,z)
Display Display
elseif (i > 3)
#
disp('done')
else 1
back2 = fcn2(i,y,x,z)
end 2
end
3
5
Name:____________________________ Student #:______________________
5. Write a script that will print the following multiplication table: [3]
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
6. Write a function called “vec2sqr” that will receive two row vectors (v1 and v2) as input arguments,
and from them create and return a square matrix in which the first two rows are v1 and v2 and the rest
are zeros. You may not assume that the length of the vectors is known. An error message will be
displayed in the command window if the length of v1 and v2 are different.
[6]
Example 1
>> v1=[1 2 3 4];
>> v2=[5 6 7 8];
>> vec2mat(v1,v2)
ans =
1 2 3 4
5 6 7 8
0 0 0 0
0 0 0 0
Example 2
6
Name:____________________________ Student #:______________________
abs()
atan()
axis()
ceil()
cos()
cosd()
disp()
exp()
find()
fix()
floor()
fprintf()
grid
inv()
inf
legend()
length()
linspace()
log()
log10()
max()
min()
mod()
norm()
ones()
pi
plot()
prod()
rand
randi()
rem()
round()
rref()
sin()
sind()
size()
sqrt()
sum()
tan()
tand()
title()
xlabel()
ylabel()
zeros()