WeBWorK Problems

Trouble with answers including units in PGML

Trouble with answers including units in PGML

by Jim Fischer -
Number of replies: 1
I am having trouble incorporating units in the answer checker with PGML and could use some help troubleshooting. I read some posts at the PREP workshop forum on problem authoring and checked out some other examples in the PGML documentation. There seem to be several variations of syntax using "NumberWithUnits" and "FormulaWithUnits". Some use double quotes, some single, some a combination of no quotes and some quotes.

For example I have seen:
$Temp = NumberWithUnits(" 5 ft");

Also,

$Temp = NumberWithUnits("$g", "m/s");
# (where $g has been defined already)


WeBWorK doesnt like the following snippet and gets hung up on the lines that have NumberWithUnits or FormulaWithUnits. Ive tried a lot of variations by including or not including quotes around expresions.

Is there an easy fix?

-----------------------------------------------------------------------------

DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGML.pl",
"MathObjects.pl",
"PGcourse.pl",
"parserNumberWithUnits.pl",
"parserFormulaWithUnits.pl",
"contextArbitraryString.pl",
"parserMultiAnswer.pl",
"parserPopUp.pl",
"contextInequalities.pl",
"PGgraphmacros.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;

Context("Numeric") ;
Context()->flags->set(tolerance => 0.01);
Context()->variables->add(v => "Real");
Context()->variables->add(k => "Real");
Context()->variables->add(t => "Real");


$g = 9.8;
$mass = random(18, 40, 2);

$DE_UP = Formula("-$g - k*v**2/$mass ");
$DE_DWN = Formula("-$g + k*v**2/$mass ");

$Temp = Compute("-sqrt($g*$mass/k)");
Terminal = FormulaWithUnits($Temp, 'm/s');

$K = random(0.7,0.9,0.05);

$Temp2 = -sqrt($g*$mass/$K) ;
$Terminal2 = NumberWithUnits($Temp2,'m/s');


BEGIN_PGML
[``\frac{dv}{dt} = ``] [_______________________]{"$DE_UP"}

[`` \frac{dv}{dt} = ``][_______________________]{"$DE_DWN"}

[``\lim_{t\rightarrow \infty} v(t) = ``] [_______________________]{"$Terminal"}
[@ helpLink("units") @]*

[``\lim_{t\rightarrow \infty} v(t) = ``] [_______________________]{" $Terminal2"} [@ helpLink("units") @]*

END_PGML

######################################################################

ENDDOCUMENT();






In reply to Jim Fischer

Re: Trouble with answers including units in PGML

by Davide Cervone -
In the PGML block, don't enclose the correct answers in quotation marks if the variables already hold MathObjects. That causes the MathObject to be stringified and re-parsed, but since units are not parsed except by NumberWithUnits() or FormulaWithUnits(), these answers won't produce the correct MathObjects when re-parsed.

You only need to use quotation marks around the answer if it is a formula that hasn't been parsed into a MathObject yet (e.g., [____]{"x^2+3x-2"}.

Similarly, for your example $Temp = NumberWithUnits("$g", "m/s"); , there is no need to put $g in quotation marks, in general.

Finally, note that the definition of Terminal is missing the dollar sign (it should be $Terminal = ...