SUNY Geneseo Department of Computer Science
CSci 120, Spring 2014
Prof. Doug Baldwin
Complete by Friday, February 21
The ability to plot graphs in two and three dimensions is one of the most powerful tools for understanding data and functions in Matlab and other mathematical programming languages. A solid understanding of how to create graphs is thus essential to being a capable mathematical programmer.
The basic knowledge for this lesson comes from the following readings and mini-lectures:
The last of these mini-lectures introduces a way of describing mathematical functions as “parametric equations.” The basic idea is that instead of thinking of the points on a graph as pairs (x,y) where y is some function of x (i.e., y = f(x)), think of them as pairs where x and y are both functions of some third variable, t (the “parameter”). Any curve you can describe in the form y = f(x), can be described by the pair of parametric equations x(t) = t and y(t) = f(t). But parametric equations can also describe a great many curves that can’t be described in the form y = f(x), in particular curves that fold back on themselves, cross over themselves, etc. Furthermore, parametric equations embody a notion of the “direction” of a curve, i.e., where it is going as t increases. These features make parametric equations very useful in mathematics, computer graphics, the physical sciences, etc. In some sense all plotting in Matlab is parametric, because you describe a plot by giving a set of (x,y) pairs that the desired line passes through, and the x and y values can be independently calculated if you wish.
Write Matlab scripts to solve the following problems.
Plot the function y = 4 - x2 for -3 ≤ x ≤ 3.
To visually see what was happening in Lesson 3 when you sampled (and undersampled) a periodic function, plot the following three curves on a single graph but in different colors:
Note that the first of these curves is one you worked with in Lesson 3 without any errors, the second is one that had errors due to aliasing, and the third is essentially a “perfect” reference for what the others should look like.
Format your graph to look nice, or at least be understandable (hint: it might be easier to see what is going on if you limit your graph to only show part of the complete curves).
What curve do you suppose the parametric equations
x(t) = cos(t)
y(t) = sin(t)
produce as t ranges from 0 to 2π? Try to come up with a guess in your study group, then write a Matlab script that plots the curve and see if you were right. (Hint: the curve will be easiest to recognize if your plot has square axes—i.e., 1 unit on the X axis is the same length on screen as 1 unit on the Y axis.)
A family of curves called “rose curves” are defined by parametric equations of the form
x(t) = k cos(t) cos(nt)
y(t) = k sin(t) cos(nt)
where k and n are constants that adjust the appearance of the curve. k is a scaling factor that adjusts the overall size of the curve; n will be explored below.
Write a Matlab script that draws a rose curve with k = 1 and n = 3. Vary t from 0 to 2π in several hundred steps (adjust the number of steps until the curve looks smooth). Now try several other n values. What do you observe about how the value of n affects the curve?
What happens if n is not a whole number? When n is not a whole number, how do you have to change the interval t ranges over in order to get the end of the curve to meet up with the beginning? Can you describe the relationship between n and the upper bound on t mathematically?
I don’t really expect you to do the following, but you are welcome to explore these if you finish the above exercises and want more to do.
Another family of simple parameteric equations are the “Lissajous curves,” defined by equations of the form
x(t) = sin( k1t + c )
y(t) = sin( k2t )
where k1, k2, and c are constants that affect the shape of the curve. Write a Matlab script that draws Lissajous curves, and experiment with the effects of different values of k1, k2, and c.
The Spirograph (legally a trademark of Hasbro) toy produces very elaborate curves that are technically known as hypotrochoids and epitrochoids. Not surprisingly, these curves can also be described by parametric equations. See if you can derive parametric equations for spirograph-like curves and implement them in Matlab. If you don’t want to derive your own equations, the Wikipedia article on Spirograph has a derivation you can study.
Finally, see what artistic effects you can produce with the various families of curve introduced in this lesson. Consider plotting multiple curves, in multiple colors, on the same graph, etc.
In your study group’s first face-to-face meeting with me following the “Complete By” date above, I will look over your solutions to the problems, ask you any further questions I have, and answer any questions from you. I may also ask you to demonstrate some of your answers in Matlab. Please bring short written solutions to each problem to the meeting (diary
output from Matlab showing you solving the problems would be one way to do this). This will speed the meeting along, but because we will also be talking face-to-face, the written answers don’t have to be long or elaborate.
If you aren’t already signed up for a study group meeting during the week following this recitation, please sign up. Make the meeting 15 minutes long, and try to make it at a time all members of your study group can attend. If no such time exists, make it at a time the greatest number of members can attend.