SUNY Geneseo Department of Mathematics

Lab 2—Vectors

Math 230 01
Fall 2015
Prof. Doug Baldwin

Complete by Wednesday, September 30
Grade by Tuesday, October 6

Purpose

This lesson begins to develop your ability to use vectors (lists of values) in Matlab. It also reinforces your ability to use Matlab functions.

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, this material in class between September 21 and September 30.

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

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.

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). (Hint: Matlab has a built-in function that will count the peaks for you).

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.

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π.

You might find it helpful to write a script for this part of the exercise. You are welcome, but not required, to do so.

Something very surprising should happen with the frequencies you calculate in this exercise. The surprise is a result of something called Nyquist’s Theorem. Find out what Nyquist’s Theorem says, and see if you can explain informally why it holds, based on your experience in this exercise.

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 a written solution to the exercise to your meeting, as that will speed the process along.

Sign up for a meeting via Google calendar. If you worked in a group on this exercise, the whole group should schedule a single meeting with me. Please make the meeting 15 minutes long, and schedule it to finish before the end of the “Grade By” date above.