SUNY Geneseo Department of Mathematics

Lesson 2—Vectors

Math 230 01
Fall 2014
Prof. Doug Baldwin

Complete by Wednesday, September 24
Grade by Monday, September 29

Purpose

This lesson develops your ability to use vectors (lists of values) in Matlab. It also introduces some other Matlab features, notably some of the logical operators and scripts.

Background

Vectors (along with matrices, i.e., tables of values) are generally described in chapter 2 of Attaway’s book. Sections 2.1.1, 2.1.4, 2.2, 2.3, and 2.5 are particularly relevant to this lesson.

Section 1.6 of Attaway discusses relational and logical operations, and section 3.2 introduces scripts.

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

Activity

Use Matlab to solve each of the following problems:

Problem 1—Grading

Here is a simplified description of how I track and calculate students’ grades (I do it in a spreadsheet rather than with Matlab though): For each student, I record a vector of grades. For example, if a student has grades of 8 points, 6 points, and 8 points on homework assignments 1, 2, and 3, and 50 points on exam 1, their vector would be [8 6 8 50]. The things I grade come in “categories,” for example homework is one category, exams another, etc. (the main reason for this is that different categories have different weights for calculating final grades, but that’s not part of this problem). Assume that categories are represented by numbers, for example homework is category 1, exams category 2, etc. To associate categories with individual grades, I use a second vector to record each grade’s category. For example, for the earlier vector containing 3 homework grades and an exam grade, the category vector would be [1 1 1 2].

A hypothetical course is graded based on 5 homework assignments (with category 1), 2 hour exams (category 2), 1 final (category 3) and class participation (category 4). Create a Matlab vector C that records the categories for these grades, and a vector G that stores the grades for a student who received grades of 10, 6, 8, 8, and 9 on the homeworks, 96 and 100 on the hour exams, 95 on the final, and 4 for class participation.

Write a Matlab expression that finds the maximum of this student’s grades.

Write a Matlab expression that produces a Boolean vector (i.e., a vector of logical values) that contains True in every position corresponding to a 2 in vector C (the category vector), and False for every other position in C.

Use your expression from the preceding step to pick out of the G vector a new vector that contains just the hour exam grades. Write a Matlab expression that computes the student’s average hour exam grade.

Problem 2—Distances

Suppose you live in a densely built up city, in which you can only move around by walking east-west and north-south along streets. You want to move 100 feet west and then 200 feet north from your current location. What is the shortest distance you must walk in order to do this? (Assume that you are standing on a street, and that there is an intersection conveniently located 100 feet west of you).

Densely built up cities typically have tall buildings with elevators in them, so you can also move up and down. Suppose that after you have gone 100 feet west and 200 feet north in the problem above, you also need to go 50 feet down. Calculate the total distance you need to move.

Now imagine you live in a 6-dimensional alternate universe, but one that still has densely built up cities with street grids (presumably in 5 dimensions) and elevators, just like above. You need to move 100 feet west, 200 feet north, 50 feet down, and then 60 feet, 150 feet, and 300 feet, respectively, in the remaining dimensions. What is the total distance you move?

(In the above problems, you have been calculating what is sometimes called “Manhattan distance”—in reference to a certain densely built up city—between your starting point and ending point.)

Now assume that you live in an open countryside where you can move in a straight line between any 2 points in 2, 3, or 6 dimensions. You have a jet pack, flying broomstick, or whatever other device you would like to imagine that lets you move off the ground as needed. Calculate the shortest distances you need to travel in each of the above three scenarios under this relaxed set of constraints.

(You are now calculating what is sometimes called the “Euclidean distance” in 2, 3, or 6 dimensions between points.)

How would you generalize the calculations you did above to universes with very large numbers of dimensions, say 100 or 1000 or more?

Problem 3—Functions and Sampling

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.

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

(The preceding was a warm-up to introduce the idea of using vectors as tables of sampled functions. Now the interesting part starts….)

Write a Matlab expression that creates a vector holding 60 equally spaced values between 0 and 2π (inclusive). It will be useful later if you define a variable holding the upper bound for this interval (i.e., 2π for now) and write the expression to generate the 60 equally spaced values in terms of this variable.

Write another Matlab expression that creates a second vector that holds the values of the function sin(6x), where the x values come from the vector created in the previous step. Note that another way of thinking about this vector is that it contains 60 samples of sin(6x), where the samples are taken at the values in the vector from the previous step.

Write a third Matlab expression that counts the number of cycles in the table of sin(6x) values. You can do this by counting the number of “peaks” in the table, where a peak is defined to be a value that is greater than the values immediately before and after it. Hint: Matlab has a built-in function that will do most of this calculation for you.

Write a fourth Matlab expression that calculates the frequency of the sampled sin(6x) wave. The frequency is simply the number of cycles (computed by the third expression) divided by the length of the interval you sampled over. Since the interval is implicitly measured in radians (because that’s the unit Matlab’s sin function works in), your frequency will be in units of cycles per radian. (As a test of your work, the function sin(6x) goes through 6 complete cycles between 0 and 2π, so you should have 6 cycles, and a frequency of slightly under 1 cycle per radian.)

Once you have written and tested the four expressions described above, write a script based on them that receives a number k as its input (probably via a Matlab variable), generates a vector containing 60 samples from the function sin(6x) for x values equally spaced between x = 0 and x = k, and outputs the frequency of the sampled wave. Run your script on k = 4π, k = 6π, and k = 8π.

There should be something very surprising in 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.