next up previous contents
Next: Functions Up: WeBWorK Newbie Guide - Previous: Captioning your graphs   Contents

Tables

Table macros use the HTML table formats, or the LATEX ``tabular'' environment. The format is fairly straightforward

     TEXT(
          begintable (number_of_columns),
             row(item_1, item_2, ... item_{number_of_columns}),
             row(@array_of_entries),
          endtable()
         );

No checking is made to see if you have entered the correct number of entries in each row. Also note that if array_of_entries is a typical row of your table, you can specify the number_of_columns as scalar(array_of_entries) + 1.

Below is an example of how to use tables to provide information about the first and second derivative of a polynomial function $ f$ with critical points at integers $ a < b < c$. The values of the first and second derivatives of $ f$ are specified at points between the critical points, and a table of those values is displayed. Then four graphs are displayed: $ f(x)$, $ -f(x)$, $ f(-x)$, $ -f(-x)$, and the student is told to choose the correct one. A rather awkward way of using the rules defining to the first and second derivatives to produce the values in the tables is used. It should be possible to do this more cleanly, perhaps via the FUN.pm library.

## variables $a, $b, $c denote the zeros of the derivative of $f
## $y0 is the $f(0)
$a = random(-6,-3,1);
$b = random(-2,1,1);
$c = random(2,6,1);
$y0 = random(-2,2,1);

## Set the domain of f
$xmin = -8;
$xmax = 8;

## Set the visible range
$ymin = -100;
$ymax = 100;

## In order to use a certain construction, I need a reference to a function
## The only way I know how to do this is via add_functions
## Probably, I could get this out of the FUN.pm library, but I am not
## yet sure how to do that

$null_graph_object = init_graph($xmin,$ymin,$xmax,$ymax,
                    'axes' => [0,0],
                    'grid' => [16,20],
                    'pixels' => [300,300]
                    );

## Define the function $f to graph as well as it's derivatives $fp and $fpp
##
$f = FEQ("(x**4)/4 - ($a + $b + $c) * (x**3)/3  + 
     (($a)*($b) + ($a + $b) * ($c))* (x**2)/2 - 
     ($a)*($b)*($c)*x + $y0 for x in [$xmin, $xmax] 
     using color:blue and weight:2");

$fp = FEQ("(x - $a) * (x - $b) * (x - $c) 
      for x in [$xmin,$xmax] using color:red and weight:2");

$fpp = FEQ("(x - $a)*(x - $b) + (x - $a)*(x - $c) + 
       (x - $b)*(x - $c) for x in [$xmin, $xmax] 
       using color:yellow and weight:2");


($f_ref, $fp_ref, $fpp_ref) = add_functions($null_graph_object, $f, $fp, $fpp);


@graph_object = ();
for ($i = 0; $i <= 3; $i++)
    {
     $graph_object[$i]=init_graph($xmin,$ymin,$xmax,$ymax,
                       'axes' => [0,0],
                       'grid' => [16,20],
                       'pixels' => [300,300]
                       );
    }

$minus_f = FEQ("-(x**4)/4 + ($a + $b + $c) * (x**3)/3  - 
          (($a)*($b) + ($a + $b) * ($c))* (x**2)/2 + 
          ($a)*($b)*($c)*x - $y0 for x in [$xmin, $xmax] using
          color:blue and weight:2");

$f_of_minus_x = FEQ("(x**4)/4 + ($a + $b + $c) * (x**3)/3  + 
                (($a)*($b) + ($a + $b) * ($c))* (x**2)/2 + 
                ($a)*($b)*($c)*x + $y0 for x in [$xmin, $xmax] using
                color:blue and weight:2");


$minus_f_of_minus_x = FEQ("-(x**4)/4 - ($a + $b + $c) * (x**3)/3  - 
                      (($a)*($b) + ($a + $b) * ($c))* (x**2)/2 - 
                      ($a)*($b)*($c)*x - $y0 for x in [$xmin, $xmax] using
                      color:blue and weight:2");

## Add the function and variations to the graph objects
##
add_functions($graph_object[0], $f);
add_functions($graph_object[1], $minus_f);
add_functions($graph_object[2], $f_of_minus_x);
add_functions($graph_object[3], $minus_f_of_minus_x);


## This next part assigns an actual path for a given graph image
## For example the value of $graph[0] is WWPlot=HASH(0x879f278)
## The actualimage is insertGraph($graph[0]), namely
## .../webwork_tmp/m13_test/gif/trs-82786-setShemanskeprob2gif1.gif
##
@graph_path=();
for ($i = 0; $i <= 3; $i++)
    {
      $graph_path[$i] = insertGraph($graph_object[$i]);
    }

## Define an array of captions with the letters A - D
@captions = @ALPHABET[0..3];

## NchooseK(n,k):  choose k integers randomly from 0, 1, ..., (n-1)
## and put them in the array @indices
@indices = NchooseK(4,4);


@permuted_graph_path = @graph_path[@indices];


BEGIN_TEXT 
Using the information about the first and second
deriviatives provided in the tables below, enter the letter of the
graph below which corresponds to the function.  
\{ ans_rule(10) \}
$PAR 
END_TEXT


## Build the table of values here
## Test the half-integer points between the critical points
## Should be adequate for the second derivative as well.
##

## The number of columns will be $c - $a + 3
$number_of_columns = $c - $a + 3;
## Now create the array of values at which to evaluate the derivatives
@x_coords = ();

## Let yp_coords be the array of values of the first derivative
## at the specified points
@yp_coords = ();

## Let ypp_coords be the array of values of the second derivative
## at the specified points
@ypp_coords = ();

$x_coords[0] = "\(x\)";
$yp_coords[0] = "\(f'(x)\)";
$ypp_coords[0] = "\(f''(x)\)";

## A little kludgey on the indexing
## $x_coords[1] = $a - .5 
## $x_coords[$number of columns] = $c + .5
## yp_coords and ypp_coords get the values of
## the first and second derivatives
##
for ($i = 1; $i < $number_of_columns; $i++)
    {$x_coords[$i] = $a - 1.5 + $i;
     $yp_coords[$i] = &{$fp_ref->rule}($x_coords[$i]);
     $ypp_coords[$i] = &{$fpp_ref->rule}($x_coords[$i]);
    }


TEXT(
     begintable ($number_of_columns),
       row(@x_coords),
       row(@yp_coords),
       row(@ypp_coords),
     endtable()
    );


## Print the graphs out here
TEXT(imageRow(~~@permuted_graph_path, ~~@captions));

ANS(str_cmp( [@ALPHABET[ invert(@indices)]]));



Thomas R. Shemanske 2002-03-05