## WeBWorK Problems

### Question with two possible answers ### Question with two possible answers

by Daniele Arcara -
Number of replies: 5
How can I allow several answers each to be correct for one problem?

Say that I want to ask the students to integrate 2*sin(x)*cos(x), and I want them to be allowed to write any of the following answers (forgetting the +C for now): (sin(x))^2 , -(cos(x))^2 , -1/2 cos(2x).

Is there a way to do that? For now, here is my simple code with answer (sin(x))^2:

DOCUMENT();

"PGstandard.pl",
"PGML.pl",
"PGcourse.pl"
);

TEXT(beginproblem());

$f = Formula("2*sin(x)*cos(x)");$F = Formula("(sin(x))**2");

$ans =$F;

BEGIN_PGML

[ \int [$f] \, dx = ] [_______________________]{$ans} [ +C ]

END_PGML

ENDDOCUMENT;

$G = -Formula("(cos(x))**2"); and writing the answer with an 'or' in it as$ans = ($F||$G);
but it did not work.

Any help would be appreciated. Thank you! ### Re: Question with two possible answers

by Paul Pearson -
Hi Daniele,

I'm going to start by being a little pedantic: there is only one function F(x) with domain equal to all real numbers that is an antiderivative of f(x) = 2cos(x)sin(x) such that F(0) = -1/2, and thus any antidervative of f(x) is of the form F(x) + C.  This function F(x) is equivalent to many different looking but equivalent) expressions involving sines and cosines, such as F(x) = sin^2(x)-1/2 = -cos^2(x) - 1/2= -1/2 cos(2x) = sin^2(x)/2 - cos^2(x)/2 = ...

So, if you were going to try to write your own answer checker for this problem, you would want to check that the difference between a correct answer and the student answer is a constant for all x.  Also, you would want to require students to enter +C to indicate that they know that the indefinite integral yields a family of functions.  Fortunately, all of this has been done for you already by Davide Cervone, who wrote the parserFormulaUpToConstant.pl macro.  (Technically, the student answer is checked against the correct answer for a small number of randomly selected x-values, not all x-values, but that's good enough.)

See the code below.

Best regards,

Paul Pearson

####################################

DOCUMENT();

"PGstandard.pl",
"PGML.pl",
"parserFormulaUpToConstant.pl",
"PGcourse.pl"
);

TEXT(beginproblem());

$f = Formula("2*sin(x)*cos(x)");$F = FormulaUpToConstant("(sin(x))^2");

BEGIN_PGML

[ \int [$f] \, dx = ] [_______________________]{$F}

END_PGML

ENDDOCUMENT; ### Re: Question with two possible answers

by Alex Jordan -
Hi Daniele,

I wouldn't view this as allowing for multiple answers, in the sense of trying to enumerate lot of common antiderivatives the student might com up with. Off the top of my head, I see two options for you.
• Use http://webwork.maa.org/wiki/FormulasToConstants#.VnNELvmDFBc. The example here should explain how to use it.
• Write a custom answer checker (http://webwork.maa.org/wiki/Custom_Answer_Checkers#.VnNE_vmDFBc) that takes the student's answer and firstly, checks that it is a Formula [and if it is a Real turns it into a Formula] and then compares the derivatives of the student answer to the derivative of the correct answer. With a MathObject Formula $f where x is the variable,$f->D('x') will give the derivative. I can only think one reason why I personally might ever go this way, and that would be if the antiderivative has any holes in its domain (so the +C in theory could be a more complicated step function with steps at the holes.)
Either way, watch out for domain issues with antiderivatives. Especially for distinguishing between ln(x) and ln(|x|) and the like. ### Re: Question with two possible answers

by Davide Cervone -
Aside from the two fine answers above, there is a third approach: the Formula object's answer checker has an upToConstant option that checks if the student enters something that differs from the correct answer by a constant (without the need for the student to type the +C part (it is a precursor to the FormulaUpToConstant object that the others have mentioned). You could use
BEGIN_PGML

[ \int [$f] \, dx = ] [_______________________]{$ans->cmp(upToConstant=>1)} [ +C ]

END_PGML

to get it. ### Re: Question with two possible answers

by Daniele Arcara -
Thank you for the help, everyone! 