Maxima : Getting a value from 'Solve'



"Lut Mentz" <lut.mentz at zen.co.uk> writes:

> Hi,
>
> I want to extract from the output of Solve() an executable string to
> make an assignment. For instance
>
> (%i1) solve([gz], [accz]);
> (%o1) [accz=-(Fz*Vt^2*F^4-Fz*Vz^2-2*Fy*Vy*Vz+Fz*Vy^2)/F]
> (%i2)  %[1];
> (%o2) accz=-(Fz*Vt^2*F^4-Fz*Vz^2-2*Fy*Vy*Vz+Fz*Vy^2)/F
>
> I'd like to turn the last output to "accz:
> -(Fz*Vt^2*F^4-Fz*Vz^2-2*Fy*Vy*Vz+Fz*Vy^2)/F" and execute it.
>
> Thanks,
> Lut Mentz

There might be a cleverer solution, but I generally use lhs and rhs:

Maxima 5.23post http://maxima.sourceforge.net
using Lisp SBCL 1.0.45.0.debian
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) [accz=-(Fz*Vt^2*F^4-Fz*Vz^2-2*Fy*Vy*Vz+Fz*Vy^2)/F];
                             2  4        2                     2
                      - Fz Vt  F  + Fz Vz  + 2 Fy Vy Vz - Fz Vy
(%o1)         [accz = ------------------------------------------]
                                          F
(%i2) accz: rhs(first(%o1));
                         2  4        2                     2
                  - Fz Vt  F  + Fz Vz  + 2 Fy Vy Vz - Fz Vy
(%o2)             ------------------------------------------
                                      F
(%i3) 


I can't think of a situation where you don't know ahead of time the
variable name that you were solving for.

Of course, if you're doing this programmatically, you have to test for
empty solution sets and also that the rhs is free of the variable for
which you're solving, since sometimes when solve() fails it returns
something like:

(%i3) solve (sin(x)+cos(x)=1, x);
(%o3)                        [sin(x) = 1 - cos(x)]


Rupert
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 315 bytes
Desc: not available
URL: <http://www.math.utexas.edu/pipermail/maxima/attachments/20110207/62e1e3d8/attachment.pgp>;