From WeBWorK_wiki
Jump to navigation Jump to search

Dynamically Generated Graphs With Filled Regions (Shading)

Click to enlarge

This PG code shows how to create a dynamically generated graph with a shaded region.

Templates by Subject Area

PG problem file Explanation

Problem tagging data

Problem tagging:




$refreshCachedImages = 1;

Initialization: Dynamically generated graphs require, and we will use to put the text and the graph side-by-side in two columns. It is important to set $refreshCachedImages = 1 if you want the image to be generated and delivered every time the page is refreshed, otherwise a cached image may be used.


$a = random(0,3,1);
$f = Formula("sqrt(x)+$a");

$answer = Compute("(2/3) * (4^(3/2) - 1) + 3*$a");

#  Graph canvas
$gr = init_graph(-5,-5,5,5,grid=>[10,10],axes=>[0,0],pixels=>[300,300]);

#  Graph labels
foreach my $j (1..4) {
  $gr->lb( new Label(-4.7,  $j, $j,'black','center','middle'));
  $gr->lb( new Label(-4.7, -$j,-$j,'black','center','middle'));
  $gr->lb( new Label(  $j,-4.7, $j,'black','center','middle'));
  $gr->lb( new Label( -$j,-4.7,-$j,'black','center','middle'));
$gr->lb( new Label(4.7,0.2,'x','black','center','middle'));
$gr->lb( new Label(0.2,4.7,'y','black','center','middle'));

#  Define new graph colors
$gr->new_color("lightblue", 214,230,244); # RGB
$gr->new_color("darkblue",  100,100,255);
$gr->new_color("darkgreen",   0, 86, 34);
$gr->new_color("lightred",  245,234,229); # light red-purple
$gr->new_color("darkred",   159, 64, 16); # red-brown

#  Choose colors
$light = "lightblue";
$dark = "darkblue";

#  Graph the function and the filled region
"$f  for x in <0,5> using color:$dark and weight:2");

Setup: For more details on the graph object $gr, see the help documents for graphs in the index of problem techniques.

We defined more colors than are actually used so that you have some options to choose from.

"Use the graph to find the area of the shaded
region under \( f(x) = $f \).
Area = " . 
image( insertGraph($gr),height=>300,width=>300,tex_size=>800 ).
"Graph of \( y = f(x) \)".
indent => 0, separation => 30, valign => "TOP"

Main Text: We use a two column format provided by ColumnTable(first column, second column, options). In each column, we use the string concatenation operator . quite a bit to join together strings and other methods because the entire ColumnTable is inside Perl mode curly braces \{ \}. To make the separation between the columns easier to read, we have put two commas on their own lines. Notice that there is no period before any of the commas.

Using a ColumnTable is best because in HTML mode the text and graph will be side by side, while in the PDF output the columns will be stacked on top of each other so that the text is on top of the graph. Pay attention to the size of your graph in HTML and in PDF output, and make it big enough to read easily in either format.

Many existing questions generate image files with larger dimensions than necessary, but scale them down so as to be unreadable in HTML and PDF output, which makes them hard or impossible to use. If you have the time to make a graph, take the extra time to make it an appropriate size in both HTML and PDF output.

$showPartialCorrectAnswers = 1;

ANS( $answer->cmp() );

Answer Evaluation:

Solution explanation goes here.

COMMENT('MathObject version.');



Templates by Subject Area