[LON-CAPA-users] Extracting the numerical value from asolve()Maxima command

Seema Ali sali at vsb.bc.ca
Sun Jan 29 23:31:36 EST 2012


Hi Gerd,

Yes, it works.  It looks similar to the regular expression used to get the expression on either side of an equal sign.

Thank you!

>From Seema.


-----Original Message-----
From: lon-capa-users-bounces at mail.lon-capa.org on behalf of Gerd Kortemeyer
Sent: Sun 1/29/2012 5:50 PM
To: Discussion list for LON-CAPA users
Subject: Re: [LON-CAPA-users] Extracting the numerical value from asolve()Maxima command
 
Hi,

I think, as you suspected, it was just the regular expression. As the saying goes: "I have a problem. Hmmm, I know, I'll just use a regular expression! Now I have two problems ."

Please see if this works:

<problem>
<script type="loncapa/perl">
$base1 = &random(2,9,1);
do {
   $base2 = &random(2,9,1);
} while ($base1==$base2);

$a = &random(2,3,1);
$b = &random(-7,-4,1);
$c = &random(1,10,1);


if (&random(0,1,1)) {
  $power1 = "$a x";
  $power2 = "$b x - $c";

  $power1maxima = "$a * x";
  $power2maxima = "$b * x - $c";
} else {
  $power1 = "$a x + $c";
  $power2 = "$b x";

  $power1maxima = "$a * x + $c";
  $power2maxima = "$b * x";
}


$solution = &cas('maxima',"float(solve(($power1maxima)*log($base1) = ($power2maxima)*log($base2),x))");
$solution=~/\=\s*(.+)\s*\]/;
$answer=$1;
</script>
<startouttext />Solve for <m>$x$</m> to the nearest hundredth where <m eval='on'>\[$base1^{$power1} = $base2^{$power2}\]</m>

$solution<br />
answer=$answer<endouttext />
<numericalresponse answer="$answer" format="2F">
<responseparam name="tol" type="tolerance" description="Numerical Tolerance" default="0.01" />

    <textline readonly="no" />
   
</numericalresponse>
</problem>




On Jan 28, 2012, at 11:32 AM, Seema Ali wrote:

> It is a little messy, I'm still in construction/learning phase. =]
> 
> <problem>
> <script type="loncapa/perl">$base1 = &random(2,9,1);
> 
> do{
>    $base2 = &random(2,9,1);
> }while($base1==$base2);
> 
> $a = &random(2,3,1);
> $b = &random(4,7,1) * -1;
> $c = &random(1,10,1);
> 
> $index = &random(1,2,1);
> $leftright = &choose($index,'left','right');
> 
> if($leftright=='right')
> {
>   $power1 = "$a x";
>   $power2 = "$b x - $c";
> 
>   $power1maxima = "$a * x";
>   $power2maxima = "$b * x - $c";
> }
> else
> {
>   $power1 = "$a x + $c";
>   $power2 = "$b x";
> 
>   $power1maxima = "$a * x + $c";
>   $power2maxima = "$b * x";
> }
> 
> $equation = "($power1maxima)*log($base1) = ($power2maxima)*log($base2)";
> 
> $solution = &cas('maxima',"float(solve(($power1maxima)*log($base1) = ($power2maxima)*log($base2),x))");
> #$solution =~ /(-?\d+(?:\.\d+)?)/;
> $solution =~ /(-?\d+?(?:\.\d+)?)/;
> $answer = $1;</script><startouttext />Solve for <m>$x$</m> to the nearest hundredth where <m eval='on'>$base1^{$power1} = $base2^{$power2}</m>
> 
> <m eval='on'>$ $equation $solution answer=$answer .............  ------ $</m><endouttext />
> <numericalresponse answer="$answer">
> <responseparam name="tol" type="tolerance" description="Numerical Tolerance" default="0.0001" />
> 
>     <textline readonly="no" />
>    
> </numericalresponse>
> </problem>
> 
> 
> 
> -----Original Message-----
> From: lon-capa-users-bounces at mail.lon-capa.org on behalf of Gerd Kortemeyer
> Sent: Fri 1/27/2012 4:41 PM
> To: Discussion list for LON-CAPA users
> Subject: Re: [LON-CAPA-users] Extracting the numerical value from a solve()Maxima command
> 
> Hi,
> 
> Could you please send me the XML source code for your problem?
> 
> Thanks,
> 
> - Gerd.
> 
> On Jan 27, 2012, at 6:14 PM, Seema Ali wrote:
> 
> > Hi,
> >
> > This was my attempt to get Maxima to solve the following equation:
> > $base1^$power1maxima = $base2^$power2maxima
> > where $power1maxima and $power2maxima are linear equations and
> > $base1 and $base2 are constant values.
> >
> > Below is a snippet of part of my code:
> >
> > $solution = &cas('maxima',"float(solve(($power1maxima)*log($base1) = ($power2maxima)*log($base2),x))");
> > $solution =~ /(-?\d+(?:\.\d+)?)/;
> > $answer = $1;
> >
> >
> > Here are a couple of examples of some values:
> >
> > Example 1:
> > An equation it is trying to solve: (2 * x)*log(7) = (-7 * x - 7)*log(9)
> > $solution is [x = -.7980624185332027]
> > answer=7
> >
> > Example 2:
> > An equation it is trying to solve: (2x)log(4)=(?5x?8)log(3)
> > $solution is [x=?1063303930517583]
> > answer=?1063303930517583
> >
> > The answer for Example 2 should be -1.063303930517583.  If I double click on the values on the page I get the decimal point, otherwise if I highlight them which is what I did for Example 2, the decimal is missing, which is a separate issue.
> >
> > Example 2 also generates this error:
> > ERROR: Computer's answer is incorrect ("-1.063303930517583"). It is likely that the tolerance range [-1.06340393051758, -1.06320393051758] needs to be adjusted.
> > This error occurred while processing response 1_7 in part 0
> >
> > My tolerance range is 5%.
> >
> > I think it is my attempt at a regular expression is probably causing this problem.
> >
> > How do I extract the numerical value from a solve command in Maxima?
> >
> >
> > Thanks.
> >
> > _______________________________________________
> > LON-CAPA-users mailing list
> > LON-CAPA-users at mail.lon-capa.org
> > http://mail.lon-capa.org/mailman/listinfo/lon-capa-users
> 
> _______________________________________________
> LON-CAPA-users mailing list
> LON-CAPA-users at mail.lon-capa.org
> http://mail.lon-capa.org/mailman/listinfo/lon-capa-users
> 
> 
> _______________________________________________
> LON-CAPA-users mailing list
> LON-CAPA-users at mail.lon-capa.org
> http://mail.lon-capa.org/mailman/listinfo/lon-capa-users

_______________________________________________
LON-CAPA-users mailing list
LON-CAPA-users at mail.lon-capa.org
http://mail.lon-capa.org/mailman/listinfo/lon-capa-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.lon-capa.org/pipermail/lon-capa-users/attachments/20120129/9851e2cb/attachment.html>


More information about the LON-CAPA-users mailing list