If you add variables (e.g., u, t, and dx) to the Context, is it then possible to restrict the use of one or more of those variables in an answer field?
For example: in the problem below (source code included) , the fifth answer fields asks for a function of u, but it is possible for a student to enter an answer that has other allowable variables in the answer (as long as they cancel out).
If the correct answer is: 1/(4u^(1/6))
then also accepted as correct is: (t+1)/(4(t+1)u^(1/6)), so how can one restrict that answer so that only the variable u is allowed?
======================================================================
DOCUMENT(); # This should be the first executable line in the problem.
loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGanswermacros.pl",
"PGauxiliaryFunctions.pl",
"MathObjects.pl",
"PGML.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
Context("Numeric");
Context()->variables->add(t=>"Real",dt=>"Real",u=>"Real");
$a = random(2,9,1);
$b = Compute("2 $a");
$c = random(11,21,2);
$u = Compute("$a t^{2}+$b t + $c");
$n = random(3,6,1);
$uprime = $u->D('t');
$du = Compute("$uprime dt");
$u0 = $u->eval(t=>0);
$u1 = $u->eval(t=>1);
$f = Compute("1/(2*$a*u^(1/$n))");
$intf = Compute("(u^(1 - 1/$n))/(2*$a*(1 - 1/$n))");
$Fb = $intf ->eval(u=>$u1);
$Fa = $intf->eval(u=>$u0);
$ans = Compute("$Fb - $Fa")->reduce;
Context()->texStrings;
BEGIN_PGML
Using the u-Substitution Method:
[` \displaystyle\int_{0}^{1} \dfrac{t+1}{\sqrt[[$n]]{[$u]}} \;dt = \displaystyle\int_{a}^{b} f(u)\;du `]
where
[` u =`][____________________________]{$u} (enter a function of [`t`])
[` du = `][____________________________]{$du} (enter a function of [`t`] and [`dt`] )
[`a =`][_________________________]{$u0} (enter a number)
[`b =`][_________________________]{$u1} (enter a number)
[`f(u) =`][_________________________]{$f} (enter a function of [`u`]).
The value of the original integral is
[_________________________]{$ans}.
END_PGML
BEGIN_PGML_HINT
* If [`u=g(t)`], then the differential is [`du=g'(t)dt`].
* To find the limits of integration, [`a`] and [`b`], evaluate [`u`] at [`t=0`] and at [`t=1`].
END_PGML_HINT
BEGIN_PGML_SOLUTION
[` u = [$u]`]
[` du = [$du] `]
[`a = [$u0]`]
[` b = [$u1]`]
[`f(u) = 1/([$b]u^{1/[$n]}) `]
The value of the original integral is
[` \frac{1}{[$b]} \int_[$u0]^[$u1] u^{-1/[$n]} \,du = \frac{1}{[$b]} \left. \frac{u^{-\frac{1}{[$n]}+1}}{-\frac{1}{[$n]} + 1}\right|_[$u0]^[$u1] = [$ans] `]
[% = \frac{1}{[$b]} \frac{u^{-1/[$n]+1}}{-1/[$n] + 1} |_[$u0]^[$u1] = [$ans] `]%]
END_PGML_SOLUTION
Context()->normalStrings;
COMMENT('MathObjects version.');
ENDDOCUMENT();
In reply to Michele Titcombe
Re: Restricting variables allowed in certain answer fields
by Paul Pearson -Hi Michele,
The short answer is that you need to reset the context with only the variables you want to allow. An example of this is given in pg and pgml here:
- http://webwork.maa.org/wiki/DomainRange1
- https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/Precalc/DomainRange1_PGML.pg
Some more tips:
- You do not need Context()->texStrings; and Context()->normalStrings; in PGML.
- Using \dfrac might cause problems; it's better to use \displaystyle\frac or in PGML just use [`` \frac{a}{b} ``] noting the doubled backticks that denote displaystyle mode.
- Just load PGstandard.pl.
Example pg code is below my signature.
Best regards,
Paul Pearson
#####################################
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGcourse.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
# define some perl scalars
$a = random(2,9,1);
$b = 2 * $a;
$c = random(11,21,2);
$n = random(3,6,1);
Context("Numeric");
Context()->variables->are(
t=>"Real",
dt=>"Real",
);
$u = Compute("$a t^2 + $b t + $c");
$uprime = $u->D('t');
$du = Compute("$uprime dt");
$u0 = $u->eval(t=>0);
$u1 = $u->eval(t=>1);
BEGIN_PGML
Use the method of u-substitution to evaluate the following definite integral:
[`` \int_{0}^{1} \frac{t+1}{\sqrt[[$n]]{[$u]}} \; dt = \int_{a}^{b} f(u) \; du ``]
where
[` u =`][____________________________]{$u} (enter a function of [`t`])
[` du = `][____________________________]{$du} (enter a function of [`t`] and [`dt`] )
[`a =`][_________________________]{$u0} (enter a number)
[`b =`][_________________________]{$u1} (enter a number)
END_PGML
# completely reset the context with only the variable u:
# use Context("Numeric")->...
# not Context()->...
Context("Numeric")->variables->are('u'=>'Real');
$f = Compute("1/(2*$a*u^(1/$n))");
$intf = Compute("(u^(1 - 1/$n))/(2*$a*(1 - 1/$n))");
$Fb = $intf ->eval(u=>$u1);
$Fa = $intf->eval(u=>$u0);
$ans = Compute("$Fb - $Fa")->reduce;
BEGIN_PGML
[$PAR]*
[`f(u) =`][_________________________]{$f} (enter a function of [`u`]).
The value of the original integral is
[_________________________]{$ans}.
END_PGML
BEGIN_PGML_HINT
* If [`u=g(t)`], then the differential is [`du=g'(t)dt`].
* To find the limits of integration, [`a`] and [`b`], evaluate [`u`] at [`t=0`] and at [`t=1`].
END_PGML_HINT
BEGIN_PGML_SOLUTION
[` u = [$u]`]
[` du = [$du] `]
[`a = [$u0]`]
[` b = [$u1]`]
[`f(u) = 1/([$b]u^{1/[$n]}) `]
The value of the original integral is
>> [`` \frac{1}{[$b]} \int_[$u0]^[$u1] u^{-1/[$n]} \,du = \frac{1}{[$b]} \left. \frac{u^{-\frac{1}{[$n]}+1}}{-\frac{1}{[$n]} + 1}\right|_[$u0]^[$u1] \approx [$ans] ``] <<
END_PGML_SOLUTION
ENDDOCUMENT();
In reply to Paul Pearson
Re: Restricting variables allowed in certain answer fields
by Michele Titcombe -
Aha! Thanks, Paul, for the informative answer.
Michele Titcombe
Michele Titcombe