xFunctions: Using Examples


xFunctions is an educational applet for exploring several aspects of calculus and precalculus mathematics. Its use is fully described on the main xFunctions Web page. This page, on the other hand, explains how extra functions and examples can be added to xFunctions. The examples can be specified as applet parameters in the HTML source code of a Web page. If xFunctions is run as a standalone applications, it can read examples from a file that is specified as a command line parameter. Unfortunately, if you want to create such examples, you have to encode them by hand. This page explains how to do the encoding.

There are two versions of the applet, one that appears right on the Web page and one that can be launched in a separate window. Examples are used in the same way with either version of the applet. You can see them in the "Launcher" version of the applet on the main page. The xFunctionsLauncher applet on that page is specified with the following rather long applet tag, which encodes three example functions and eight examples for the various xFunctions utilities:

   <applet  archive="xFunctions.zip"  code="xFunctionsLauncher.class"  width=200 height=30>
      <param  name="1"   value="function; -5,5,-5,5; table; Tbl; 3; -5,-2; -3,1; -1,3; 0,0; 2,1; 4,2; 5,1">
      <param  name="2"   value="function; -5,5,-5,5; graph; B; -5,1,-1, -3,-0.5,1; -3,-0.5,-1, 0,2,0; 0,-2,4, 2.5,1,-1; 2.5,1,-1, 5,0,1">
      <param  name="3"   value="function; 0,5,-1,4; expression; Split;">
      <param  name="4"   value="+  x^2; x < 1; 2*x - 1; x >= 1 and x < 2; 5 - x; x >= 2 and x < 3; (x-3)^2 + 1">
      <param  name="5"   value="multigraph; tan(x) = sin(x) / cos(x); -5,5,-5,5; tan(x); sin(x); cos(x)">
      <param  name="6"   value="animation; Tweening Animation; -8,8,-2,2,0,1,50; sin(x)*k + (1/(x^2+1))*(1-k); false">
      <param  name="7"   value="param; Squiggly Curve; -6,6,-6,6,0,6.3,300; 2*cos(5*t) + 3*cos(9*t); 2*sin(10*t) - 4*sin(2*t)">
      <param  name="8"   value="derivatives; Bezier Curve Derivative; -5,5,-5,5; b(x)">
      <param  name="9"   value="reimann; Close to e; 1,2.718,0,1.5,10; 1/x; 3">
      <param  name="10"  value="graph3d; Singularity; -2,2,-2,2,-2,2,20; 1/(x^2 + y^2); 2">
      <param  name="11"  value="g; 3D Graph with Table Function; -5,5, -5,5, -5,10, 32; Tbl(x)*Tbl(y); 4">
      <param  name="12"  value="integral curves; Attracting/Repelling; -1.5,1.5,-1.5,1.5,0,1,0.01; x^2 + y^2 - 1; (x-y) * (x+y); 3; true;">
      <param  name="13"  value="+   -1.2,-1.2; -0.8,,-1.2; -0.4,,-1.2;  0,,-1.2;  .4,,-1.2;  .8,,-1.2;  1.2,-1.2,;">
      <param  name="14"  value="+   -1.2,-0.8; -0.8,-0.8; -0.4,-0.8;  0,-0.8;  .4,-0.8;  .8,-0.8;  1.2,-0.8;">
      <param  name="15"  value="+   -1.2,-0.4; -0.8,-0.4; -0.4,-0.4;  0,-0.4;  .4,-0.4;  .8,-0.4;  1.2,-0.4;">
      <param  name="16"  value="+   -1.2,0; -0.8,0; -0.4,0;  0,0;  .4,0;  .8,0;  1.2,0;">
      <param  name="17"  value="+   -1.2,.4; -0.8,.4; -0.4,.4;  0,.4;  .4,.4;  .8,.4;  1.2,.4;">
      <param  name="18"  value="+   -1.2,.8; -0.8,.8; -0.4,.8;  0,.8;  .4,.8;  .8,.8;  1.2,.8;">
      <param  name="19"  value="+   -1.2,1.2; -0.8,1.2; -0.4,1.2;  0,1.2;  .4,1.2;  .8,1.2;  1.2,1.2">
   </applet>



Applet Params and Files

An applet parameter is specified by a <param> tag between the opening <applet> tag and the closing </applet>. The applet can read these parameters when it runs. A <param> tag has the form

              <param  name="some name"  value="some value">

For the xFuctions applet, the names must be the consecutive numbers 1, 2, 3, 4, ... You can have as many as you want, but you can't skip any. The applet will stop reading as soon as it encounters a missing number. (By the way, the applet requests params by name, and it doesn't matter to the applet what order the params appear in the <applet> tag. That's why you need the names.)

When xFunctions processes these parameters, it will simply process all the param values in order, as if they were lines in a file. In fact, if you run xFunctions as a standalone application, you can put the exact same lines in a file and tell xFunctions to read them when it starts up. (You do this by providing the file name as a command line argument when you run xFunctions. If you download xFunctions, using one of the downloading links on the main xFunctions page, you'll find a README file that explains more about running xFunctions as an application. You also find a file of examples which contains the same examples given in the applet tag above.)

Now, when xFunctions reads the examples, either from applet params or from a file, any line beginning with a plus sign (+) is appended to the preceding line. (The plus sign is discarded.) This allows you to spread long example definitions over several lines. After lines have been joined in this way, each line defines one example.

So, all you need to know is how to compose a line to specify each of the possible types of example. It's not all that complicated, but the computer is picky about the syntax, and it's easy to make mistakes. If an example contains an error, xFunctions will ignore the example. (It won't crash.) To help you find the problem, xFunctions writes messages to standard output as it processes the example descriptions. If it finds an error, it prints an error message. If you run xFunctions in a Web browser, you will have to figure out where it actually prints these messages. Netscape, for example, prints them to a "Java Console." You can open this console with an entry under the "Communicator" menu (possibly inside a "Tools" submenu in this menu, depending on what version of the program you are using).

(Note: As an alternative to -- or in addition to -- using applet params to specify examples in an applet, you can put the examples in a file and tell the applet to read that file. The file should be on the Web server in the same directory with the source file of the Web page. Then you just have to give the applet the name of the file as an applet parameter. The name is specified in a <param> tag whose name is "file" and whose value is the name of the file. For example: <param name="file" value="example_file.txt">.)

Every example specification consists of a series of items separated by semicolons (;). The first item tells what kind of example it is. This can be the word "function" to specify that you are defining a new function, or it can be the name of a utility (Multigraph, Animate,...) to specify that you are giving an example for one of the utilities. Actually, xFunctions only looks at the first character, so you can say "f" or "func" or "fried green tomatoes" instead of "function". Also, you can use either upper or lower case. The rest of the items in the example depend on which type of example it is. I'll give lists of the items required for all the different types of examples.




Defining New Functions in Examples

It is possible to define new functions as examples. These will be added to the list of functions that appears on xFunction's main screen. Once they are in that list, they can be used in the Utilities and in the definitions of other functions -- you can even use them in later examples. There are three ways to define functions: as one or more expressions, as a graph, or as a table of xy-points. I'll describe each of these possibilities separately, but all three types of function specification begin with the same four items (separated by semicolons): the word "function" (or any string beginning with an "f"), the xy-limits for the function, the type of function, and the name of the function.

The xy-limits consist of four numbers, separated by commas. These are the xmin, xmax, ymin, and ymax values that will be used in the corresponding input boxes when the function is graphed on the Main Screen. Note that they are not the domain and range of the function. They merely specify what region of the xy-plane is displayed on the Main Screen by default. The type of function is one of the words "expression", "table", or "graph". Again, xFunctions will actually only look at the first character, so you can abbreviate these to "e", "t", and "g". The name of the function can be any sequence of letters and digits, as long as it begins with a letter. You cannot redefine a function that is already defined. The letters can be either upper or lower case. (When names are used in expressions, xFunctions doesn't distinguish between upper and lower case. However, when it places functions in the list on the Main Screen, it will order upper case letters before lower case letters. You can make the functions that you define easier to find if you start their names with upper case letters, so that they appear at the top of the list.)


Expression Functions

An expression function is defined by from one to eight expressions. These are the same expressions that would be entered in the Expression Function Input Screen in xFunctions. Note that if you have more than one expression, then every even-numbered expression must be a logical-valued expression (such as "x <= 3") which defines the domain on which the previous expression is valid. For an expression function example, the defining expressions are simply listed as separate items in the example -- separated by semicolons -- after the four standard items. Here is list of the items that go into an expression function example:

  1. the word "function" (or anything beginning with f)
  2. a list of four numbers, separated by commas, giving the xy-limits for the display on the Main Screen
  3. the word "expression" (or anything beginning with e)
  4. the name of the function you are defining
  5. anywhere from one to eight expressions that define the function (separated by more semicolons)

For example, here is how you could define the hyperbolic functions and add them to the function list one the Main Screen. Once they are there, they can be used just like the built-in functions. (Note that spaces between items or between numbers in a list are not significant.)

             f; -3,3,-3,3; e; sinh; (e^x - e^(-x)) / 2
             f; -3,3,-3,3; e; cosh; (e^x + e^(-x)) / 2
             f; -3,3,-3,3; e; tanh; sinh(x) / cosh(x)

Table Functions

A table function is defined by a list of xy-points that lie on the graph of the function. You also have to specify how to fill in the graph between points. There are three possible styles: a step function, a piecewise linear function, or a smooth function. In an example, the style is specified (in order of increasing "quality") as one of the numbers 1, 2, or 3. The specification for a table function includes the following items:

  1. the word "function" (or anything beginning with f)
  2. a list of four numbers, separated by commas, giving the xy-limits for the display on the Main Screen
  3. the word "table" (or anything beginning with t)
  4. the name of the function you are defining
  5. one of the numbers 1, 2, or 3 to specify whether its a step function, piecewise linear, or smooth
  6. two or more points, where each point is a pair of numbers separated by a comma. The x-values of the points must be in strictly increasing order. The points are separated by semicolons.

For example, this defines a piecewise linear function named "W" that looks like a W"

              f; -5,5,0,5; t; W; 2; -4,4; -2,0; 0,2; 2,0; 4,4

Graph Functions

A graph function is a sequence of Bezier segments. Each segment is defined by a list of six numbers, separated by commas. The numbers give:

The x-coordinates in a segment must agree with the x-coordinates of its neighbors. (It is not possible to define graphs with gaps in the domain.) The y-coordinates don't have to agree, so you can make functions with discontinuities. Even if the y-coordinates of neighboring segments do agree, the slopes don't have to agree, so you can make functions with corners.

The specification of a graph function consists of the following items:

  1. the word "function" (or anything beginning with f)
  2. a list of four numbers, separated by commas, giving the xy-limits for the display on the Main Screen
  3. the word "graph" (or anything beginning with g)
  4. the name of the function you are defining
  5. specifications for one or more bezier segments, separated by semicolons. Each segment is specified as a list of six numbers, as described above, separated by commas.

Here is an example that defines a graph function named "Grf" consisting of two bezier segments with a sharp corner at the point (0,0) and with horizontal tangents at (-2,1) and (2,1)

            f; -2,2,0,2; g; Grf; -2,1,0, 0,0,-5; 0,0,5, 2,1,0;



Examples for the Utilities

To define an example for one of the seven utilities, you have to say what goes into each of the inputs on that utility screen. In all cases, this includes a set of values for xmin, xmax, ymin, ymax, and possibly for other numerical values. It also includes one or more expressions to define the functions that the utility will use. There might be other inputs, such as the checkbox labeled "Loop back and forth" in the Animate Utility or the radio buttons for controlling the style of graph in the Graph 3D Utility. I'll describe the exact format you need in order to specify the inputs for each of the utilities. In all cases, the first item in the example is the name of the utility, which can be abbreviated to as little as one letter. This is followed by a menu entry, which is the string that will appear in the pop-up menu in xFunctions to describe this example to the user. (It has to be short enough to fit into this menu.) Next comes a list of numbers, separated by commas, that go into the numerical input boxes in the utility. This is followed by one or more expressions, separated by semicolons. Any further inputs are specified after that.


Multigraph Utility

An example for the multigraph utility is specified by

  1. the word "multigraph" (or anything beginning with m)
  2. the menu entry for the example, which can be any (short) string
  3. a list of 4 numbers, separated by commas, specifying xmin, xmax, ymin, and ymax
  4. anywhere from one to eight expressions, giving the functions of x that are to be graphed, separated by more semicolons.

For example:

                m; An interesting function; -1,2,0,2; abs(x)^x
                multigraph; Two halves of a circle; -2,2,-2,2; sqrt(1-x^2); -sqrt(1-x^2)

Animate Utility

For the animate utility, the list of numbers has seven entries. In addition to xmin, xmax, ymin, and ymax, there are kmin and kmax, which give the limits on the parameter k, and the number of intervals in the animation. The number of intervals must be between 1 and 1000, inclusive. The expression to be animated uses the variables x and k. There is a final item that specifies whether the "Loop back and forth" checkbox should be on. The value of this item must be specified as "true" or "false". (As usual, you can abbreviate to one character.) When the user selects an animation example, the animation will start playing automatically after a second or two. So, an Animate Utility example consists of:

  1. the word "animate" (or anything beginning with a)
  2. the menu entry for the example, which can be any (short) string
  3. a list of 7 numbers, separated by commas, specifying xmin, xmax, ymin, ymax, kmin, kmax and the number of intervals, where the number of intervals is between 1 and 1000
  4. an expression that uses the variables x and k
  5. one of the values "true" or "false" to specify whether the animation loops back and forth

For example:

            a; Functions Converging to f(x)=x; 0,1.2,0,1.2,0,50,50; x^k; false

Parametric Curves Utility

The Parametric Curves Utility requires seven numerical inputs. The three extra inputs are the values of tmin and tmax (the range of values for the parameter) and the number of points that are plotted on a curve. The number of points must be between 2 and 1000, inclusive. You can have up to eight curves. For each two curves, you need two expressions involving the variable t, one to define x(t) and one to define y(t). A Parametric Curves example consists of:

  1. the words "parametric curves" (or anything beginning with p)
  2. the menu entry for the example, which can be any (short) string
  3. a list of 7 numbers, separated by commas, specifying xmin, xmax, ymin, ymax, tmin, tmax, and the number of points on a curve. The number of points must be between 2 and 1000.
  4. an even number of expressions, from two to sixteen, giving the functions of t that define x(t) and y(t) for each curve, separated by more semicolons.

For example (using a + on the second line to break the example into two lines):

              p; A Circle and Two Ellipses; -3,3, -3,3, 0,6.29, 200; 2*cos(t); 2*sin(t);
              +   2*cos(t); sin(t);   cos(t); 2*sin(t)

Derivatives Utility

An example for the derivatives Utility consists of the following items:

  1. the word "derivatives" (or anything beginning with d)
  2. the menu entry for the example, which can be any (short) string
  3. a list of 4 numbers, separated by commas, specifying xmin, xmax, ymin, and ymax
  4. an expression giving the function of x

For example:

             d; Vertical tangent at x=0?; -2,2,-2,2; abs(x)^x

Riemann Sums Utility

The Riemann Sums Utility requires five numerical inputs. The last one is the number of intervals into which the domain is to be divided. This must be a number between 1 and 512, inclusive. You also have to specify which summation method will be displayed on the graph. There are six possible methods, which are specified by the numbers from 1 to 6: left endpoint rule, right endpoint rule, midpoint rule, inscribed rectangle rule, circumscribed rectangle rule, and trapezoid rule. So, an example consists of:

  1. the words "riemann sums" (or anything beginning with r)
  2. the menu entry for the example, which can be any (short) string
  3. a list of 5 numbers, separated by commas, specifying xmin, xmax, ymin, ymax, and the number of subintervals. The number of subintervals must be between 1 and 512.
  4. An expression giving the function of x
  5. A number from 1 to 6 specifying the display method

For example:

         r; Area Under a Parabola; 0,1,-0.2,1.2,6; x^2; 3

Integral Curves Utility

An Integral Curves example requires seven numbers. The fifth and sixth of these go in the boxes labeled "Start x" and "Start y" in the utility. (These values give a point where a curve will start when the user presses the New Curve button. You might not find it useful to provide such a point in an example, but you have to include it anyway.) The seventh number is the "dt" value that specifies time between points on the curves. The example must also specify the integration method as a number 1, 2, or 3 standing for Euler's method, Runge-Kutta Order 2, or Runge-Kutta Order 4 (in order of increasing "quality"). Finally, you have to specify whether the checkbox labeled "Extend curves in both directions" is checked. You do this with one of the values "true" or "false", which can be abbreviated to just "t" or "f".

The Integral Curve utility draws integral curves starting at specified points. You can, optionally, include a list of points in your example where curves are to be started. Each point is specified as a pair of numbers, separated by a comma. If your example includes such points, then the curves will be started a second or two after the user chooses the example from the menu.

An Integral Curve example consists of

  1. the words "integral curves" (or anything beginning with i)
  2. the menu entry for the example, which can be any (short) string
  3. a list of 7 numbers, separated by commas, specifying xmin, xmax, ymin, ymax, Start x, Start y, and dt. The value of dt must be greater than zero.
  4. two expressions using the variables x and y, separated by a semicolon. These give the values of dx/dt and dy/dt.
  5. A number 1, 2, or 3 specifying the integration method.
  6. One of the values "true" or "false" to specify whether curves are to be drawn both forwards and backwards in time
  7. Optionally, you can add any number of points. Each point is a pair of numbers, separated by a comma. The points are separated by semicolons.

Here is an example that does not use extra points. For an example with points, see the example in the applet tag at the top of this page.

            i; Pendulum Phase Space; -4, 4, -4, 4, 0, 0, 0.1; y; sin(x); 3

Graph 3D Utility

For the Graph 3D Utility, you need seven numbers, including zmin, zmax, and the grid size. The grid size is a number between 8 and 64, inclusive, that determines how many points are plotted on the graph. Then, in addition to a function of x and y, you need to specify which type of graph will be drawn. There are four types, which are specified by the numbers 1, 2, 3, and 4 (in order of increasing "quality"): wireframe model, wireframe with hidden lines removed, shaded model, and shaded model with wires. So, an example for Graph 3D consists of:

  1. the words "graph 3d" (or anything beginning with g)
  2. the menu entry for the example, which can be any (short) string
  3. a list of 7 numbers, separated by commas, specifying xmin, xmax, ymin, ymax, zmin, zmax, and the grid size. The grid size must be between 8 and 64.
  4. an expression giving the function of x and y that is to be graphed
  5. A number between 1 and 4 specifying the style of graph.

For example:

                g; Eggcrate; -4,4,-4,4,-2,2,50; sin(x)*cos(y); 4


David Eck (eck@hws.edu), 27 October 1999