WeBWorK Problems

Questions about disabling/enabling functions

Questions about disabling/enabling functions

by Larry Riddle -
Number of replies: 3

I am writing a problem about integration using trig substitution. I am looking for more than just the final result and trying to get students to work through the various steps in the process. The code below has been shortened to just contain the lines that pertain to the following questions.

Using int(sqrt(4-x^2))dx as an example, the trig substitution x = 2sin(t) results in the trig integral int(4cos^2(t))dt. To get to that result, the student should be substituting in the original integral to get a new trig integrand of sqrt(4-4sin^2(t))*(2cos(t)).

This is a "correct" answer (since cos(t)>=0 in this process), but not yet a useful one for the next step of evaluating that trig integral. So I would like the student to simplify that to 4cos^2(t) to get the answer marked as correct. Thus I have been playing around with using

Context()->functions->disable("sqrt");

That has given rise to a few questions about using this method.

The document https://webwork.maa.org/wiki/Introduction_to_Contexts says that disable will make the function unavailable to the student but "the function is still recognized by MathObjects". However, if I declare

$f = sqrt(4-x^2);

after the disable line, I get an error message that 'sqrt" is not allowed in this context, so it seems that the sqrt function is not being recognized by MathObjects. That error goes away if I move the declaration of $f before the disable line. And this did work to block an answer using sqrt. Am I not understanding the documentation correctly?

Now the final answer for the integral in terms of x does require using sqrt, so I initially thought that I could just enable 'sqrt' again since the documentation says "To make the function available again, use enable". But my naive first attempt at doing that by just inserting the line

Context()->functions->enable("sqrt");

did not work because now the answer previously blocked for using sqrt was marked as correct. My fix was to use two pgml blocks, one for the first answer with sqrt disabled, and then the second block for the second answer where sqrt could be used. In between the two blocks I inserted another context("Numeric") statement and the code for the correct answer which uses sqrt. I did not have to include the enable command, I assume because the previous disable command applied only to the context that contained it. Was this the appropriate way to handle the situation of disabling use of sqrt for one answer and allowing it for the other answer? It does work.

Here is the code I came up with.

DOCUMENT();

loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "PGML.pl",
  "PGcourse.pl"
);

TEXT(beginproblem());

Context("Numeric");
Context()->variables->add(t => "Real");
Context()->variables -> set(t =>{limits => [-1,1]});
Context()->{error}{msg}{"Function 'sqrt' is not allowed in this context"} = "Simplify the answer to not use 'sqrt'";

$f = Formula("sqrt(4-x^2)");
$ans3 = Formula("4*cos(t)^2");

Context()->functions->disable("sqrt");

BEGIN_PGML

The goal of this problem is to evaluate [``\int [$f]\;dx``] using trig substitution.

[``\int\;[$f]\;dx = \int``][_]{$ans3}{12}[`\;dt`]

END_PGML

Context("Numeric");
Context()->{error}{msg}{"Variable 't' is not defined in this context"}
  = "Substitute back in terms of x";
$ans4 = Formula("x*sqrt(4-x^2)/2 + 2*arcsin(x/2)");

BEGIN_PGML

Evaluate the trig integral and substitute back in terms of [`x`].

[``\int [$f]\;dx``] = [_]{$ans4}{12}[`+\,C`]

END_PGML

ENDDOCUMENT();


In reply to Larry Riddle

Re: Questions about disabling/enabling functions

by Alex Jordan -

When you execute

Context("Numeric");

you are creating an instance of the "Numeric" context and setting that as the "default" context for any MathObjects you subsequently create. Also as the context that will be modified when you do things like Context()->...;.  But you can create a second instance by executing

Context("Numeric");

again. You can have two instances, one where sqrt is allowed in the parsing of a string to create a MathObject, and one where it is not. You have done this split up with a PGML block in between, but you can do this all before the first PGML block if you want. Your $ans3 knows its context is the first instance of Numeric context, and your $ans4 knows its context is the second instance.

Note that disabling sqrt will not prevent a student from typing it by using a power of 1/2. If you are trying to get an answer in a particular form, you might look into using contextForm.pl, which relies on bizarroArithmetic.pl.

Also maybe you were simplifying things for this post, but another valid substitution a student might use is x=2cos(t), leading to -4sin^2(t) as the integrand, and I think then you want the test point window to straddle pi/2 instead of straddling 0. So you have that complication to consider.


In reply to Alex Jordan

Re: Questions about disabling/enabling functions

by Larry Riddle -
Alex, thanks for the explanation about creating two instances to handle the sqrt issue. That was very helpful. Glad to know I was somewhat on the right track.

I'll take a look into contextForm.pl, which I've never used. I also thought that rather than disabling sqrt, I could try to write an answer checker that could catch when a student leaves the sqrt in the answer for the integrand (and I suppose could also catch using a power of 1/2) and that could then leave a message that such an answer is correct but the student should simplify it to remove the square root. But I don't know how to parse the student's answer to catch those details, and haven't had the time yet to investigate that approach.

At the moment this is really just a work in concept, both to see what can be done with this particular type of problem, and also to learn more about writing problems for WeBWorK. My initial version did allow for either 2sin(t) or 2cos(t) as valid entry points for the trig substitution. I then used a multianswer checker for the possible answers for which trig substitution to use, the derivatives of those trig substitutions, and the resulting new integrands. I didn't think about the need to handle different test point windows. But the text I am basing this problem on only uses the sine function for this type of trig substitution, so I eventually decided to simplify things and just allow only that choice. I then wrote a simpler answer checker for just the initial trig substitution that included a message that 2cos(t) was ok but the student should redo the problem using the sine function. I removed that, however, when simplifying things for the post.
In reply to Larry Riddle

Re: Questions about disabling/enabling functions

by Alex Jordan -
Here is a version of the problem you posted before. Notes:

  • it uses scaffold.pl for the parts of the problem. If you want to, you can set that so that the first part must be answered correctly before the student can reveal the second part.
  • it uses contextForm.pl. I think this may sweep aside the concerns you have about students using sqrt.
  • for the second answer, it uses a FormulaUpToConstant. Done this way, the student must type the "+C". Also, while you are expecting the formula to be "x sqrt(4 - x^2)/2 + 2 arcsin(x/2)", imagine somehow a student ends up with "x sqrt(4 - x^2)/2 + 2 arcsin(x/2) + 17". (17 is a little ridiculous but sometimes students legitimately end up with an expression differing from what was expected by some constant.) In the original version you posted, that would not be counted correct. Here, as long as they type the "+C" too, then "x sqrt(4 - x^2)/2 + 2 arcsin(x/2) + 17 + C" is still counted correct.

DOCUMENT();

loadMacros(qw(
PGstandard.pl
PGML.pl
contextForm.pl
parserFormulaUpToConstant.pl
scaffold.pl
PGcourse.pl
));

Context("Form")->variables->add(t => "Real");
Context()->variables->set(x => {limits => [-2,2]});
Context()->variables->set(t => {limits => [-pi/2,pi/2]});

$f = Formula("sqrt(4-x^2)");
$ans3 = Formula("4 cos(t)^2");
$ans4 = FormulaUpToConstant("x sqrt(4 - x^2)/2 + 2 arcsin(x/2)");

BEGIN_PGML
The goal of this problem is to evaluate [``\int [$f]\;dx``] using trig substitution.
END_PGML

Scaffold::Begin(numbered => 1);
Section::Begin("Use substitution");
BEGIN_PGML
Make an appropriate substitution.

[``\int\;[$f]\;dx = \int``][_]{$ans3}{12}[`\;dt`]
END_PGML
Section::End();

Section::Begin("Evaluate and back-substitute");
BEGIN_PGML
Evaluate the trig integral and substitute back in terms of [`x`].

[``\int [$f]\;dx``] = [_]{$ans4}{12}
END_PGML
Section::End();
Scaffold::End();
ENDDOCUMENT();