2 Loops

Begin by creating a script named q2.m and copy the following Matlab code into the script:

% q2.m 

% do not change the variable t on the next line
t = linspace(0, 2 * pi, 500);

% (a) Answer part (a) below this comment



% (b) Answer part (b) below this comment



% (c) Answer part (c) below this comment 


(a) [5 marks]

Create a vector v whose elements are equal to:

\[ v = \cos(8t + \frac{\pi}{3}) \sin(t - \frac{\pi}{4}) \]

where \(t\) is the vector defined for you in the script. The vector v is a simple example of amplitude modulation.

If you cannot correctly compute the vector v then set v to the vector having elements 1, 2, 3, ..., 500 so that you can attempt parts (b) and (c).

If you plot v versus t you should get the following:

(b) [5 marks]

Create a vector w of all zeros that has the same size as v.

Write a loop that sets the element at index i of w to the value

\[ {v_{n-i+1}} \]

where \(v_{n-i+1}\) is the element at index n-i+1 of v, and n is equal to the number of elements in v. This elements of w should be equal to the elements of v in reverse order.

You must use a loop to solve this problem. You may use any function that determines the length or size of an array and the function zeros. No other functions are allowed.

Hint

  • w(1) is equal to v(end)
  • w(2) is equal to v(end - 1)
  • w(3) is equal to v(end - 2)
  • and so on

(c) [10 marks]

Notice that in the figure shown in part (a) the value of the function \(v\) oscillates; the value of the \(v\) repeatedly goes up and down between its minimum and maximum values. This particular function repeatedly crosses the line \(v = 0\). The index of v where the function crosses the line \(v = 0\) is called a zero crossing.

A zero crossing occurs when the sign of of an element in v is different from the previous element in v. For example, the value of v(6) is negative and the value of v(7) is positive; because the sign of v(7) is different than the sign of v(6) a zero crossing occurs at index 7.

Similarly, the value of v(37) is positive and the value of v(38) is negative; because the sign of v(38) is different than the sign of v(37) a zero crossing occurs at index 38.

Note that no value in v is exactly equal to zero.

Write a loop that finds the locations (indexes) of all of the zero crossings in v. Store the vector of values in a variable named zc.

You must use a loop and an if (or if-else or if-elseif) statement to solve this problem. You may use any function that determines the length or size of an array. No other functions are allowed.

Hint

The first two zero crossings are 7 and 38. There are 18 zero crossings in total.