Difference between revisions of "ProblemsWithUnits"

From WeBWorK_wiki
Jump to navigation Jump to search
m
 
(4 intermediate revisions by 2 users not shown)
Line 10: Line 10:
   
 
<p style="text-align:center;">
 
<p style="text-align:center;">
[[IndexOfProblemTechniques|Problem Techniques Index]]
+
[[Problem_Techniques|Problem Techniques Index]]
 
</p>
 
</p>
   
Line 36: Line 36:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
$numAns = NumberWithUnits(
 
  +
Context()->variables->are(t=>"Real");
"(1/32)(25 + sqrt(25^2 + 32*16)) s");
 
  +
$fxnAns = FormulaWithUnits(
 
  +
$time = NumberWithUnits(
"8 + 25x - 16x^2 ft/s");
+
"(1/32)(25 + sqrt(25^2 + 32*16)) s");
  +
$height = FormulaWithUnits(
  +
"8 + 25t - 16t^2 ft/s");
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<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;x&nbsp;-&nbsp;16&nbsp;x^2","ft/s")</code>.
 
  +
(Note that in this problem we're using time as our variable, so we reset the Context to
  +
[[VariablesOtherThanX|use a variable other than x]].)
  +
</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>
 
</p>
 
</p>
 
</td>
 
</td>
Line 51: Line 55:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
BEGIN_TEXT
+
BEGIN_TEXT
The equation for the height is
+
The equation for the height is
\{ ans_rule(35) \}.
+
\(h(t) = \) \{ ans_rule(35) \}.
$BR
+
$BR
${BITALIC}(Include units in your answer!)$EITALIC
+
Please include \{ helpLink('units') \} in your answer.
$PAR
+
$PAR
The object hits the ground when
+
The object hits the ground when
\(x = \) \{ ans_rule(35) \}
+
\(t = \) \{ ans_rule(35) \}
${BITALIC}(Include units in your answer!)$EITALIC
+
${BITALIC}(Include units in your answer!)$EITALIC
END_TEXT
+
END_TEXT
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
Line 71: Line 75:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( $func->cmp() );
+
ANS( $height->cmp() );
ANS( $time->cmp() );
+
ANS( $time->cmp() );
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
Line 108: Line 112:
 
BEGIN_TEXT
 
BEGIN_TEXT
 
The object hits the ground when
 
The object hits the ground when
\(x = \) \{ ans_rule(35) \}
+
\(t = \) \{ ans_rule(35) \}
 
${BITALIC}(Include units in your answer!)$EITALIC
 
${BITALIC}(Include units in your answer!)$EITALIC
 
END_TEXT
 
END_TEXT
Line 132: Line 136:
   
 
<p style="text-align:center;">
 
<p style="text-align:center;">
[[IndexOfProblemTechniques|Problem Techniques Index]]
+
[[Problem_Techniques|Problem Techniques Index]]
 
</p>
 
</p>
   

Latest revision as of 11:13, 20 June 2013

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