CSE1560 FinSOL
CSE1560 FinSOL
CSE1560 FinSOL
CSE 1560
Sample Midterm
Wed. Mar. 30, 2014
1
Question 1. [40 points]
1. [5 points] What would the value of the following expression be if the prece-
dence was from right to left
1>2<=3
if precedence was from right to left:
2<=3 evaluates to 1 (true)
1>1 evaluates to 0 (false)
2. [5 points] How does one get the handle to the sine function in Matlab.
@sin
7. [5 points] What is the size of the vector returned by root(p) if p has size 5
4
2
8. [5 points] Give an example of an anonymous function.
Any of the following:
@(x)sin(x)/x
@(y)y-1
@(y)1
Question 2.
[60 points]
1. [10 points] What is the bug with the following Matlab code
function [ A ] = buggyfun(B)
%BUGGYFUN is buggy
B = inv(A)+1;
return;
end
A and B are switched
2. [10 points]
What is the bug with the following Matlab code
V = rand(1,10)
for i=1:10
V(i) = V(i-1)ˆ2;
end
When i==0 (first iteration) V(i-1) is V(0) and Matlab will report
an “array out of bounds” error.
3
3. [20 points]
Write a simple Matlab function named vecint that integrates a vector. The pro-
cedure accepts one argument, a column vector V and returns another vector. Ev-
ery element of the new vector is the sum of all previous elements in the original
vector.
function [ Vo ] = vecint( Vi )
% Comments not necessary
N = length(Vi);
Vo = zeros(N,1);
Vo(1) = Vi(1);
for i=2:N
Vo(i) = Vo(i-1)+Vi(i);
end;
end
4
4. [20 points]
Write a simple matlab procedure named mergevec that accepts two arguments,
both of them column vectors. These Vectors are assumed sorted and not necessar-
ily of equal size. The procedure returns another vector, with size equal to the sum
of the sizes of the two original vectors. The new vector contains the elements of
both original vectors sorted.
function [ M ] = mergevec( A, B )
Na = length(A);
Nb = length(B);
N = Na+Nb;
ia=1;
ib=1;
M = zeros(N,1);
for i=1:N
if (ia<=Na)&(ib<=Nb);
if A(ia)<=B(ib);
M(i) = A(ia);
ia = ia+1;
else
M(i) = B(ib);
ib = ib+1;
end;
elseif ia<=Na
M(i) = A(ia);
ia = ia+1;
else
M(i) = B(ib);
ib = ib+1;
end;
end