|
The on-the-fly graphics example
To obtain this problem
WARNINGS µ¦å{h
{viewSource(setMAAtutorial/ontheflygraphicsexample1.pg)}
Comments:
- First we define a graph with x and y in the range -4 to 4, axes (strong lines)
defined at the point [0,0] and
with 8 gridlines horizontally and 8 grid lines veritically.
$graph is a graph object (or more appropriately,
a pointer to a graph object).
- We define a function and it's first and second derivatives
on the domain [-4,4]
- We need to mix up the colors assigned to each function,
since it won't do us
any good if every student's function is colored blue,
their first derivative red, and their second derivative green.
- Create a scrambled list of colors and letters.
Here are the basic colors
@colors = ("blue", "red", "green");
Slice will contain the numbers 1,2,3 in some permuted order
@slice = NchooseK(3,3);
The slice of the color list (sc) contains the colors in a new
order defined by the ordering in @slice.
This construction @array[@slice] is the way to apply a permutation to a list.
@sc = @colors[@slice];
Now we scramble the letters labeling the graphs using the same permutation.
These list will contain the correct answers in the proper order.
@sa = ('A','B','C')[@slice];
-
$f = FEQ("sin($a+$b*cos(x)) for x in <-$dom,$dom> using color:$sc[0] and weight:2");
The first string (for $f) should be read as: "The function is
calculated using sin($a+$b*cos(x))
and is defined for all x in the
interval -$dom to +$dom. Draw the function using the first color
in the permuted color list @sc
and using a weight (width) of two pixels."
The FEQ macro (Format EQuation) cleans
up the writing of the function.
Otherwise we would need to worry about the signs of $a, $b and so forth.
For example if $b were negative, then after interpolation
$a+$b might look like 3+-5. FEQ replaces the +- pair by -, which is what you want.
- Install the functions into the graph object.
Plot_functions converts the string to a subroutine
which performs the necessary calculations and
asks the graph object to plot the functions.
($fRef,$fpRef,$fppRef) = plot_functions( $graph, $f,$fp,$fpp );
The output of plot_functions is a list of pointers to functions
which contain the appropriate data and methods.
So $fpRef->rule points to the method which will calculate
the value of the function.
&{$fpRef->rule}(3) calculates the value of
the function at 3. See Fun.pm for more details.
- Create labels for each function
The 'left' tag determines the justification of the label to the defining point.
Place the second letter in the permuted letter list at the point (-.75, fp(-.75)) using
the second color in the permuted color list.
- The construction
\{ image(insertGraph($graph)) \} inserts
the graph at this point in the text.
|