SUNY Geneseo Department of Mathematics

Lab 2—Vectors

Math 230 02
Spring 2016
Prof. Doug Baldwin

Complete by Friday, February 12
Grade by Wednesday, February 17

Purpose

This lesson begins to develop your ability to use vectors (lists of values) in Matlab, in particular your ability to create vectors, and to map computations over them. This lab also gives you practice writing and using a user-defined function.

Background

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

Section 4.3 of our text discusses functions, and we talked about them in class on February 3.

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( samples, range )

where samples is a vector of samples, such as generated in Step 3, and range is the total number of radians covered by the samples in samples (range would be 2π for the wave from Step 3). 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 length of the interval sampled over (i.e., by the range argument).

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.) Your copy of Matlab should include the signal processing toolbox, although in the past I have seen copies at Geneseo that don't. If this becomes a problem this semester, I will either provide you a substitute findpeaks function, or use some class time to show you how to write one, depending on how quickly the rest of the lab is going.

Step 5

Finally, use your frequency function from Step 4 to calculate the frequencies of sets of 60 samples from the function sin(6x) over an increasing number of radians. In particular, generate sample vectors for x ranging from 0 to 2π, 4π, 8π, and 16π.

Something very surprising should happen with the frequencies you calculate in this exercise. 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.