SampleProblem3
A Third WeBWorK Sample Problem: Graphics
This sample problem shows how to write problems that include simple dynamically generated graphs.
Recall that a WeBWorK PG file has five sections: (1) a tagging and description section, (2) an initialization section, (3) a problem setup section, (4) a text section, and (5) an answer and solution section. These sections are shown below, and the parts of each of each that are related to the different problem types being discussed here are indicated.
The sample file explained below is this file (need attachment).
PG problem file  Explanation 

# DESCRIPTION # A simple sample problem that illustrates # how to include dynamically generated graphics # in a problem. # WeBWorK problem written by Gavin LaRose, # <glarose(at)umich(dot)edu> # ENDDESCRIPTION ## DBsubject('WeBWorK') ## DBchapter('Demos') ## DBsection('Problem') ## KEYWORDS('graphs') ## TitleText1('') ## EditionText1('') ## AuthorText1('') ## Section1('') ## Problem1('') ## Author('Gavin LaRose') ## Institution('UMich') 
This is the tagging and description section of the problem. Recall that the tagging information exists to allow the problem to be easily indexed. Lists of tag values are online: current chapter and section names, and current keywords. The list of keywords should be comma separated and quoted (e.g., KEYWORDS('calculus,derivatives')). 
DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", "PGgraphmacros.pl", ); 
This is the initialization section of the problem. Note that we need to include 
# make sure we're in the context we want Context("Numeric"); # we define a parabola to graph, making sure # that the x and y intercepts are nice $root1 = random(2,4,2); $root2 = $root1 + random(2,4,2); $func = Compute("(1/8)(x$root1)(x$root2)"); # the minimum and maximum y values this # reaches on the domain [2,$root2+1] are $ymin = $func>eval(x=>2); $ymax = $func>eval(x=>($root1+$root2)/2); # the domain is $xmin = 2; $xmax = $root2 + 1; # and the number of gridlines we graph $xgrid = $xmax + 2; $ygrid = $ymax  int($ymin) + 2; # now initialize the graph $graph = init_graph($xmin, int($ymin)1, $xmax, $ymax+1, axes=>[0,0], grid=>[$xgrid,$ygrid], size=>[150,150]); # and add the function to be graphed plot_functions( $graph, "$func for x in " . "<$xmin,$xmax> using color:blue " . "and weight:2"); # for use below: the yintercept is $yint = $func>eval(x=>0); 
This is the problem setup section of the problem.
We first define a MathObject to be the function we want. We will graph it on the domain
Then we define the graph. This is first initialized; the required syntax is
Another commonly used option is
Then we add the function to the graph. The string that is used in this case always has the form indicated here:
One other note here: to break the string over multiple lines, we've used Perl's concatenation operator,
is the same as saying

TEXT(&beginproblem); Context()>texStrings; BEGIN_TEXT Consider the graph $BR $BCENTER \{ image( insertGraph($graph), tex_size=>100, height=>150, width=>150, extra_html_tags=>'alt="graph of a ' . 'downward opening parabola with ' . 'xintercepts ' . $root1 . ' and ' . $root2 . ', and yintercept ' . $yint . '."' ) \} $ECENTER $PAR What is the equation of this parabola? $BR \( y = \) \{ ans_rule(35) \} END_TEXT Context()>normalStrings; 
This is the text section of the problem. Note that we've use a couple of formatting variables here:
The graph is inserted with the

ANS( $func>cmp() ); Context()>texStrings; SOLUTION(EV3(<<'END_SOLUTION')); $PAR SOLUTION $PAR Because we know that this is a parabola, and because we know that the xintercepts are \(x = $root1\) and \(x = $root2\), we know that the equation of the function must be \[ y = a (x  $root1) (x  $root2) \] for some constant \(a\). The yintercept is \(y = $yint\), which occurs when \(x = 0\), so we must have \(a($root1)($root2) = $yint\), so \(a = \frac{1}{2}\), and our function is \[ y = $func. \] END_SOLUTION Context()>normalStrings; ENDDOCUMENT(); 
This is the answer and solution section of the problem. Note that we use the same formatting conventions in the solution that are used in the text section of the problem. 