File:  [LON-CAPA] / loncom / homework / templates / custom_equation.problem
Revision 1.1: download - view: text, annotated - select for diffs
Wed Jan 25 00:59:02 2012 UTC (12 years, 11 months ago) by www
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, version_2_11_5_msu, version_2_11_5, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, HEAD
Bug #6550: testing equations

<problem>
<script type="loncapa/perl">
$a=&random(3,8,1);
$b=$a+&random(2,5,1);
$sample="x=$b or y=$a*x would be correct";

sub compareequations {
    my ($var,$value,$equation,$real,$lenient)=@_;
# var is the variable given
# value is the expected value of $var, $var=$value
# equation is the equation to be tested
# real: if set, operate only in the real realm
# lenient: if set, give credit even if the correct answer is only one of the possible answers, e.g., x^2=9 is "correct" if x=3 is expected
    $correctanswer=&cas('maxima','trigsimp(trigreduce('.$var.'='.$value.'))');
    if ($correctanswer=~/^Error\:/) { return $correctanswer; }
    $solution=&cas('maxima','trigsimp(trigreduce(solve('.$equation.','.$var.')))');
    if ($solution=~/^Error\:/) { return $solution; }
    $solution=~s/^\[//;
    $solution=~s/\]$//;
    $total=0;
    $found=0;
    foreach my $thissolution (split(/\s*\,\s*/,$solution)) {
         if (($real) && ($thissolution=~/\%i/)) { next; }
         $total++;
         if ($thissolution eq $correctanswer) { $found=1; }
    }
    if ((!$lenient) && ($total>1)) { return 'false'; }
    if ($found) { return 'true'; }
    return 'false';
}
</script>

<startouttext />
Given an equation for an asymptote of <m eval="on">\[y=$a\cdot x+\frac{1}{x-$b}\]</m>
<endouttext />

<customresponse answerdisplay="$sample">

<answer type="loncapa/perl">$first=&compareequations('y',"$a*x",$submission,1);
if ($first=~/^Error/) { return 'BAD_FORMULA'; }
if ($first=~/true/) { return 'EXACT_ANS'; }

$second=&compareequations('x',$b,$submission,1);
if ($second=~/^Error/) { return 'BAD_FORMULA'; }
if ($second=~/true/) { return 'EXACT_ANS'; }

return 'INCORRECT';</answer>

    <textline readonly="no" />
</customresponse>

</problem>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>