Difference between revisions of "AdaptiveParameters"

From WeBWorK_wiki
Jump to navigation Jump to search
m
Line 19: Line 19:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
$aSoln = Compute("e^x - 1");
+
$aSoln = Compute("e^x - 1");
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 31: Line 31:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
BEGIN_TEXT
+
BEGIN_TEXT
Find one solution to the differential equation
+
Find one solution to the differential equation
\[ \frac{dy}{dx} = y + 1. \]
+
\[ \frac{dy}{dx} = y + 1. \]
\( y = \) \{ ans_rule(35) \}
+
\( y = \) \{ ans_rule(35) \}
END_TEXT
+
END_TEXT
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
Line 46: Line 46:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( $aSoln->cmp( checker => sub {
+
ANS( $aSoln->cmp( checker => sub {
my ( $correct, $student, $self ) = @_;
+
my ( $correct, $student, $self ) = @_;
my $context = Context()->copy;
+
my $context = Context()->copy;
$context->flags->set(no_parameters=>0);
+
$context->flags->set(no_parameters=>0);
$context->variables->add('C0'=>'Parameter');
+
$context->variables->add('C0'=>'Parameter');
my $c0 = Formula($context,'C0');
+
my $c0 = Formula($context,'C0');
$student = Formula($context,$student);
+
$student = Formula($context,$student);
$correct = Formula($context,"$c0 e^x - 1");
+
$correct = Formula($context,"$c0 e^x - 1");
return $correct == $student;
+
return $correct == $student;
}));
+
}));
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">

Revision as of 13:45, 24 December 2010

Adaptive Parameters: PG Code Snippet

This code snippet shows the essential PG code to include adaptive parameters in a problem solution. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working. This page is largely drawn from this discussion thread, which looks at one application.

Note that if we want to have a formula that is only unique up to a specified additive constant, we have the upToConstant option in the answer checker, as discussed in the formulas up to constants page.

Problem Techniques Index

PG problem file Explanation
$aSoln = Compute("e^x - 1");

In this case we work with the adaptive parameters only in the custom answer checker that we use for the problem, so no special set-up needs to be done in the problem set-up section of the PG file. Here we've defined $aSoln so that we have a MathObjects function to work with later.

BEGIN_TEXT
Find one solution to the differential equation
\[ \frac{dy}{dx} = y + 1. \]
\( y = \) \{ ans_rule(35) \}
END_TEXT

And the text section of the file is similarly "normal." The general solution to this differential equation is y = c ex - 1, but a student could also write it as something like y = ec ex - 1. We use the adaptive parameter to find the value of the constant.

ANS( $aSoln->cmp( checker => sub {
    my ( $correct, $student, $self ) = @_;
    my $context = Context()->copy;
    $context->flags->set(no_parameters=>0);
    $context->variables->add('C0'=>'Parameter');
    my $c0 = Formula($context,'C0');
    $student = Formula($context,$student);
    $correct = Formula($context,"$c0 e^x - 1");
    return $correct == $student;
}));

Then in the answer, we define a custom answer checker that creates a copy of the Context in which an adaptive parameter is allowed, redefines the student and correct answers in that context, and checks that the answers match with the adaptive parameter.

Again, note that if we just want to allow a solution to differ by an additive constant from the answer in the problem, we can use the existing MathObjects infrastructure, as discussed on the formulas up to constants techniques page.

Problem Techniques Index