### Re: Absolute Value in Logarithmic Functions Student Answers

by Danny Glin -### Re: Absolute Value in Logarithmic Functions Student Answers

by Davide Cervone -$f = Formula("|x|")->with(limits->[-5,5]);it is possible that all the randomly selected test points will be positive. So it is best to use

`test_at`

in this case, as well.
Note that with your limits, even though you force all negative values of `x`

, that means you can still accept incorrect answers as correct. For example, `ln(-x-sqrt(x^2-4))+C`

would be marked correct in the case where `$a`

is 2. So you really do need to use the `test_at`

approach to make sure you have the needed values on both sides.

But there is one caveat: since you have added `a`

as a variable, there are *two* variables in the context, and so when you give the test points, you need to have a value for *both* of these. This is true even in formulas that don't include both variables, since the student answer *can* use both (because both are part of the context). They are listed in alphabetical order, so the first coordinate is for `a`

and `x`

is second. So you want do do something like

$ans1 = FormulaUpToConstant("ln(abs(x+sqrt(x^2-$a^2)))+C")->with(limits => [-45,-38], test_at => [[$a,$a+2],[$a,$a+3]])->reduce();You will also have to do the same sort of thing for the formula involving

`a`

rather than the value of `a`

. I think that `limits => [-45, -36]`

applies to both the `x`

and the `a`

, so it is possible that you would get points where `a > x`

, and the root will be undefined. WeBWorK will try again and look for additional points, but it is possible that this will happen enough times that WeBWorK will give up, so you might want to give the limits for `a`

when you create it, so that they are always less than `x`

, via something like
Context()->variables->set(a => {limits => [-2, 2]});

Finally, Danny has also suggested `AnswerHints()`

as a way to provide a warning message when the student forgets the absolute values. In order to do that in PGML, one approach is to set `$ans1`

to the answer checker itself. For example:

$ans1 = $ans1->cmp()->withPostFilter(AnswerHints( FormulaUpToConstant("ln(x+sqrt(x^2-$a^2))+C")->with(limits=>[6,10]) => "What happens if x is negative?" ));Note that the answer hint will only run when the answer is incorrect (by default), and so you don't have to worry about matching the correct answer, here. But you do have to set the limits to values of

`x`

that are in the domain of the function.
Hope that helps.

### Re: Absolute Value in Logarithmic Functions Student Answers

by Brittni Lorton -Context()->variables->set(a => {limits => [-2, 2]});

to have fixed limits for a. And then I chose to use

$ans1=FormulaUpToConstant("ln(abs(x+sqrt(x^2-$a^2)))+C")->with(limits => [[$a,-$a-7],[$a,-$a-2]], test_at => [[$a,$a+2],[$a,$a+3]])->reduce();

$ans1 = $ans1->cmp()->withPostFilter(AnswerHints( FormulaUpToConstant("ln(x+sqrt(x^2-$a^2))+C")->with(limits=>[6,10]) => "Are you missing an absolute value somewhere?" ));

$ans2=FormulaUpToConstant("ln(abs(x+sqrt(x^2-a^2)))+C")->with(limits => [[1,-6],[1,-2]], test_at => [[1,2],[1,6]])->reduce();

$ans2 = $ans2->cmp()->withPostFilter(AnswerHints( FormulaUpToConstant("ln(x+sqrt(x^2-a^2))+C")->with(limits=>[6,10]) => "Are you missing an absolute value somewhere?" ));to create the set limits and the AnswerHints.

### Re: Absolute Value in Logarithmic Functions Student Answers

by Brittni Lorton -### Re: Absolute Value in Logarithmic Functions Student Answers

by Davide Cervone -

*But those two are the same functions up to a constant.*
These answers differ by `ln|a|`

, but in the problem as you have coded it, this is *not* a constant, since `a`

is a *variable*, not a constant. You are thinking of `a`

as a constant, but you have added it to the context as a variable.

Try using

Context()->constants->add(a => sqrt(1/e)); # a value unlikely to be used by studentsinstead of

Context()->variables->add(a => "Real");and see if that helps. You will need to change the test points, since

`a`

is no longer a variable, but since you haven't includes an updated copy of the problem code, I will leave that to you.
### Re: Absolute Value in Logarithmic Functions Student Answers

by Davide Cervone -
*Does it mean that the limits for a are [1, 3] and the limits for x are [2,6]
OR does it mean the limits for a are [1,2] and the limits for x are [3,6]?
*

It is the first of the two.

Note, however, that for a `FormulaUpToConstant()`

, there is also a variable `C`

that is added to the context automatically. Since the variables are handled in ASCII order, they are `C`

, `a`

, `x`

, so in this case, you have set `C`

to `[1,3]`

and `a`

to `[2,6]`

, and because you don't give a third set, `x`

gets the first value in your list, `[1,3]`

.

For this reason, it is often best to set the limits in the context rather than in `with()`

calls, as I indicated above:

Context()->variables->set(a => {limits => [1,3]});

This may also affect the coordinates of the test points, but I haven't checked (and it has been many years since I wrote that section of the code, so can't remember of the top of my head).