Forum archive 2000-2006

Thomas Hagedorn - Numerical Bug with WW Problem Using Parser

Thomas Hagedorn - Numerical Bug with WW Problem Using Parser

by Arnold Pizer -
Number of replies: 0
inactiveTopicNumerical Bug with WW Problem Using Parser topic started 9/16/2005; 8:18:54 PM
last post 9/17/2005; 3:21:03 PM
userThomas Hagedorn - Numerical Bug with WW Problem Using Parser  blueArrow
9/16/2005; 8:18:54 PM (reads: 449, responses: 5)
We've been discovering a few numerical bugs with the code we've written for a problem using the Parser. If you enter seed 1234 and enter the point (-10.6, -2.9), then Webwork marks the answer as partially incorrect. If you enter points with integer coefficients such as (2,-5) or (-10,-3), the answer is marked as correct. Some decimal answers do work, but many don't. I don't see any problem with the code (which is attached below). By the way, the problem below is a simplification that still shows the error.

Thanks in advance for your suggestions, Tom

 

## DESCRIPTION ## KEYWORDS('vectors', 'independent', 'dependent', 'relation') ## ## ENDDESCRIPTION

DOCUMENT(); # This should be the first executable line in the problem.

loadMacros( PG.pl, PGbasicmacros.pl, PGchoicemacros.pl, PGanswermacros.pl, PGgraphmacros.pl, PGmatrixmacros.pl, PGnumericalmacros.pl, PGauxiliaryFunctions.pl, PGmorematrixmacros.pl, PGdiffeqmacros.pl, "Parser.pl", "Generic.pl" );

TEXT(beginproblem());

$showPartialCorrectAnswers = 1;

$p1 = non_zero_random(-6,6,1); $p2 = random(-5,5,2); $q1 = non_zero_random(-6,6,1); $q2 = random(-6,6,2);

$ans1 = $q1-$p1; $ans2 = $q2-$p2; $vecp = Matrix->new_column_matrix([$p1, $p2]); $slope = Matrix->new_column_matrix([$ans1, $ans2]);

$v1 = new Matrix(2,1); $v1->assign(1,1, $ans1); $v1->assign(2,1, $ans2);

BEGIN_TEXT

Enter a point on the line { mbox( '( v = )', display_matrix($vecp), '( + t) ', display_matrix($slope), '.' ) } $BR { mbox( '(x=)', answer_matrix(2,1,5) ) }

END_TEXT

$spot=0; ANS(generic_cmp("1", type => 'Number', length=>1, checker=> ~~&check)); ANS(generic_cmp("1", type => 'Number', length=>1, checker=> ~~&check));

sub check{

my $stu=shift();

$w[$spot]=$stu->extract(1);

if($spot<1){

$spot=$spot+1; 1;

}else{

$ww[0]=$w[0]-$p1;

$ww[1]=$w[1]-$p2;

$avec1=$v1->element(1,1);

$avec2=$v1->element(2,1);

if($avec1 * $ww[1] == $ww[0] * $avec2){

1;

}else{

0;

} } }

ENDDOCUMENT() ;

<| Post or View Comments |>


userDavide P. Cervone - Re: Numerical Bug with WW Problem Using Parser  blueArrow
9/16/2005; 10:54:00 PM (reads: 504, responses: 0)
The main problem is that you are doing an exact equality check between real numbers, which almost never will be true. The statement
     if ($avec1 * $ww[1] == $ww[0] * $avec2) ...
is the problem, since this only is true when the two real numbers on each side are exactly the same. You need to do a fuzzy check, not an exact check. That is, you need to check if abs($avec1 * $ww[1] - $ww[0] * $avec2) is sufficiently small.

Since you have loaded Parser.pl, you could use the the fuzzy checks that are built into the Real number objects:

    if (Real($avec1 * $ww[1]) == Real($ww[0] * $avec2)) ...
but this may be too forgiving (the default tolerances are pretty loose).

Another problem is that you are always marking the x-coordinate as correct, so the student will always get partial credit for the problem. This is a bit awkward. It also could fail in the case where you have a vertical line (which is possible given your range of random numbers), since the student could enter an x-value that is off the line, but it will still be marked correct.

On a more general note, you are working a lot harder than you need to in this problem. Here is an alternative approach that uses the Parser to better effect.

 


 

    DOCUMENT();        # This should be the first executable line in the problem.


loadMacros(
"PG.pl",
"PGbasicmacros.pl",
"PGanswermacros.pl",
"Parser.pl",
"parserVectorUtils.pl",
);


TEXT(beginproblem());


Context("Vector")->variables->are(t=>'Real');
$t = Formula("t");


$p = non_zero_vector2D(-6,6,1)->with(ColumnVector=>1);
$v = non_zero_vector2D(-5,5,1)->with(ColumnVector=>1);


$L = $p + $t*$v;


Context()->texStrings;
BEGIN_TEXT


Enter a point on the line \($L\).
$PAR
\(x=\)\{$p->ans_array\}


END_TEXT
Context()->normalStrings;


ANS($p->cmp(showCoordinateHints=>0, checker=> sub {
my $correct = shift; my $student = shift;
return $student == $p || $v->isParallel($student-$p);
}));


$showPartialCorrectAnswers = 0;


ENDDOCUMENT();

Here, we use the parser's Vector objects to produce some column vectors for the point and vector for the line, and then combine them into the formula for a line. Because of the Context()->texStrings, the $L in the text area will produce its TeX form automatically. Then the x value is requested using an answer array. Finally, the answer checker automatically collects the entries for the array into a vector, and the checker subroutine checks if the student's point is the same as the one used to generate the line, or if the vector from the starting point to the student's answer is to the line's vector.

Hope this helps.

Davide

<| Post or View Comments |>


userDavide P. Cervone - Re: Numerical Bug with WW Problem Using Parser  blueArrow
9/17/2005; 7:00:59 AM (reads: 527, responses: 1)
PS, there are also several bugs in Generic.pl (at least the one that was posted here during the summer). For example, try entering something that has a syntax error into one of the answer blanks in your original problem.

Davide

<| Post or View Comments |>


userBob Byerly - Re: Numerical Bug with WW Problem Using Parser  blueArrow
9/17/2005; 9:07:43 AM (reads: 579, responses: 0)
As Davide points out, Generic.pl has some problems. It was a work in progress when I posted it as a proof of concept. Davide's adding problem-writer-defined answer checkers to the parser made it obsolete, and Davide's solution, being more tightly integrated with the parser, has much better error checking.

I'm not developing it any further, and have already converted all the problems I wrote with it. Conversion is usually pretty routine. Thanks for trying it out!

Bob

<| Post or View Comments |>


userDavide P. Cervone - Re: Numerical Bug with WW Problem Using Parser  blueArrow
9/17/2005; 9:34:28 AM (reads: 507, responses: 0)
I have edited the code in the example above to compare $student == $p rather than my original $student-$p == Vector(0,0). This is because the latter uses the zero-level tolerances, while the former will use the usual tolerances (the same ones that are in effect for all the other points). That makes the checking more consistent.

Davide

<| Post or View Comments |>


userThomas Hagedorn - Re: Numerical Bug with WW Problem Using Parser  blueArrow
9/17/2005; 3:21:03 PM (reads: 495, responses: 0)
Thanks for the suggestions. The example of the new code really helps out. I'll try to convert our Generic.pl problems to ones using just the Parser. I bet I'll have some more questions then.

-Tom

<| Post or View Comments |>