The error statement reads:
You say that
In any case, I don't think
For the latter, you would need
parser::Assignment
is loaded, but your loadMacros()
call doesn't include "parserAssignment.pl"
, which is the file that defines it. In any case, I don't think
parser::Assignment
is what you want, since it allows students to enter answers of the form L(t) = 3t^3+5
(a function name equal to a formula), not L(5)-L(3)
(using a predefined function in their answers).For the latter, you would need
parserFunction.pl
where you define the function L(t)
to be some unusual formula that the student's aren't likely to type themselves. Paul gave an example of this. See Workshop3-PM problem 10 and view the source, and listen to the recording of that section of the presentation.
Hello again,
I did find Paul's presentation on getting webwork to use equations for answer but unfortunately I am still having problems getting the correct answer to display an expression and instead it evaluates the dummy formula.
The student's answer is marked correct, but the correct answer shows an evaluated number. Part of my problem is mapping Pauls example that uses a difference of inputs while my example uses a difference of outputs.
Below is the code: Thanks, in advance.
# Webwork Workshop 2015 for Payer, Homework 19, Practice:
# Exercises for Definite Integral applications: pg411, #47.
DOCUMENT();
loadMacros("PGstandard.pl",
"MathObjects.pl",
"parserFunction.pl",
"PGML.pl");
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
Context("Numeric");
parserFunction( "L(x)" => "cos(3x) -2x^2");
$a =Real(random(2, 8,1));
$b =Real(random(10,22,1));
$f = Formula("L($b)-L($a)");
$ans = $f->substitute(x=>"$b", x=>"$a");
BEGIN_PGML
Released on an island without predators a lemming population grows at the rate of [`L'(t)`] at time [`t`] in months. Find an expression that tallies the number of lemmings that have been added to the island population between months of [``t =[$a]``] and [``t = [$b]``].
[______________]{$ans}
END_PGML
Since your result would normally be numeric, in order to have it show the formula rather than the number, you should add
Context()->flags->set( reduceConstantFunctions => 0, reduceConstants => 0, formatStudentAnswer=>'parsed', );right after setting the context to Numeric. That will prevent automatic reduction of constant operations and function calls on constants (you don't technically need to set
reduceConstants
, but reduceConstantFunctions
is the key). The formatStudentAnswer
being set to 'parsed'
means that the "Entered" column will show the unevaluated version of the student answer as well.
Finally, you don't need the line
$ans = $f->substitute(x=>"$b", x=>"$a");since $f has no variable
x
, and so this does nothing. Also, you have given two different values to x
, so only the last one will be used. You also convert both $a
and $b
to a string by putting quotation marks around them, and then they will be re-parsed into a MathObject, which is inefficient. Just use x => $a
directly (in the future -- you don't need the line at all here).
Hi Tim,
A few things:
- Your example needs an ENDDOCUMENT();
- $f = Formula("L($b)-L($a)"); defines $f as a constant function whose value is L($b)-L($a) no matter what the value of x is, which is probably not what you want.
- $f->substitute(x=>"$b", x=>"$a"); attempts to substitute both the values $b and $a in for the (now nonexistent) variable x in the constant formula $f. You do not need to substitute anything into $f to get the correct answer, and you do not need to substitute twice for x. Also, the quotes around $b and $a would not be necessary.
Instead, you should:
- Define the correct answer as Compute("L($b) - L($a)"); so that the correct answer string has the form L(b)-L(a) and is not reduced to some unintelligible number (the actual difference in values of the hidden function L).
- Modify the current context so that student answers are parsed, which means that when students submit their answer, it will appear to them in the "Entered" column of the answer table in the form L(b)-L(a) instead of some unintelligible number.
Best regards,
Paul Pearson
############## begin pg code #############
DOCUMENT();
loadMacros("PGstandard.pl",
"MathObjects.pl",
"parserFunction.pl",
"PGML.pl",
"PGcourse.pl",
);
TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
Context("Numeric");
Context()->flags->set(formatStudentAnswer=>"parsed");
parserFunction( "L(x)" => "cos(3x) -2x^2");
$a = random(2, 8,1);
$b = random(10,22,1);
$ans = Compute("L($b) - L($a)");
BEGIN_PGML
Released on an island without predators a lemming population grows at the rate of [`L'(t)`] at time [`t`] in months. Find an expression that tallies the number of lemmings that have been added to the island population between months of [`t =[$a]`] and [`t = [$b]`].
[______________]{$ans}
END_PGML
ENDDOCUMENT();
Thanks, Paul! I didn't catch the
Formula()
versus Compute()
problem. With Compute()
, you don't need to set the reduceConstants
or reduceConstantFunctions
like I did, so your solution is better for that reason as well as the fact that it gets the proper error messages for incorrect answers.