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();