# [LON-CAPA-users] Having an equation or a range as a response

Gerd Kortemeyer korte at lite.msu.edu
Sun Jan 22 20:03:37 EST 2012

Hi,

On Jan 22, 2012, at 8:38 AM, Gerd Kortemeyer wrote:
>
>> Oh and I also tested:
>>
>> x^3 = 125 and it failed on both which is good if you're looking for linear solutions.
>>
>
> That's apparently because MAXIMA wants to solve this in the complex plane:
>
> (%i3) solve(x^3=125,x);
>                  5 sqrt(3) %i - 5        5 sqrt(3) %i + 5
> (%o3)        [x = ----------------, x = - ----------------, x = 5]
>                         2                       2
>
> I guess I need to work in some way to tell MAXIMA if it should stay real.

MAXIMA does not respect declarations with doing "solve":

(%i4) declare(x,real);
(%o4)                                done
(%i5) solve(x^3=125,x);
5 sqrt(3) %i - 5        5 sqrt(3) %i + 5
(%o5)        [x = ----------------, x = - ----------------, x = 5]
2                       2
(%i6) facts();
(%o6)                           [kind(x, real)]

:-(

Thus, we need to do this in Perl:

<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'; }