Parametric Equations in Matlab
Rather than express `y` as a function of `x`, it is often advantageous to express `x` and `y` in terms of another variable such as `t`, called a parameter. What follows is called a set of parametric equations.
$\begin{eqnarray} x&=&|t|\\ y&=&|1-t| \end{eqnarray}$
First, arbitrarily choose some `t`-values in a domain common to each function. Place your values of `t` in a table, then calculate the value of `x` and `y` for each value of the parameter `t`, placing your results in the appropriate columns of your table as your work. For example, if `t = -5`, then
$\begin{eqnarray} x&=&|-5|=5\\ y&=&|1-(-5)|=6 \end{eqnarray}$
Which we've recorded in Table 1, along with the corresponding `x` and `y`-values when `t = -4`, `-3`, ... , 5.
| `t` | `x` | `y` | `t` | `x` | `y` |
| `-5` | `5` | `6` | `1` | `1` | `0` |
| `-4` | `4` | `5` | `3` | `3` | `1` |
| `-3` | `3` | `4` | `3` | `3` | `2` |
| `-2` | `2` | `3` | `4` | `4` | `3` |
| `-1` | `1` | `2` | `5` | `5` | `4` |
| `0` | `0` | `1` |
Points satisfying `x=|t|` and `y=|1-t|`.
This sort of work is tediously done by hand, but easily carried out in Matlab. First, note that if no increment is provided in Matlab's start:increment:stop structure, then Matlab assumes an increment of 1.
>> t=-5:5
t =
-5 -4 -3 -2 -1 0 1 2 3 4 5
We next calculate the corresponding values of `x` and `y`.
>> x=abs(t),y=abs(1-t)
x =
5 4 3 2 1 0 1 2 3 4 5
y =
6 5 4 3 2 1 0 1 2 3 4
Note that these values are identical to those found in Table 1.
Of course, we usually want to plot a lot more points, especially since Matlab handles this chore with ease. The command
>> t=linspace(-5,5);
produces 100 equally spaced points from `-5` to 5, including both `-5` and 5 in the list. We've suppressed the output because of the large size of this list, but you should probably remove the semi-colons and examine the output carefully. The general form of the linspace command is linspace(a,b,N), which produces `N` equally spaced points from `a` to `b`, including `a` and `b` in its output. If you don't provide a value for `N`, as in our linspace(-5,5), Matlab defaults to 100 equally spaced points. Calculate `x` and `y` with
>> x=abs(t);y=abs(1-t);
At this point, you have three choices of plots: `x` versus `t`, `y` versus `t`, and `y` versus `x`. The commands
plot(t,x)
xlabel('t-axis')
ylabel('x-axis')
title('x versus t')
will produce the image shown in Figure 1.

A plot of `x` versus `t`.
The commands
plot(t,y)
xlabel('t-axis')
ylabel('y-axis')
title('y versus t')
will produce the image shown in Figure 2.

A plot of `y` versus `t`.
And finally, the commands
plot(x,y)
xlabel('x-axis')
ylabel('y-axis')
title('y versus x')
will produce the image shown in Figure 3.

A plot of `y` versus `x`.
Script Files
It won't be long until you find yourself typing dozens of commands at the Matlab prompt to produce a single plot. It is not uncommon in this situation to make a mistake, necessitating that you retype a sequence of commands. Even with the use of the up-arrow and Matlab's command history window to replay commands, you'll soon find this is an inefficient way to work with Matlab.
The best way to work with Matlab is with script files and functions. A script file is simply a text file that contains a complete list of the commands that you intend to type at the Matlab prompt. Perhaps an example will make this clear.
You can use any editor you wish to create your script file. Just remember to save your file in ASCII format with extension .m. However, Matlab contains a built-in editor with a number of features dear to the hearts of programmers. The easiest way to open this editor is to type edit at the Matlab prompt, but you can also: (1), select New->M-file from the File menu, or (2), click the "New M-file" icon on the Matlab toolbar. Once the editor opens, type the following lines in the editor, exactly as they appear.
close all
t=linspace(-5,5);
x=abs(t); y=abs(1-t);
subplot(131)
plot(t,x)
xlabel('t-axis')
ylabel('x-axis')
title('xversus t')
axis square
subplot(132)
plot(t,y)
xlabel('t-axis')
ylabel('y-axis')
title('y versus t')
axis square
subplot(133)
plot(x,y)
xlabel('x-axis')
ylabel('y-axis')
title('y versus x')
axis square
Select Save from the File menu of the editor and save the file as myfile.m. Return to the Matlab prompt and type
>> myfile
and all of the commands in the file myfile.m are executed in sequence, just as if you typed them sequentially at the Matlab command prompt. The script file myfile.m produces the image in Figure 4.

Plots of `x` versus `t`, `y` versus `t`, and `y` versus `x`.
Alternatively, while in the Matlab editor, you can run your script by selecting Run from the Debug menu. You can also use the accelerator key F5. Pressing F5 automatically saves your changes before running your script. This is probably the most efficient way to work: make a few changes to your script file, then press F5 to save and run the script.
A few new commands in the script file myfile.m warrant some explanation. First of all, the command close all closes all open figure windows. Also new is the subplot command, which allows the user to draw multiple plots in a single figure window. In general, the command subplot(m,n,p) creates a matrix of plots, `m` rows and `n` columns, and makes the `p` th axis active, counting from left to right by rows. For example, the command subplot(132) divides the current figure window into one row and three columns, placing an axes at each position, and dictates that the second axes (the one in row one, column two) is the current axes. All subsequent drawing commands are made on this axes.
The Current Directory and Path
Once you start saving script files, then you have to understand how Matlab searches for files on your hard disk or network. Two phrases are key to this understanding: (1), the Matlab path, and (2), the current working directory. We assume that you have some familiarity with the directory structure on your hard disk and/or network.
When you enter a script file name at the Matlab prompt, the first place Matlab searches for this file is the "current working directory." You can determine the current working directory by entering
>> pwd ans = /Users/darnold/classes/trunk/math50c/matlab/parametric
In this case, Matlab will first search in the directory or folder /Users/darnold/classes/trunk/math50c/matlab/parametric on your computer's hard drive. If you did not save the file myfile.m in this directory, then Matlab will probably be unable to find your file, responding with
>> myfile ??? Undefined function or variable 'myfile'.
Cryptic messages like this can cause quite a bit of panic on the part of the user; but, if you think about it, the file is truly undefined if Matlab cannot find it.
So, how do you get yourself out of this mess? That is, what can you do so that Matlab knows where to find your file? You have a lot of options, but we will share only a few.
If you saved myfile.m in the directory c:\mywork, one obvious option is to change the current directory to c:\mywork. You can do this at Matlab's command line.
>> cd c:\mywork >> pwd
The command cd c:\mywork changes the current directory to c:\mywork.14 The second command, pwd, stands for "present working directory," which responds with the current directory. It's always good to use pwd to check the current directory.
A second way to change the current working directory is to use the navigation bar in the Matlab interface. There is a drop down arrow which will provide a list of all the most recently visited folders and directories on your computer. To the right of the drop down list is an icon with three little dots. If you click this, a dialog box will open which will allow you to browse to a folder in the usual manner for your operating system. Once you select a new folder, note that the location is now indicated in the navigation box. If you use pwd to check the current directory, the result should match what is in the navigation box.
Sometimes you have folders of "special" M-files that you find you are constantly using during in your work. For example, you might find that you frequently us John Polking's Multigraf program. Perhaps you might find that you also use dfield and pplane from Dr. Polking's page quite frequently. In this case, it is probably best to create a folder named polking on your system, then download all of these files into this folder. Once the download is complete, you will want to add the polking directory to Matlab's "path."
After searching the current directory, Matlab begins searching directories in the order listed on Matlab's path. You can type path at the Matlab prompt and Matlab will list the all of the directories on its path. Order is important. If you have two files named myfile.m, then the one executed is the one in the directory that occurs earliest on the path.
So, how do you add your your polking directory to the path? There are a number of ways to do this, but the easiest is to use the pathtool. Open the pathtool by (1), typing pathtool at the Matlab prompt, or (2), selecting Set Path from Matlab's File menu. In the Set Path dialog box, click the Add to Path icon and browse to the folder you wish to add to the path. When first learning the intricacies of Matlab's path, it's probably best to click "Move to Top", which moves your folder to the front of the path (first to be searched). If you wish to make your path changes permanent, click Save before closing the Set Path dialog box.
Now, regardless of the "current working directory," the files in the polking directory will always be found. If you've downloaded multigraf.m into the polking directory, regardless of the current working directory, typeing multigraf will run the multigraph.m script.
Comet Plots
Matlab has animation capability that enables you to examine the plot of a set of parametric equations as it is drawn in real time. Let's animate the plot of a Lissajous curve, shown in Figure 5.

A plot of a Lissajous curve.
Save the following sequence of commands in a script file named lissajous.m.
close all t=linspace(0,2*pi,4000); x=cos(5*t); y=sin(7*t); comet(x,y)
Execute the script file by typing lissajous at the Matlab prompt (or simply press F5 in Matlab's editor).
Clearly, the command comet(x,y) is similar to the plot command, but it animates the plot in real time. Two commands in the script file lissajous warrant further comment. The close all command close all open figure windows, clearing the desktop for "fresh start" before opening a new figure window with the comet command. Secondly, we've created 4,000 equally spaced time points with the command t=linspace(0,2*pi,4000). You can slow the animation further by adding more time points (or speed things up by adding fewer time points). Experiment with the linspace command to find the optimum setting for your personal workstation.
If you intend to do anything with the final image, it's best to redraw it with the plot command as the comet command saves only the last point plotted in its routine for further use. Try resizing the figure window containing the Lissajous plot with your mouse to see what we mean.
Multigraf
It won't take long for you to realize that we are wasting a lot of paper printing dozens of Matlab plots. Dr. John Polking of Rice University has written a nice routine called multigraf which helps to save a lot of paper. This routine is already present on the campus server, so typing multigraf at the Matlab prompt is all you need to do to start the routine. Once the multigraf figure window opens, its use is pretty self explanatory.
If you would like to use this routine at home, vistit Dr. Polking's site at Rice and download multigraf.
Matlab Files
Although the following file features advanced use of Matlab, we include it here for those interested in discovering how we generated the images for this activity. You can download the Matlab file at the following link. Download the file to a directory or folder on your system.
The file parametric.m is designed to be run in "cell mode." Open the file parametric.m in the Matlab editor, then enable cell mode from the Cell Menu. After that, use the entries on the Cell Menu or the icons on the toolbar to execute the code in the cells provided in the file. There are options for executing both single and multiple cells. After executing a cell, examine the contents of your folder and note that a PNG file was generated by executing the cell.
Exercises
In each of exercises 1-6, `x` and `y` are defined in terms of a parameter t on a given interval. Use Matlab's plot command to draw a plot of `y` versus `x`. Label each axis and provide a title for your plot.
- `x = t - sin\ t`, `y = t - cos\ t`, `[0,6 pi]`
- `x = cos^3 t,\ y = sin^3 t`, `[0,2 pi]` (Hint: x = (cos(t)).^3)
- `x = 2 cos\ t + cos\ 2t`, `y = 2 sin\ t - sin\ 2t`, `[0, 2 pi]`
- `x = t + 2 sin\ 2t`, `y = t + 2 cos\ 5t`, `[-2 pi, 2 pi]`
- `x = 9 cos\ t - cos 9t`, `y = 9 sin\ t - sin\ 9t`, `[0, 2 pi]`
- `x = sin\ 3t`, `y = cos\ 4t`, `[0, 2 pi]`
- Use the comet command to animate the plot in exercise 1.
- Use the comet command to animate the plot in exercise 2.
- Use the comet command to animate the plot in exercise 3.
- Use the comet command to animate the plot in exercise 4.
- Use the comet command to animate the plot in exercise 5.
- Use the comet command to animate the plot in exercise 6.
The comet command can be used to check your parametrizations. For example, a particle moves about the unit circle, starting at `(0, 1)` at time `t = 0` and proceeding in a counter-clockwise direction. You believe a valid parametrization of this path is given by
`x = cos\ t`, `y = sin\ t`,
where `0 le t le 2 pi`. Check this supposition with
t=linspace(0,2*pi,1000); x=cos(t); y=sin(t); comet(x,y)
- Make the particle in exercise 13 move about the unit circle, starting at `(1, 0)` at `t = 0`, but traveling in a clockwise direction. Do this by adjusting the parametric equations, not the time span. Check your result with the technique shown in exercise 13.
- Make the particle in exercise 13 move about the unit circle, starting at `(1, 0)` at `t = 0`, still traveling in a counterclockwise direction, but moving twice as fast. Do this by adjusting the parametric equations, not the time span. Check your result with the technique shown in exercise 13.
- Make the particle in exercise 13 move about the unit circle, but let it travel in a clockwise direction, starting at the point `(0,-1)` at time `t = 0`. Do this by adjusting the parametric equations, not the time span. Check your result with the technique shown in exercise 13.
