Difference between revisions of "ProblemsWithUnits"

From WeBWorK_wiki
Jump to navigation Jump to search
m
Line 50: Line 50:
 
</p>
 
</p>
 
<p>
 
<p>
In the problem set-up section of the file, we define a numerical answer as a <code>NumberWithUnits</code> MathObject, and similarly for a <code>FormulaWithUnits</code>. 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., <code>FormulaWithUnits("8&nbsp;+&nbsp;25&nbsp;t&nbsp;-&nbsp;16&nbsp;t^2","ft/s")</code>.
+
In the problem set-up section of the file, we define a numerical answer as a <code>NumberWithUnits</code> MathObject, and similarly for a <code>FormulaWithUnits</code>. 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., <code>FormulaWithUnits("8&nbsp;+&nbsp;25&nbsp;t&nbsp;-&nbsp;16&nbsp;t^2","ft/s");</code>.
 
</p>
 
</p>
 
</td>
 
</td>

Revision as of 15:59, 2 January 2010

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.

  Context()->variables->are(t=>"Real");

  $time = NumberWithUnits(
  "(1/32)(25 + sqrt(25^2 + 32*16)) s");
  $height = FormulaWithUnits(
  "8 + 25t - 16t^2 ft/s");

(Note that in this problem we're using time as our variable, so we reset the Context to use a variable other than x.)

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 t - 16 t^2","ft/s");.

  BEGIN_TEXT 
  The equation for the height is 
  \(h(t) = \) \{ ans_rule(35) \}. 
  $BR
  Please include \{ helpLink('units') \} in your answer.
  $PAR
  The object hits the ground when 
  \(t = \) \{ 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( $height->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 
  \(t = \) \{ 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