Now we work on a slightly more serious programming example, with some plotting. Our task is to define two functions, find their intersection points, and then plot each function and highlight their intersections. We will use colors.
All of the following go in the same file, plot_f.pro. First, a routine to do RGB colors on a true color display. You will usually copy this function exactly and use it in your own work. A number like "256L" means that it is a "long integer," so big that the usual integer storage space is not large enough.

Here are our two functions.

We find their intersections by exploiting the fact that both are even functions, and that they intersect twice. To find the precise point on the + side, let's use Newton's method, which is easy in IDL. To do this, we need to first define a function for the difference between the two functions f and g. Give this, we can use the built in function "newton," which takes an initial guess and the difference function name as arguments. (If the initial guess is very bad this might not work, but here there is no danger.)

And finally, the main program plot_f. Note how the user can pass optional parameters/arguments to the procedure. If they are not supplied, default values are calculated.

The default run, from the IDL command line:

Which gives:

Here's an example of an error caught before plotting proceeds:

A non-default run:

Which gives:

| Taner Edis | Home |
| Last modified: November 15, 2006 |   |