# [Maxima] Assigning solve result to function

Rupert Swarbrick rswarbrick at gmail.com
Sat Nov 28 09:57:22 CST 2009

```Dmitry Vinokurov <d.vinokuroff at gmail.com> writes:

> I've got an equation, for example 'x_1 + x_2 = 0' (of course real case
> is not so simple, but it's enough for demonstration) and I need to get
> function F(x_2) from it.
>
> I tried to use solve for it 'solve(x_1 + x_2 = 0, x_1)' and get 'x_1 = -
> x_2'. Now I couldn't find, how to extract right part of solve result
> into function. Just 'F(x_2) := solve(x_1 + x_2 = 0, x_1)' of course
> doesn't work, it results to 'x_1 = -x_2' instead of wanted '- x_2'.

Hi,

I'm not sure whether you really want solve, since I'm not sure exactly
what you're doing, but something like the following should work:

Maxima 5.19post http://maxima.sourceforge.net
using Lisp SBCL 1.0.31.0.debian
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) eq: x_1+x_2 = 0;
(%o1)                            x_2 + x_1 = 0
(%i2) f(x_2) := ''(rhs(first(solve(eq, x_1))));
(%o2)                           f(x_2) := - x_2
(%i3) f(3);
(%o3)                                 - 3
(%i4) f(x_2) := rhs(first(solve(eq, x_1)));
(%o4)                f(x_2) := rhs(first(solve(eq, x_1)))
(%i5) f(3);
(%o5)                                - x_2
(%i6)

Note that solve returns a list of solutions, so first() selects
(hopefully) the only member of the list and rhs then selects the right
hand side.

Also note the ''( ) to make sure the definition gets evaluated: the
second version doesn't do what you want.

Of course, this only works if solve manages to find exactly one solution
to your equation and can express it in the form x_1 = g(x_2).

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/20091128/ef51d76a/attachment.pgp
```