Writing a Graphing Problem

Every WebWork problem should begin with the following code.  Recall that the ## simply denotes a comment line that is not read by the compiler.  This problem is a sample graphing problem (graphing problem).  The complete source code for this problem can be found here. Source code

## DESCRIPTION

## Algebra

## ENDDESCRIPTION


## KEYWORDS('algebra','graph','line','slope')


## DBsubject('Algebra')

## DBchapter('Coordinates and Graphs')

## DBsection('Lines')

## Date('7/20/07')

## Author('N. Wakefield')

## Institution('University of Northern Colorado')

## TitleText1('')

## EditionText1('')

## AuthorText1('')

## Section1('')

## Problem1('')


The next section marks the beginning of actual PG code.


DOCUMENT();        # This should be the first executable line in the problem.


After you have typed the  DOCUMENT();  every line will  need to end with a semicolon.



Once you have begun the document you should load all of the appropriate macros.  However, it is a good practice to only load the macros that you will be using.  Also, if you are editing a WebWork problem you should not change any of the macros.



loadMacros(

   "PG.pl",

   "PGbasicmacros.pl",

   "PGchoicemacros.pl",

   "PGanswermacros.pl",

   "PGauxiliaryFunctions.pl",

   "PGgraphmacros.pl",

);


Now that the macros have been loaded we are ready to begin coding the problem.  The code below grabs three random numbers between the number -2 and 2.  The step size for these numbers is one.


$x1 = non_zero_random(-2,2,1);

$y1 = non_zero_random(-2,2,1);

$y2 = non_zero_random(-2,2,1);


In the next line we begin writing the code that will create our graph.  $graph1 is just a variable that we will  use to denote our coordinate plane.  init_graph tells the computer to initialize a graph.  The numbers after init_graph represent the minimum and maximum x and y values.  'axes'=>[0,0] tells the computer to place the axis at the origin.  'grid'=>[10,10] tells the computer how many lines to draw in the x axis and how many lines to draw in the y axis.  'size'=>[200,200] tells the computer to make the picture 200 by 200 pixels.


$graph1 = init_graph(-5,-5,5,5,'axes'=>[0,0],'grid'=>[20,20],'size'=>[200,200]);


At this point we have created a graphic object and stored that object in $graph1.  However, notice that we have not placed this object on the screen.  We will place the object later.  The next thing we need to do is write out the function we would like to graph.  


We are going to store our function in $f.  FEQ essentially tells the computer to transform whatever is written in the parenthesis into an equation.  Recall that $x1, $y1, $y2  all represent random numbers.  Thus our function is an absolute value that is stretched and translated.  One of the nice features of WebWork is that each student will be given a different function.  However, if a student tries a problem then decides to take a break and logs out the student will still be able to access the same problem (the numbers do not change for the same student).  The command for x in [-5,5] tells the computer where it will be graphing the equation.  using weight:1" tells the computer how thick of a line to use when drawing the function.


$f= FEQ("($x1*abs(x+$y1)-$y2) for x in [-5,5] using weight:1");


Notice that when we defined $f we defined a graphics object.  However, when we ask the students for a response we would like them to actually write out a function.  Thus we also need to define a variable that will compare what the students write to what the correct answer should be.  This variable will be $ans.


$ans= " $x1*abs(x+$y1)-$y2 ";

Notice that this is exactly the same function as we used in $f, except now we don't have FEQ.

Once the answer function has been created we should add the graph of the function to our $graph1 object.  The plot_functions()  operation will combine the second entry with the first entry and store the new object in the first entry.  Thus plot_functions($graph1,$f); combines $graph1 and $f  and stores the new object in $graph1.


plot_functions($graph1,$f);


Now that we have set up all of the background functions we can begin writing the problem.  Everything between 

BEGIN_TEXT 

END_TEXT

represents the actual text of the problem.  $BR creates a page break.  $PAR  creates a paragraph break.  Recall that \{ \} denotes the begging and the end of Perl code. 


The command  image(insertGraph($graph1),width=>400,height=>400,tex_size=>850) places our graphics object on the page with the dimensions specified.


The final part of the question asks the actual question then has a command which looks like \{ans_rule(10)\}.  ans_rule tells the computer to create an answer box.  The number 10 tells the computer how long to make the answer box.


BEGIN_TEXT

Find an equation for the graph sketched.  Remember to use abs() for the absolute value, and sqrt() for the square root.

$BR

$PAR \{ image(insertGraph($graph1),width=>400,height=>400,tex_size=>850) \} $PAR

$BR

The equation is \(y=\) \{ans_rule(10)\}.

$BR


END_TEXT


The command  $showPartialCorrectAnswers = 1; specifies whether or not the computer will show partial correct answers a zero means no and a one means yes.

$showPartialCorrectAnswers = 1;


The final step in the problem is to check whether the student got the problem right or not,  ANS(function_cmp($ans,"x", limits=>[-5,5],.25)); does this for us. The function operates like this ANS(function_cmp("what is the correct answer","what variable are you using", limits=>["where would you like to evaluate the answer"],"what percentage error can the student have and still get credit.));

function_cmp compares the student equation to the equation specified at several random points in the limit interval.  


ANS(function_cmp($ans,"x", limits=>[-5,5],.25));


The final command tells the computer that the problem is done.


ENDDOCUMENT();        # This should be the last executable line in the problem.