WeBWorK Problems

Declaring a variable to be positive

Declaring a variable to be positive

by Ted Shifrin -
Number of replies: 1
Is there a way to define a variable as a positive real, rather than just real, so that when a student enters sqrt(r^2) it will be accepted as the "official" answer r ? (This comes up naturally in limits with cylindrical coordinates, for example.)
In reply to Ted Shifrin

Re: Declaring a variable to be positive

by Paul Pearson -
Hi,

I'm assuming what you want is for the student's answer to be evaluated by substituting only positive values for the variable r. This can be done easily using MathObjects by setting the limits for the variable r in the problem source (the PG file). There are several ways to do this, detailed at

http://webwork.maa.org/wiki/FormulaTestPoints

For example:

---- begin pg code ----
Context("Numeric")->variables->are(r=>"Real"); Context()->variables->set(r=>{limits=>[1,2]});

$f = Formula("r");

---- end pg code ----
Personally, I would like to have my students simplify sqrt(r^2) to r in a context where r is supposed to be non-negative, so I would be inclined to add a few negative test points to the domain of formula evaluation in order to encourage students to simplify their answer further.

---- begin pg code ----
$f->{test_at} = [[-3],[-2],[-1],[1],[2],[3]];
---- end pg code ----

This could be taken one step further by providing customized answer hints.

http://webwork.maa.org/wiki/AnswerHints

For example:
---- begin pg code ----

loadMacros(...,"answerHints.pl",...);


... insert pg code from above ...


ANS( $f->cmp() ->withPostFilter(AnswerHints( Formula("sqrt(r^2)") => "Simplify your answer further", )) );

---- end pg code ----

Good luck!

Paul Pearson