Notes 03: Vectorized Operations

Reading: Gilat 3.1-3.6


One great advantage of MATLAB is the ease with which you can support simultaneous computations across a vector using vectorized operations. We looked at some of these during classtime- and if you get stumped, take a peek at our solutions.

Exercise i)
Construct a vector with contents $[\,\, 100 \,\, 200 \,\, 300 \,\, \ldots \,\, 1500 \,\, ]$.

Exercise ii)
Construct a vector with contents $[ \,\, 1 \,\, 2 \,\, 3 \,\, \ldots \,\, 18 \,\, 19 \,\, 20 \,\, 19 \,\, 18 \,\, \ldots \,\, 3 \,\, 2 \,\, 1 \,\, ]$.

Exercise iii)
Construct a vector containing n entries with the alternating pattern $[ \,\, 1 \,\, -\!\!1 \,\, 1 \,\, -\!\!1 \,\, 1 \,\, \ldots \,\, ]$. Can you think of several ways to do this? Do your formulations work for both odd and even-lengthed vectors?

Exercise iv)
Construct a vector containing the first n powers of 2, starting with $2^0$, i.e., $[ \,\, 2^0 \,\, 2^1 \,\,2^2 \,\, \ldots
\,\, 2^{n-1} \,\, ]$.

Exercise v)
Construct a vector containing the inverse of the first n powers of 2, i.e., $[ \,\, \frac{1}{2^0} \,\, \frac{1}{2^1} \,\,\frac{1}{2^2} \,\, \ldots
\,\, \frac{1}{2^{n-1}} \,\, ]$.

Exercise vi)
As a classic example of a geometric series, we have that

\begin{displaymath}\sum_{k=0}^{\infty} \frac{1}{2^k} = 2\end{displaymath}

Although we cannot perform an infinite calculation, we can observe how the limit is approaching $2$ if we evaluate a finite portion of that sum.

One way to do this is to build a series as in the previous problem for a particular $n$, and to use the sum function to cmpute the sum. This could be repeated for several values of $n$. An easier way to see the progression is to rely instead on the cumsum function. For a vector v, the call sum(v) returns a scalar that is the sum of all entries. In constrast, the call cumsum(v) returns a vector with length equal to v, where the kth entry of the result is the sum of the first k entries of v (thus the last entry of the result the sum of all entries).

If we let v denote the vector from the previous problem for $n =
10$, examine the results of cumsum(v).

Exercise vii)
The constant $e$ is not predefined, but I thought we may as well learn a way to estimate it ourself. It can be shown that

\begin{displaymath}e = \sum_{k=0}^{\infty} \frac{1}{k!}\end{displaymath}

Although, we cannot precisely evaluate an infinite sum, we can create a decent approximation to $e$ by evaluating the sum of the first $N$ terms of this series.

Using cumsum as in the previous problem, compute a vector approxE that has approximations of $e$ based on the first $k$ terms of the infinite series, for $k$ ranging from 1 to 20. Then compute another vector named errors by subtracting exp(1) from all entries so that what remains is the difference between our approximation and MATLAB's computation of $e$.

Exercise viii)
Much as there was a way to express $e$ as an infinite series, the value of the natural log of 2 can be expressed as

\begin{displaymath}\ln 2 = \sum_{k=1}^{\infty} \frac{1}{(2k-1) \cdot (2k)}\end{displaymath}

Create a script that approximates the value by summing the first $N$ terms of the series and then test the results by evaluating the expression yourApprox - log(2), which represents the difference between your approximation and MATLAB's calculation of the value. How does this difference vary with the choice of $N$?

Exercise ix)
In some contexts MATLAB represents a polynomial by storing its coefficents as a sequence in a vector. For example, the polynomial $p(x) = 3x^3 - 5x + 2$ would be represented as the vector $[ \,\, 3 \,\, 0 \,\, -\!\!5 \,\, 2]$ (notice the 0 coefficient to designate the term $0x^2$).

There is a library function polyval(p,x) that computes $p(x)$ given such a vector $p$ and scalar $x$. However, we can perform the evaluation ourself through use of vectorized operations. Give an expression or a series of commands to compute $p(x)$ given $p$ and $x$.

Exercise x)
MATLAB provides a function rand that produces a matrix of pseudo-random numbers that are uniformly distributed between 0 and 1. This means that a particular random number should have a 10% chance of being less than or equal to $0.1$.

Let's test this empirically. Assuming that variable $N$ represents the desired number of trials, develop an expression that computes the percentage of $N$ random numbers that are less than or equal to $0.1$. Once you have your formula developed, test it by hand on values of $N$ such as $100$, $1000$, $10000$, $\ldots$.

Exercise xi)
MATLAB also provides a function randn that produces a matrix of pseudo-random numbers that follow the standard normal distribution (i.e., the ``bell curve''). These have a mean of $0$ and a variance of $1$. Theoretically, the chance of a single such random number being greater than or equal to $1$ is approximately 15.8655%.

Let's test this empirically. Assuming that variable $N$ represents the desired number of trials, develop an expression that computes the percentage of $N$ normally distributed random numbers that are greater than or equal to $1$. Once you have your formula developed, test it by hand on values of $N$ such as $100$, $1000$, $10000$, $\ldots$.