WeBWorK Problems

Re: Student defined variables

by Davide Cervone -
Number of replies: 0
If you don't need to actually display the student's value later in the problem, you might be able to use the MultiAnswer object to tie several answers together.

For example, here is a problem that asks the students to come up with a value for a that makes x3 - ax not be one-to-one, and then asks for two values of x where the values are the same.

loadMacros("parserMultiAnswer.pl", "PGML.pl");

Context("Numeric");

$ma = MultiAnswer(-1, 0, 1)->with( checker => sub { my ($c, $s,$ans) = @_;
my ($a,$x1, $x2) = @$s;
my $aOK = ($a->value < 0);
my $f = Compute("x^3 + ($a) x");
my $xOK =$x1 != $x2 &&$f->eval(x => $x1) ==$f->eval(x => $x2); return [$aOK, $xOK,$xOK];
}
);

BEGIN_PGML
Consider the function [f(x) = x^3 + a x].

Find a value of [a] so that [f] is *not* one-to-one:

[a] = [________]{$ma} Show that, for your value of [a], [f] is not one-to-one by finding two values, [x_1] and [x_2] where [f(x_1) = f(x_2)]: [x_1] = [________]{$ma}
[x_2] = [________]{$ma} END_PGML  Of course, this one doesn't give very good error messages, and it will mark all three answers incorrect if any one of them is blank, so you might want to get more sophisticated about it, as in the following: loadMacros("parserMultiAnswer.pl", "PGML.pl"); Context("Numeric");$ma = MultiAnswer(-1, 0, 1)->with(
checker => sub {
my ($c,$s, $ans) = @_; my ($a, $x1,$x2) = @$s; Value->Error("Your answer can't be checked without a value here") if$a eq '';
my $aOK = ($a->value < 0);
$ma->setMessage(2, "Both x1 and x2 must be entered in order to check your answer") if$x1 eq '' && $x2 ne '';$ma->setMessage(3, "Both x1 and x2 must be entered in order to check your answer") if $x2 eq '' &&$x1 ne '';
return [$aOK, 0, 0] if ($x1 eq '' || $x2 eq '');$ma->setMessage(3, "Your answers for x1 and x2 must be different") if $x1 ==$x2;
my $f = Compute("x^3 + ($a) x");
my $xOK =$x1 != $x2 &&$f->eval(x => $x1) ==$f->eval(x => $x2); return [$aOK, $xOK,$xOK];
}
);

BEGIN_PGML
Consider the function [f(x) = x^3 + a x].

Find a value of [a] so that [f] is *not* one-to-one:

[a] = [________]{$ma} Show that, for your value of [a], [f] is not one-to-one by finding two values, [x_1] and [x_2] where [f(x_1) = f(x_2)]: [x_1] = [________]{$ma}
[x_2] = [________]{$ma} END_PGML  Perhaps this approach is good enough for your situation? Otherwise, Mike's suggestion of using $inputs_ref is the way to go. You might find the scaffolding macros to be useful for breaking the problem into sections.