CS 1132 lab exercise 2

You have until Wednesday, September 15, at 2:30pm (before next week’s lab) to complete this exercise on Matlab Grader.

Multiples of k

Let k be a positive integer smaller than 100. Write a colon expression on the blank below so that the vector v stores all the multiples of k up to and including 100.

k= floor(rand*99)+1;  % a random integer in [1..99]

v= _____________________________________ ;

Approximate cosine

Write a function approxCos(x,n) to approximate and return cos(x) by summing the first n+1 terms of this series:

$$ \sum_{k=0}^{n}{\frac{(-1)^{k} x^{2k}}{(2k)!}} $$

The sum converges to cos(x) as n → ∞. You can use built-in function factorial(), e.g., factorial(h) returns the value of h! . Check the Matlab documentation for further information on factorial if you like.

Hint: Use the “accumulation pattern”:

accumulator = initialValue
for k = 1:numberOfTerms
    accumulator = accumulator + value_of_kth_term
end

The above block is only an example pattern (algorithm), not code. Your actual code, including the number of iterations, may be different.

Searching within a vector

Write a function vectorQuery(v,n,r) to determine whether the number r appears in the first n components of vector v. The function returns true if r is in the first n components of v and returns false otherwise. Your function assumes that v is a vector of numbers, n is a positive integer, and r is a number. Use a while-loop to do the search. (Do not use built-in function find() or vectorized code.) Make sure that the loop index doesn’t go “out of bounds” (if n is greater than the length of vector v). Try to be efficient: the loop should end as soon as r is found. Do not use for-loops, break, continue, or return.

Creating arrays of unknown length

Write a function sequence(m) that generates a sequence of random integer numbers between 1 and m, inclusive, stopping when a value is repeated for the first time. The function returns an array containing all the numbers generated (in the order in which they were generated) except for the last value that is a repeated occurrence.

Example: If the generated sequence is 3 1 9 5 7 2 5, the array to be returned should be 3 1 9 5 7 2.

For this question, use this expression to generate a random integer in [1..m]: floor(rand()*m) + 1. Do not use function randi(). Use a while-loop since this problem is a case of indefinite iteration—the number of iterations needed is not known in advance.

Hints: (1) Use the function vectorQuery() that you have developed already. (2) When you don’t know how long a vector needs to be, you can build it one component at a time. Here is an example to store only the positive, even integer values that a user enters:

% Prompt user to enter positive integers nd store the even integers in a vector v

% METHOD 1:  KEEP A COUNTER AND USE IT AS THE VECTOR INDEX, see variable k
k= 0;  % vector length so far
num= input('Enter a positive integer: ');
while num > 0
    if rem(num,2) == 0
        k= k + 1;   % Increment counter k
        v(k)= num;  % Assign to the kth position of vector v
    end
    num= input('Enter a positive integer (negative to stop): ');
end

% METHOD 2:  CONCATENATE NEW ELEMENT TO EXISTING VECTOR
v= [];  % initialize empty vector
num= input('Enter a positive number: ');
while num > 0
    if rem(num,2) == 0
        v= [v num];  % Concatenation: build a vector with the existing v and the user-entered num
    end
    num= input('Enter a positive number (negative to stop): ');
end

Be sure to log off the lab computer before leaving the lab.