From WeBWorK_wiki
Jump to navigation Jump to search

A Second WeBWorK Sample Problem: Problem Types

This sample problem shows how to write three commonly used problem types: formulas making use of more of the MathObjects Formula functionality, multiple choice, and string entry problems.

Recall that a WeBWorK PG file has five sections: (1) a tagging and description section, (2) an initialization section, (3) a problem set-up 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
# A simple sample problem that illustrates 
# three common problem types.
# WeBWorK problem written by Gavin LaRose, 
# <glarose(at)umich(dot)edu>

## DBsubject('WeBWorK')
## DBchapter('Demos')
## DBsection('Problem')
## TitleText1('')
## EditionText1('')
## AuthorText1('')
## Section1('')
## Problem1('')
## Author('Gavin LaRose')
## Institution('UMich')

This is the tagging and description section of the problem.

All of the tagging information exists to allow the problem to be easily indexed. Because this is a sample problem there isn't a textbook per se, and we've used some default tagging values. There is an on-line list of current chapter and section names and a similar list of keywords, as well as a page of best practices for tagging problems. The list of keywords should be comma separated and quoted (e.g., KEYWORDS('calculus','derivatives')).


This is the initialization section of the problem. To use the multiple choice object we use in this problem, we have to add to the list of macros files that we load.

# make sure we're in the context we want

# set up a function for our formula problem.
$a = random(2,9,1);
$func = Formula("cos($a t)");
$funcDeriv = $func->D('t');
$m = $funcDeriv->eval(t=>2);
$y0 = $func->eval(t=>2);
$line = Formula("$m (t - 2) + $y0");

# set up for a multiple choice problem.
$radio = new_multiple_choice();
$radio->qa("This problem is", "easy");
$radio->extra("very easy", "hard");

$strAns = String('True');

This is the problem set-up section of the problem.

For part 1, we define a function of the variable t. By default, the only defined variable is x, so we first add the real variable t to the Context. A list of commonly used Context modifications is available. Then we define the function, find its derivative, and work out the equation of the tangent line to the function.

Note: we're using a number of characteristics of Formulas here. $f->D('t') finds the derivative of $f with respect to t. Then, $f->eval(t=>2) evaluates $f at the point t=2. Note that values must be specified for all variables when calling eval.

For part 2, we define a radio object to include a multiple choice problem. The $radio->qa("question","correctAnswer") line gives the question and answer to the question. Then $radio->extra("answer","answer",...) defines extra (incorrect) answers for the problem. By using $radio->makeLast("lastAnswer") we can specify which answer is displayed last (all others will be presented in a random order). To make the correct answer be the last one, just call $radio->makeLast("correctAnswer") anytime after the initial $radio->qa("question","correctAnswer") call.

For part 3, we are asking the student to enter a String answer. To avoid error messages we first add to the Context all valid strings (in this case, the strings "True" and "False"). Then we define a String object which gives the correct answer.

Find the equation of the line tangent to 
\(f(t) = $func\) at \(t=2\).
\(y = \) \{ ans_rule(35) \} (note that this 
must be a function of \(t\).)

\{ $radio->print_q() \}
\{ $radio->print_a() \}

(Enter True or False: ) All instructors 
love WeBWorK.  \{ ans_rule(6) \}


This is the text section of the problem. Note that we use a couple of formatting variables here: ${BBOLD} begins a bold section of text, and $EBOLD ends it. $BR inserts a line break, and $PAR inserts a paragraph break.

For Part 1, we display the equation in-line with \( f(t) = $func\ ) and then ask for the answer with an answer rule.

For Part 2, we can just use the radio button object to print the question and options. Recall that \{ \} executes code in the text section; $radio->print_q() (and ...print_a()) are methods of the radio object that print the question and answers.

For Part 3, we just ask a question and insert an answer rule.

ANS( $line->cmp() );
ANS( radio_cmp( $radio->correct_ans() ) );
ANS( $strAns->cmp() );

The derivative of \(f(t) = $func\) is 
\(f'(t) = $funcDeriv\), so the slope of 
the line is \(f'(2) = $m\).  Then when
\(t = 2\) we have \(f(2) = $y0\), so the
equation of the line is \(y = $line\).

This problem is clearly easy.

A careful study of one WeBWorK 
instructor showed that 100% of all
respondents thought this statement
was true.


This is the answer and solution section of the problem. Because there are three parts to the question, we have three ANS() calls. These evaluate the answer blanks in the order that they appeared in the problem.

For the first part of the problem, we want to check that the student entered the right equation for the line. We therefore use the comparison method of the $line Formula we defined.

For the second part of the problem we have to check that the student entered the correct (multiple-choice) answer. At the moment this requires that we use an old-style answer checker, radio_cmp. This just verifies that the answer that the student submitted is the same as the correct answer to the problem.

For part 3, we just check that the answer the student submitted compares with the expected String.

Finally, note that we can use any formatting that we use in the text section of the problem in the solution.