SUNY Geneseo Department of Mathematics

Lab 2—Vectors

Math 230 02
Spring 2015
Prof. Doug Baldwin

Complete by Monday, February 9
Grade by Thursday, February 12

Purpose

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

Background

Vectors (along with matrices, i.e., tables of values) are generally described in chapter 2 of Attaway’s book. Sections 2.1, 2.2, and 2.3 are particularly relevant to this lesson. We discussed, or will discuss, this material in class on February 4 and February 6.

Scripts are discussed in section 3.2 of Attaway’s book. We will talk about them in class on February 9.

Matlab features useful in this lesson are also covered in the Edinburgh video lectures, particularly…

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 “square root table” for the numbers 1 through 10. In other words, the vector should contain the square roots of the integers from 1 through 10, with element i of the vector holding √i.

Step 2

Write a similar Matlab expression that creates a vector that serves as a square root 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 square root 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 and 2π, inclusive. In other words, create 60 numbers equally-spaced between 0 and 2π, then calculate the sine of 6 times each number. It will be convenient later if you use a variable to hold the upper bound (i.e., 2π for now) on the numbers input to the sin(6x) calculation.

Step 4

Write another expression, or group of expressions, that calculates the frequency of the wave sampled in step 3. You can do this by counting the number of peaks (i.e., places where a value is greater than the values immediately before and after itself) in the vector of sin(6x) values. Then divide the number of peaks by the length of the interval you sampled over (i.e., 2π). (Hint: Matlab has a built-in function that will count the peaks for you.)

As a test of your work in step 3, note that the function sin(6x) goes through 6 complete cycles between 0 and 2π, so you should count 6 cycles, and a frequency of slightly under 1 cycle per radian, in this step.

Step 5

Once you have written and tested the expressions for steps 3 and 4, write a script based on them that begins by initializing a variable k, then generating a vector containing 60 samples from the function sin(6x) for values of x equally spaced between x = 0 and x = k, and finally outputing the frequency of the sampled wave. Run your script for k = 4π, k = 8π, and k = 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. Find out what Nyquist’s Theorem says, and see if you can explain informally why it holds, based on your experience in this exercise. (Warning: this lab will be more fun if you don’t look up Nyquist’s Theorem until after you have finished the programming part of the lab.)

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.