ProblemsWithUnits

From WeBWorK_wiki
Revision as of 13:17, 1 February 2008 by Glarose (talk | contribs) (New page: <h2>Units in Problems: PG Code Snippet</h2> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>This code snippet shows the essential PG code to require student a...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Units in Problems: PG Code Snippet

This code snippet shows the essential PG code to require student answers that include units with their answers. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Note that it is possible to incorporate units with both the newer MathObjects and with the "old-style" answer evaluators. The former are more flexible, so we show those here. A second sample with the "old-style" answer evaluators appears below this.

Problem Techniques Index

PG problem file Explanation
  loadMacros(
  "parserNumberWithUnits.pl",
  "parserFormulaWithUnits.pl",
  );

To make the answer to the problem be a number with units, we need to load the file parserNumberWithUnits.pl; to include a formula with units, not surprisingly, we need parserFormulaWithUnits.pl. So we include these in the initialization section of our PG source file.

  $numAns = NumberWithUnits(
  "(1/32)(25 + sqrt(25^2 + 32*16)) s");
  $fxnAns = FormulaWithUnits(
  "8 + 25x - 16x^2 ft/s");

In the problem set-up section of the file, we define a numerical answer as a NumberWithUnits MathObject, and similarly for a FormulaWithUnits. The units are specified at the end of the input string, and it may be necessary to include a space between the answer and the units. (Note that we've introduced a linebreak in both of these definitions to make it format better in the double-column format we're using here; it's not necessary and should, perhaps, be omitted). An alternate call to either of these includes the units as a second argument, e.g., FormulaWithUnits("8 + 25 x - 16 x^2","ft/s").

  BEGIN_TEXT 
  The equation for the height is 
  \{ ans_rule(35) \}. 
  $BR
  ${BITALIC}(Include units in your answer!)$EITALIC
  $PAR
  The object hits the ground when 
  \(x = \) \{ ans_rule(35) \}
  ${BITALIC}(Include units in your answer!)$EITALIC
  END_TEXT

The text section of the problem is unchanged, except that it's a good idea to remind students that they are required to include units in their answer.

  ANS( $func->cmp() );
  ANS( $time->cmp() );

Finally, in the answer and solution section of the file, the MathObjects know how to check units, and will correctly deal with the case where a student enters a correct answer with different units (e.g., m/s instead of ft/s).

It is also possible to include problems that require units with "old-style" answer checkers. However, the "old-style" checkers do not deal with Formulas with units! So only numerical answers with units can be checked if we eschew MathObjects. In any event, the following code snippet illustrates this.

PG problem file Explanation
  $time = "(1/32)(25 + sqrt(25^2 + 32*16))";

In this case we don't need to include any additional tagging or description information, nor any additional initialization. We define the numeric value that we're looking for normally (without units) in the set-up section of the problem.

  BEGIN_TEXT
  The object hits the ground when 
  \(x = \) \{ ans_rule(35) \}
  ${BITALIC}(Include units in your answer!)$EITALIC
  END_TEXT

As before, it's a good idea to remind students that they will be required to enter units when we put the text of the problem in.

  ANS( num_cmp( $time, units=>"s" ) );

And we define the answer evaluator with num_cmp, including units as an option in the call to the evaluator. As before, a student answer that is correct but which uses different units will be marked correct.

Problem Techniques Index