Difference between revisions of "GraphsInTables"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 212: Line 212:
 
foreach $j (0..5) {
 
foreach $j (0..5) {
   
$graph[$j] = init_graph(-10,-10,10,10,'axes'=>[0,0],'ticks'=>[10,10],'size'=>[400,400]);
+
$graph[$j] = init_graph(-10,-10,10,10,'axes'=>[0,0],
  +
'ticks'=>[10,10],'size'=>[400,400]);
 
$graph[$j]->lb('reset');
 
$graph[$j]->lb('reset');
   

Revision as of 21:16, 11 February 2010

Putting Graphs into Tables


This PG code shows how to put graphs into tables so that they will be displayed compactly and a proper size in both HTML and TeX modes.

  • Example 1: Putting a text block and a graph side-by-side using ColumnTable from unionTables.pl
  • Example 4: Displaying a narrow text block and an array of graphs side-by-side using ColumnTable and BeginTable from unionTables.pl


Example 1: Putting a text block and a graph side-by-side using ColumnTable from unionTables.pl

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

loadMacros(
"PGstandard.pl",
"PGgraphmacros.pl",
"MathObjects.pl",
"unionTables.pl", # or "PGunion.pl",
);

TEXT(beginproblem());

$refreshCachedImages=1;

Initialization: Be sure to load unionTables.pl (or PGunion.pl which will load it automatically). Set $refreshCachedImages=1; if you desire the graphics files to be refreshed each time.

Context("Numeric");

$a = random(2,3,1); # -$a is the left x-intercept
$b = random(2,4,2); #  $b is the right x-intercept
$c = random(1,4,1); # -$c is the y-intercept

$k = $c/($a * $b);

$A = $k;
$B = $k*($a - $b);
$C = -($c);

$gr = init_graph(-5,-5,5,5,axes=>[0,0],grid=>[10,10]);

add_functions($gr, "$A*x**2+$B*x+$C for x in <-5,5> using color:blue and weight:2");
$gr -> lb(new Label ( 4.5,0,'x','black','left','bottom'));
$gr -> lb(new Label ( 0.25,8.5,'y','black','left','bottom'));
$gr -> lb(new Label ( 0.25,$c,'y = f(x)','black','left','bottom'));

Setup: Generate a graph to be included in the main text. This graph is a parabola opening up with roots -$a, $b and y-intercept -$c.

Context()->texStrings;
BEGIN_TEXT
\{
ColumnTable(
"Use the graph to find the missing values.  
There may be more than one correct answer, 
in which case you should enter your answers
as a comma separated list.  If there are no
correct answers, enter ${BITALIC}NONE.${EITALIC}".
$BR.
$BR.
"(a) \( f(0) = \) ". 
ans_rule(7).
$BR.
$BR.
"(b) \( f \big( \) ".ans_rule(7)." \( \big) = 0 \). ", # comma!
image(insertGraph($gr), width=>400, height=>400, tex_size=>700).
$BR.$BCENTER.
"(Click on graph to enlarge)".
$ECENTER, # comma!
indent => 0, separation => 30, valign => "TOP"
)
\}
END_TEXT
Context()->normalStrings;

Main Text: We use ColumnTable( columnA, columnB, options ) to display the text and answer blanks in the left column, and the graph in the right column. Notice that within each column, every line except for the last ends with a period to join together things like strings, answer rules, etc. The last line in both columns ends with a comma, as noted in the commented out portions # comma!.

Using ColumnTable has two main advantages.

  • Using a ColumnTable to put the graphics and text side-by-side is very useful when the text is long. If the graphic were below a long block of text, the student would have to scroll up and down frequently between the questions and the graphic, making it harder to use.
  • ColumnTable behaves well in TeX mode. The default in TeX mode is a two-column format for the printed page, which means that each column is very narrow. Using an ordinary table (instead of a ColumnTable) to put text and graphics side-by-side would likely mean that the graphics would spill over from the left column of the printed page to the right column of the printed page (causing text and graphics to overlap) or, worse yet, spill over from the right column of the printed page into oblivion. However, using a ColumnTable, the graphics will appear underneath the text in TeX mode, as desired.

$showPartialCorrectAnswers = 1;

ANS( List(-$c)->cmp() );

ANS( List(-$a,$b)->cmp() );

ENDDOCUMENT();

Answer Evaluation: Since there may be multiple answers, we use List(), even when there is only one answer (to avoid error messages if a student enters multiple answers when there is only one correct answer).




Example 4: Displaying a narrow text block and an array of graphs side-by-side using ColumnTable and BeginTable from unionTables.pl

Problem Techniques Index

PG problem file Explanation
loadMacros(
"PGstandard.pl",
"PGchoicemacros.pl",
"PGgraphmacros.pl",
"unionTables.pl",
);
       
TEXT(beginproblem());

$refreshCachedImages=1;

Initialization: Be sure to load unionTables.pl (or PGunion.pl which will load it automatically). Set $refreshCachedImages=1; if you desire the graphics files to be refreshed each time. We use PGchoicemacros.pl for commands to shuffle the images and thus randomize the answers.

####################################
#  Create blank canvases with labels
#
foreach $j (0..5) {

 $graph[$j] = init_graph(-10,-10,10,10,'axes'=>[0,0],
              'ticks'=>[10,10],'size'=>[400,400]);
 $graph[$j]->lb('reset');

   foreach ($i = 1; $i <= 4; $i++) {
     $graph[$j]->lb(new Label(2*$i,-.1,2*$i,'black','center','top'));
     $graph[$j]->lb(new Label(-2*$i,-.1,-2*$i,'black','center','top'));
     $graph[$j]->lb(new Label(-.1,2*$i,2*$i,'black','right','middle'));
     $graph[$j]->lb(new Label(-.1,-2*$i,-2*$i,'black','right','middle'));
   }

 $graph[$j]->lb(new Label(9.8,-0.1,"x",'black','right','top'));
 $graph[$j]->lb(new Label(-.1,9.8,"y",'black','right','top'));

}



#########################################
#  Manually add functions to the canvases
#

$f[0] = "\frac{-1}{x-5}-1";
plot_functions($graph[0],
"-1/(x-5)-1 for x in <-10,4.99> using color:blue and weight:2",
"-1/(x-5)-1 for x in <5.01,10>  using color:blue and weight:2",
);
#  vertical asymptote
$graph[0]->moveTo(5,-10);
$graph[0]->lineTo(5,10,'black',1,'dashed');
#  horizontal asymptote
$graph[0]->moveTo(-10,-1);
$graph[0]->lineTo(10,-1,'black',1,'dashed');


$f[1] = "\frac{x-2}{(x+1)(x-3)}";
plot_functions($graph[1],
"(x-2)/((x+1)*(x-3)) for x in <-10,-1.01>  using color:blue and weight:2",
"(x-2)/((x+1)*(x-3)) for x in <-0.99,2.99> using color:blue and weight:2",
"(x-2)/((x+1)*(x-3)) for x in <3.01,10>    using color:blue and weight:2",
);
#  vertical asymptote
$graph[1]->moveTo(-1,-10);
$graph[1]->lineTo(-1,10,'black',1,'dashed');
#  vertical asymptote
$graph[1]->moveTo(3,-10);
$graph[1]->lineTo(3,10,'black',1,'dashed');


$f[2] = "\frac{2x+4}{x-1}";
plot_functions($graph[2],
"(2x+4)/(x-1) for x in <-10,0.99> using color:blue and weight:2",
"(2x+4)/(x-1) for x in <1.01,10>  using color:blue and weight:2",
);
#  vertical asymptote
$graph[2]->moveTo(1,-10);
$graph[2]->lineTo(1,10,'black',1,'dashed');
#  horizontal asymptote
$graph[2]->moveTo(-10,2);
$graph[2]->lineTo(10,2,'black',1,'dashed');


$f[3] = "\frac{1}{x+1} + \frac{1}{x-3}";
plot_functions($graph[3],
"1/(x+1)+1/(x-3) for x in <-10,-1.01>  using color:blue and weight:2",
"1/(x+1)+1/(x-3) for x in <-0.99,2.99> using color:blue and weight:2",
"1/(x+1)+1/(x-3) for x in <3.01,10>    using color:blue and weight:2",
);
#  vertical asymptote
$graph[3]->moveTo(-1,-10);
$graph[3]->lineTo(-1,10,'black',1,'dashed');
#  vertical asymptote
$graph[3]->moveTo(3,-10);
$graph[3]->lineTo(3,10,'black',1,'dashed');


$f[4] = "\frac{1-x^2}{x-2}+3";
plot_functions($graph[4],
"(1-x^2)/(x-2)+3 for x in <-10,1.99> using color:blue and weight:2",
"(1-x^2)/(x-2)+3 for x in <2.01,10>  using color:blue and weight:2",
);
#  vertical asymptote
$graph[4]->moveTo(2,-10);
$graph[4]->lineTo(2,10,'black',1,'dashed');


$f[5] = "\frac{1-4x}{2x+2}";
plot_functions($graph[5],
"(1-4x)/(2x+2) for x in <-10,-1.01> using color:blue and weight:2",
"(1-4x)/(2x+2) for x in <-0.99,10>  using color:blue and weight:2",
);
#  vertical asymptote
$graph[5]->moveTo(-1,-10);
$graph[5]->lineTo(-1,10,'black',1,'dashed');
#  horizontal asymptote
$graph[5]->moveTo(-10,-2);
$graph[5]->lineTo(10,-2,'black',1,'dashed');



###############################################
#  Create figures that can be inserted directly
#
foreach $j (0..5) {
  $fig[$j] = image(insertGraph($graph[$j]), 
  width => 200, height => 200, tex_size => 310);
}


###############################################
#  Shuffle the figures
#
@perm = shuffle(6);
@inv = invert(@perm);
@fig = @fig[@perm];
@letter = ("A","B","C","D","E","F");

Setup: Generate a number of graphs to be included in the main text. Then, near the bottom, shuffle the graphs.

BEGIN_TEXT
\{
ColumnTable(
"Without a calculator, match each function with its graph A-F ".
"by finding the zeros, asymptotes, and end behavior for each function.".
$BR.$BR.pop_up_list(["?",@letter]).$SPACE."\( \displaystyle y = $f[0] \)".
$BR.$BR.pop_up_list(["?",@letter]).$SPACE."\( \displaystyle y = $f[1] \)".
$BR.$BR.pop_up_list(["?",@letter]).$SPACE."\( \displaystyle y = $f[2] \)".
$BR.$BR.pop_up_list(["?",@letter]).$SPACE."\( \displaystyle y = $f[3] \)".
$BR.$BR.pop_up_list(["?",@letter]).$SPACE."\( \displaystyle y = $f[4] \)".
$BR.$BR.pop_up_list(["?",@letter]).$SPACE."\( \displaystyle y = $f[5] \)",# comma!
BeginTable().
  AlignedRow([$fig[0],$fig[1],$fig[2]]).
  TableSpace(5,0).
  AlignedRow(["A","B","C"]).
  TableSpace(25,6).
  AlignedRow([$fig[3],$fig[4],$fig[5]]).
  TableSpace(5,0).
  AlignedRow(["D","E","F"]).
EndTable().
$BR.$BCENTER."(Click on a graph to enlarge it)".$ECENTER, # comma!
indent => 0, separation => 10, valign => "TOP"
)
\}
END_TEXT

Main Text: We use ColumnTable( columnA, columnB, options ) to display the text and answer blanks in the left column, and the graphs in the right column. Notice that within each column, every line except for the last ends with a period to join together things like strings, answer rules, etc. The last line in both columns ends with a comma, as noted in the commented out portions # comma!.

install_problem_grader(~~&std_problem_grader);
$showPartialCorrectAnswers = 0;

foreach $j (0..5) {
ANS( str_cmp($letter[$inv[$j]]) );
}
      
ENDDOCUMENT();

Answer Evaluation: To keep students from guessing, we use the all-or-nothing grader std_problem_grader and set $showPartialCorrectAnswers = 0; to withhold feedback.



Problem Techniques Index


\{ imageRow( ["graph1.png","graph2.png"], 
["A","B"], width=>250, height=>250, tex_size=>450 ) \}

\{ imageRow( ["g1.png","g2.png","g3.png"], 
["A","B","C"], width=>200, height=>200, tex_size=>310 ) \}

\{ imageRow( ["g1.png","g2.png","g3.png","g4.png"], 
["A","B","C","D"], width=>150, height=>150, tex_size=>200 ) \}

The imageRow function displays multiple images in a row. Its first argument is a list of image files and its second argument ["A","B"] is a list of caption names. The double quotes in caption names are necessary, for example, to prevent the string "E" from being interpreted as E = 2.718...