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, 5 months ago) by www
Branches: MAIN
CVS tags: version_2_12_X, version_2_11_X, 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

    1: <problem>
    2: <script type="loncapa/perl">
    3: $a=&random(3,8,1);
    4: $b=$a+&random(2,5,1);
    5: $sample="x=$b or y=$a*x would be correct";
    6: 
    7: sub compareequations {
    8:     my ($var,$value,$equation,$real,$lenient)=@_;
    9: # var is the variable given
   10: # value is the expected value of $var, $var=$value
   11: # equation is the equation to be tested
   12: # real: if set, operate only in the real realm
   13: # 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
   14:     $correctanswer=&cas('maxima','trigsimp(trigreduce('.$var.'='.$value.'))');
   15:     if ($correctanswer=~/^Error\:/) { return $correctanswer; }
   16:     $solution=&cas('maxima','trigsimp(trigreduce(solve('.$equation.','.$var.')))');
   17:     if ($solution=~/^Error\:/) { return $solution; }
   18:     $solution=~s/^\[//;
   19:     $solution=~s/\]$//;
   20:     $total=0;
   21:     $found=0;
   22:     foreach my $thissolution (split(/\s*\,\s*/,$solution)) {
   23:          if (($real) && ($thissolution=~/\%i/)) { next; }
   24:          $total++;
   25:          if ($thissolution eq $correctanswer) { $found=1; }
   26:     }
   27:     if ((!$lenient) && ($total>1)) { return 'false'; }
   28:     if ($found) { return 'true'; }
   29:     return 'false';
   30: }
   31: </script>
   32: 
   33: <startouttext />
   34: Given an equation for an asymptote of <m eval="on">\[y=$a\cdot x+\frac{1}{x-$b}\]</m>
   35: <endouttext />
   36: 
   37: <customresponse answerdisplay="$sample">
   38: 
   39: <answer type="loncapa/perl">$first=&compareequations('y',"$a*x",$submission,1);
   40: if ($first=~/^Error/) { return 'BAD_FORMULA'; }
   41: if ($first=~/true/) { return 'EXACT_ANS'; }
   42: 
   43: $second=&compareequations('x',$b,$submission,1);
   44: if ($second=~/^Error/) { return 'BAD_FORMULA'; }
   45: if ($second=~/true/) { return 'EXACT_ANS'; }
   46: 
   47: return 'INCORRECT';</answer>
   48: 
   49:     <textline readonly="no" />
   50: </customresponse>
   51: 
   52: </problem>

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