WeBWorK Problems

Declaring a variable to be positive

Re: Declaring a variable to be positive

by Paul Pearson -
Number of replies: 0

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


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.


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


... 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