SUNY Geneseo Department of Mathematics

Lab 2—Vectors

Math 230 02
Spring 2017
Prof. Doug Baldwin

Complete by Wednesday, February 8
Grade by Monday, February 13

Purpose

This exercise begins to develop your ability to use vectors in Matlab, in particular your ability to create vectors, to map computations over them, and to use them in larger programs. This exercise also introduces you to writing Matlab scripts.

Background

Vectors are generally described in chapter 4 of our textbook. Sections 4.1 through 4.7 are the ones most relevant to this exercise. We discussed, or will discuss, these sections in class on February 1 and February 3. We will discuss some advanced material on vectors that you may find interesting but not essential to this lab on February 8.

This exercise also asks you to write something called a script, which is somewhat like a very simple function. Section 3.1 of our text discusses scripts, and I expect to talk briefly about them in class on February 6.

Activity

In the following steps, you will explore the implications of using a vector as a table of “samples” of values from a function. The first steps are warm-up and practice with the basic idea; the remaining steps introduce one of its more surprising consequences.

Write your solutions to all steps except step 4 in a script file, with each step in a separate section of the script. Step 4 specifically asks you to write a function, and so its solution will have to be in its own file.

Step 1

Write a Matlab expression that creates a vector that serves as a “logarithm table” for the numbers 1 through 10. In other words, the vector should contain the logarithms (base 10) of the integers from 1 through 10, with element i of the vector holding log10i.

Step 2

Write a similar Matlab expression that creates a vector that serves as a (base ten) logarithm table for 901 equally-spaced numbers between 1 and 10 (the numbers should end up being 1, 1.01, 1.02, … 9.98, 9.99, and 10). You can think of this table as a a series of “samples” of the logarithm function over the interval [1,10].

Step 3

Write a Matlab expression (or group of expressions) that creates a vector holding 60 equally spaced samples of the function sin(6x), where x is a variable that ranges from 0 to 2π, inclusive. In other words, create 60 numbers equally spaced between 0 and 2π, then calculate the sine of 6 times each number.

Step 4

Write a function that calculates the frequency of a wave such as the one sampled in Step 3. Your function should have a header such as

function f = frequency( wave )

where wave is a vector of samples, such as generated in Step 3. Assume that the samples in wave represent values of some function over the interval [0,2π]. The result of your function, f, should be the frequency of the wave in cycles per radian.

You can compute frequency by first counting the number of peaks (i.e., places where a value is greater than the values immediately before and after itself) in the samples vector. Then divide the number of peaks by the number of radians in the interval sampled over (i.e., by 2π in this exercise).

To help you test your frequency function, note that the function sin(6x) goes through 6 complete cycles between 0 and 2π, so your frequency function should yield a frequency of slightly under 1 cycle per radian when applied to the vector built in Step 3. But be careful that your function must work for any vector of samples from a periodic function, not just for the one computed in Step 3.

Note that Matlab’s “signal processing toolbox” provides a function named findpeaks that will help you find peaks in a sampled waveform. (A “toolbox” is an add-on package of functions for Matlab; many are available for almost any imaginable application. Geneseo’s Matlab should have the signal processing toolbox already installed.) If you call findpeaks in a statement such as

peaks = findpeaks( wave );
where wave is a sampled function, variable peaks will end up containing a vector of the peak values (i.e., values that are larger than their neighbors) from wave. But beware that if you don’t use the result from findpeaks (e.g., store it into a variable as above, immediately use it in another calculation, etc.), findpeaks will draw a plot of the wave with its peaks marked and not return anything.

Step 5

Finally, use your frequency function from Step 4 to calculate the frequencies of sets of 60 samples from the functions sin(6x), sin(12x), sin(24x), and sin(48x), all sampled over the interval [0,2π].

Notice that each function in the above list has twice the frequency of the function before it, which should help you predict the values your frequency function should return. But also be warned that something very surprising will happen as the frequency increases far enough. The surprise is a result of something called Nyquist’s Theorem, which we will discuss in class at the end of this lab (you are welcome to research Nyquist’s Theorem on your own before we discuss it, of course).

Follow-Up

I will grade this exercise in a face-to-face meeting with you. During this meeting I will look at your solution, ask you any questions I have about it, answer questions you have, etc. Please bring your solution on your computer, as that will speed the process along.

Sign up for a meeting via Google calendar. Please make the meeting 15 minutes long, and schedule it to finish before the end of the “Grade By” date above.