( 15 )
Previous Table Of Contents Next


Sinusoids and Spirographs

I have now taught you enough about programming the RPN calculator that you should be ready to write your first program from scratch. Programs that generate plots are obviously fun to watch so let's create one of those. Instead of a second order equation (which results in a parabolic shape) your assignment is to write a program that draws the sine wave given by the following equation:

y = sin(x)

You study the sin() function in a trigonometry class where you learn that this mathematical function describes the sinusoidal oscillation that we can observe in the movement of a pendulum, the reed on a clarinet, etc. We want to plot this equation for the infinite sequence of x axis values 0, 1, 2, etc. Hence the structure of the new DrawSineWave.txt program will be largely identical to the SecondOrderEq6.txt program we just studied except the new program won't require any passed parameters. You can either generate the new program using Notepad or else type it in using the calculator's keypad (remember to clear the old program first). Once you have a new x axis value in the X stack location you simply employ the sin mnemonic (which corresponds to the keystroke) to get the corresponding y axis value. Then arrange the X and Y stack locations to hold the x and y axis values before issuing the SumG command.

You can do this. Give it your best shot. My solution required only 13 statements and over half of those were simply carried over from SecondOrderEq6.txt (even professional programmers usually accomplish a new assignment by using a lot of cut and paste from prior programs). Don't just skip past this exercise or you won't learn to program. Think for a while and try some wild guesses if the solution isn't immediately obvious. Debug your attempt by single-stepping your program. Your observations on what the program is and what it is not doing will give you ideas on what to try next. You can't hurt your computer or this simulator. Only when you are exhausted do I give you permission to look at my solution which you can find in the "Solutions" folder.

Another function that you study in trigonometry is the cos() function which results in the cosine waveform which is quite similar to the sine waveform that you have now seen thanks to the DrawSineWave.txt program. Together, the sin() and cos() functions allow you to convert Cartesian coordinates (i.e., x/y coordinates) to polar coordinates (rho, theta) where rho is the radial distance from the origin and theta is the angle measured counterclockwise from the positive x axis. Every point can be described either by an (x,y) pair or a (rho,theta) pair as shown in the drawing below:

It is easier to describe curves that have circular symmetry using polar coordinates rather than Cartesian coordinates. For example, a circle of radius A centered on the origin can be described in polar form by the parametric equation shown below where i is the parameter that takes on the infinite sequence of integer values 0, 1, 2, ...

rho = A theta = i

You don't often see equations simpler than that! In Cartesian (also called "rectangular") coordinates this same circle is described by:

x = A * cos(i) y = A * sin(i)

Since the cGraph window can only accept points described by Cartesian coordinates, we have no choice but to employ the more complicated set of equations. You can find an RPN calculator program that draws a circle of radius A in the DrawCircle.txt file. This program requires 1 passed parameter, the desired radius A. To get a circle of radius 10, you would initiate this program via the keystroke sequence 10, GSB 0.

I kind of like watching the plots appear in the cGraph window as the cGraph software continually re-selects the best ranges for the x and y axes. But if you don't like this Auto Range feature then you can shut it off via the "Reformat Plot..." menu choice found on the cGraph window's menu bar.

When the circle is finished it may look more like an oval than a circle. This is because the cGraph window allows you to drag its width and height to any value and hence the plot's aspect ratio is not guaranteed to be unity. A unity aspect ratio means that 1 unit on the y axis covers the same number of pixels vertically as 1 unit on the x axis covers horizontally. Use your mouse to resize the cGraph window until you see a nice circle.

Once we can draw a circle it takes only a small change to get a spiral. A spiral is a circle whose radius is always decreasing. We can accomplish this using the following parametric equations:

  x = .999i * A * cos(i)

  y = .999i * A * sin(i)

When the i parameter has the value 0 the radius of the spiral will be .9990 * A = 1 * A = A. When i =1 the radius will be .9991 * A = .999 * A. When i =2 the radius will be .9992 * A = .999 * .999 * A = .998 * A. As i continues to increase the radius will continue to decrease thus distorting the circle into a spiral.

Start with my DrawCircle.txt example program and modify it to create a DrawSpiral.txt program that employs these last two equations. If you get stuck you can find my solution in the "Solutions" folder.

Can you predict the shape that will be generated by the following parametric equation?

x = sin ( 3 * i ) * cos( i ) y = sin ( 3 * i ) * sin( i )

This looks like our standard circle equation except the radius term is now sin(3i). This means that the radius will be changing in a sinusoidal fashion as we draw out the circle. Run my DrawFlower.txt (oops, I gave the answer away) example program to see what this small change causes.

Okay, let's move on to something even more flowery: spirographs (which mathematicians call "epicycloids"). Besides the spirograph toy, another place you have seen this complicated shape is when watching a wheel mounted bicycle reflector at night (assuming you are looking at a right angle to the path of the bicycle).

A Spirograph is formed by rolling a first circle (called the moving circle) inside or outside of another circle (called the stationary circle). The pen is attached to the moving circle at some arbitrary distance from that circle's center. If the radius of the fixed circle is R, and the radius of the moving circle is r, and the offset of the pen from the center of the moving circle is p, then the resulting curve drawn by the pen is described by:

x = ( R+r )*cos( i ) - p*cos( ( ( R+r ) / r ) * i ) y = ( R+r )*sin( i ) - p*sin( ( ( R+r ) / r ) * i )

Now that's what I call an equation! As before, i is the angle which starts at 0 degrees and continues incrementing forever. Let's not attempt to analyze or derive this equation, let's just enjoy the pictures it produces. Load up my DrawSpirograph.txt example program and run it for the following test cases:

R = 75, r = -30, p = 60 R = 75, r = -32, p = 60 R = 60, r = -16, p = -15 R = 90, r = 1, p = 105 R = 90, r = 2, p = 105 R = 89, r = 1, p = 105 R = 100, r = 48, p = 60

The keystroke sequence that will initiate the first test case listed above is:

75 ENTER 30 CHS ENTER 60 GSB 0

Here are some of the pretty pictures I observed using the particular test cases I have already listed. Remember that the cGraph window gives you complete control of the plot format so you can change the default pen color and plot range.

I list below a URL from the University of California at San Diego where you may still be able to find a web site that provides a Java applet that visually constructs the spirograph plots so you can see why they turn out like they do. As with all Java applets, it takes a while for the necessary code to be downloaded onto your computer, so be patient.

http://math.ucsd.edu/~dlittle/java/SpiroGraph.html


Previous Table Of Contents Next