Directions and Discussion for the Functions Plotter


The Functions Plotter Applet is designed to quickly plot mathematical functions of one parameter. The screen snapshot below shows a typical example with 2 functions plotted in the Plot Pane tab.

Functions Plotter Snapshot

You will notice that the 2 functions are specified by name exp and expsin in a comma-separated list. The function exp is built-in whereas the function expsin is user-defined in the Function Definition tab.

Function Definition Snapshot

The definition of the function expsin is quite simple.

You may test a function definition by typing expressions that use the function in the Expression Evaluation Pane.

You can go a long distance in using the Functions Plotter with just this much information. The rest of this page documents information needed for more sophisticated usage.

Plot Controls

The controls in the Plot Pane are straightforward.

The plotter is sophisticated and will simply ignore all data points that are generated during the evaluation process that are infinite or NaN, that is, “not a number”. If you show the plot data table, values that are infinite or NaN will be noted as such.

Looking Ahead

The plotter works by reading or parsing expressions including the expressions that define user functions. User functions can have zero or more parameters. The functions that will be plotted must have exactly one parameter but, for example, a function with two parameters might be used in the definition of another function with one parameter. Thus the generality is useful.

Before examining user functions and special forms, it is useful to tabulate all of the built-in definitions. We will then describe the special forms which make sophisticated definitions easy. Next we will comment on user functions. Finally we will provide access to the source code of FunctionsPlotter.

Built-in Definitions

The tables below list all of the built-in definitions.

Constant definitions:
Identifier Type Definition
true boolean true
false boolean false
pi double Math.PI
e double Math.E
MaxInt int Integer.MAX_VALUE
maxint int Integer.MAX_VALUE
MinInt int Integer.MIN_VALUE
minint int Integer.MIN_VALUE
MaxLong long Long.MAX_VALUE
maxlong long Long.MAX_VALUE
MinLong long Long.MIN_VALUE
minlong long Long.MIN_VALUE
MaxDouble double Double.MAX_VALUE
maxdouble double Double.MAX_VALUE
MinDouble double Double.MIN_VALUE
mindouble double Double.MIN_VALUE
Infinity double Double.POSITIVE_INFINITY
infinity double Double.POSITIVE_INFINITY
NaN double Double.NaN
nan double Double.NaN
Function definitions:
Function Name Typical Usage
abs abs(x)
ceiling ceiling(x)
floor floor(x)
round round(x)
max max(x,y)
min min(x,y)
sqrt sqrt(x)
power power(x,y)
root root(x,y)
todegrees todegrees(theta)
toradians toradians(angle)
sin sin(theta)
sindeg sindeg(angle)
cos cos(theta)
cosdeg cosdeg(angle)
tan tan(theta)
tandeg tandeg(angle)
asin asin(s)
asindeg asindeg(s)
acos acos(c)
acosdeg acosdeg(c)
atan atan(t)
atandeg(t) atandeg(t)
atan2 atan2(y,x)
atan2deg atan2deg(y,x)
exp exp(x)
log log(x)
ln ln(x)
log2 log2(x)
log10 log10(x)
logtobase logtobase(x,b)

In the above table:
    x and y stand for generic variables;
    theta is an angle in radians;
    angle is an angle in degrees;
    s, c, t stand for possible values of sin, cos, tan;
    and b is the base for a logarithm.

The “function” random is implemented in a special fashion to take one of three forms with the following interpretations.

Function Name Typical Usage Interpretation
random random() Random number between 0 and 1
random random(x) Random number between 0 and x
random random(x,y) Random number between x and y
Operation definitions:
Precedence Operations Symbols
1 operationOR ||
2 operationAND &&
3 operationEQ operationNE == !=
4 operationLT operationGT operationLE operationGE < > <= >=
5 operationPlus operationMinus + -
6 operationTimes operationSlash operationPercent * / %
7 operationCaret ^
8 operationNOT !

The caret operation ^ implements exponentiation. The operation x^y will be performed if the combination of arguments makes sense mathematically and the result can be represented in the range implemented in computer arithmetic.

Special Forms

The expression parser has the ability to define special forms and five such forms are built-in: set, let, if, eval, and random. Here is a brief synopsis of these special forms.

Special Form Typical Usage
set set(identifier,expression)
let let(identifier,expression)
if if(test,expression-1,expression-2)
eval eval(expression-1,...,expression-n)
random random() or random(x) or random(x,y)

The special forms are interpreted as follows.

random is introduced as a special form since in our simple parser ordinary functions cannot be overloaded to take different numbers of arguments.

Defining User Functions

The definition of user functions can be more sophisticated than it may appear at first because:

Of course, the bread and butter of user-defined functions remains those defined by simple arithmetic expressions as in our example of expsin above.

Source Code

The standalone FunctionsPlotter applications requires 2 files in addition to the Java Power Tools.

This class contains the heart of the Functions Plotter code. The code makes liberal use of many tools built into Java Power Tools.

This class defines a widget that shows a labeled list of ColorView objects so that multiple colors may be set at once. The application of this class is to define 8 colors for a sequence of up to 8 functions.

The FunctionsPlotter applet is built using a standard template that calls the application code.

For pedagogical purposes, I took snapshots of the code during the development process. Each of the snapshots may be run as a standalone application. The MultiColorView class is needed for versions 7 and 8. Interestingly, no plotting is done until version 5. Earlier versions develop GUI structure only.

Version 1 uses JPT to structure the GUI for the Plot Parameters area of the window and the Plot Functions button that is below that area. The button has no functionality at this stage.

Version 2 changes the base class to BasePane to access definitions of “prettier” fonts that are then used for the labels, text fields, and buttons.

Version 3 introduces a 500-by-500 graphics pane for plotting. The plot controls now occupy the left hand side of the window and the graphics pane is placed on the right hand side.

Version 4 introduces a JTabbedPane in order to make available a pane in which the user may define simple mathematical functions that may later be plotted. This pane is prebuilt into JPT as the SimpleFunctionBuilder class. Underneath this class is a substantial infrastructure of both GUI and parser code. The graphics pane size was also changed to 600-by-600.

Version 5 is the first version that plots functions. The JPT parser class BaseParser has built-in tools to take precisely the information in the Plot Parameters pane and compute the array of Point2D data needed for the plot. Plotting itself is then handled by the JPT class PlotTool which can plot multiple data sets in multiple colors. In this version, however, all functions are plotted in one color: red.

Version 6 introduces mouse behavior in the graphics pane. Specifically, if a plot is visible, then as the mouse moves over the plot, the x,y coordinates will be displayed in 2 text fields below the graphics pane. The coordinates are “world” coordinates in the problem space not raw pixel positions.

Version 7 introduces distinct colors for up to 8 functions plotted. This uses the widget MultiColorView which was developed for this purpose but which is also coded in a general fashion so that it may someday migrate into JPT. Access to the colors is via a button that opens a dialog box.

Version 8 eliminates the dialog box for the MultiColorView widget since opening the dialog box turned out to be inconvenient. Instead the widget is placed directly below the Plot Parameters pane in the left side of the main GUI.

The final version (9) whose source code is given above adds the facility for displaying the actual plot data that was used to construct the function plots. This enables the user to examine the data carefully, look for interesting values such as maxima and minima, and even see if some values are infinite or NaN.

As an interesting example of plot data, plot exp,log on the range -2 to 2 and then look at the plot data.