## Graphing Polar Equations in Matlab

Consider the point P in the Cartesian plane pictured in Figure 1. A point in the Cartesian plane.

We've drawn a segment from the point P to the origin and marked its length as r. In addition, we've marked the angle made with the horizontal axis as theta.

In the Cartesian coordinate system, the coordinates of the point P are (x,y). In polar coordinates, we mark the point P with the coordinates (r,theta). The r represents the radial distance from the origin and the theta represents the angle made with the horizontal axis (counterclockwise being the positive direction).

A simple trigonometric definition describes the relation between Cartesian and polar coordinates.

$\begin{eqnarray} x&=& r \cos\theta\\ y&=& r \sin\theta \end{eqnarray}$

### Matlab's POLAR Command

Consider the polar equation

$r=\cos 2\theta,$

called the four-leaf rose. We can use Matlab's polar command to plot the graph of this equation on [0,2 pi]. First, use Matlab's linspace to generate 100 equally spaced points on the interval [0,2pi], then generate the corresponding r-values. Semicolons are used to suppress the output.

>> theta=linspace(0,2*pi);
>> r=cos(2*theta);


Now use Matlab's polar command.

>> polar(theta,r)


This will produce the image shown in Figure 2. The polar plot of r=cos(2theta).

### Changing to Cartesian Coordinates

We can use Matlab's plot command if we change to Cartesian coordinates. This is a simple matter. We simply use the equations of transformation:

$\begin{eqnarray} x&=& r \cos\theta\\ y&=& r \sin\theta \end{eqnarray}$

So, first get the polar coordinates again.

>> theta=linspace(0,2*pi);
>> r=cos(2*theta);


Next, transform these into Cartesian coordinates.

>> x=r.*cos(theta);
>> y=r.*sin(theta);


Now we can use the plot command.

>> plot(x,y)


This will produce the image shown in Figure 3. Changing to Cartesian coordinates allows us to use the plot command.

In Figure 3, you'll note a bit of distortion. The graph seems to be "stretched" a bit in the horizontal direction. You can understand the source of this "strech" by examining the tick marks on each axis. Note that 0.5 units on the x-axis is a slight bit longer that 0.5 units on the y-axis. We can "equalize" with Matlab's axis equal command.

>> axis equal


This produces the image shown in Figure 4. The command axis equal makes 1 unit on the x-axis the same length as 1 unit on the y-axis

Of course, we can also provide axis labels and a title.

>> xlabel('x-axis')
>> ylabel('y-axis')
>> title('The plot of r = cos(2\theta)')


This produces the plot in Figure 5. Adding axis labels and a title to our plot.

Note that Matlab understands a subset of TeX commands. Hence, the command title('The plot of r = cos(2\theta)') typesets a theta in the title.

#### Script Files are More Efficient

Of course, a script file is a much more efficient way to work. Open a new file in the editor, then enter the following code.

theta=linspace(0,2*pi);
r=cos(2*theta);
x=r.*cos(theta);
y=r.*sin(theta);
plot(x,y)
axis equal
xlabel('x-axis')
ylabel('y-axis')
title('The plot of r = cos(2\theta)')


Save the file as script1.m and execute by pressing F5 in the editor.

### Comet Plots

By using Matlab's comet command, we can get a sense of how the plot progresses. Try the following adjustment to the script file posed above.

theta=linspace(0,2*pi,2000);
r=cos(2*theta);
x=r.*cos(theta);
y=r.*sin(theta);
comet(x,y)
plot(x,y)
axis equal
xlabel('x-axis')
ylabel('y-axis')
title('The plot of r = cos(2\theta)')


1. The linspace command is used to generate 2000 equally spaced points. The goal is to slow down the animation. Adjust the 2000 upward if the animation is too fast, downward if the animation is too slow.
2. We added a comet(x,y) command to animate the plot. In order to obtain a plot that will print, we added a plot command and some annotations. These will execute as soon as the comet animation is complete.

Save the file as script2.m and execute by pressing F5 in the editor.

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

polar.m

The file mypolar.m is designed to be run in "cell mode." Open the file mypolar.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.

One final fun thing to do is to select Publish to HTML from the File menu and watch what happens. You might want to try a smaller number of points in the comet plot before you try this. The result is shown in mypolar.html.

### Exercises

The beauty of polar equations is the chance to see some really beautiful and exatic plots. Write a script similar script2.m in the Comet Plots section to plot the graph of the following polar equation, known as the "butterfly." It will be your duty to discover an appropriate interval for theta.

r=e^{sin theta}-2cos(4theta)+sin^5((2theta-pi)/24)

Include a printout of the image and the M-code that produced your image with your homework submission.