Extracting components of a equation solution

Hi all

I have trouble extracting components from of  equation solutions:

p1 := x^2+y;

e:=solve({p1}, {x});

                                         e := {x = sqrt(-y)}, {x = -sqrt(-y)}

next I used rhs() to extract right component of the first solution

x1 := rhs(e[1]);

but I got the error:

Error, invalid input: rhs received {x = (-y)^(1/2)}, which is not valid for its 1st argument, expr

I don't know how to deal with this problem.

Could someone help me out?  Thanks alot!!

Tain

 

 

acer's picture

equation

The rhs() routine expects an equation, and will not accept a set (even if its only member is an equation). So you could get your hands on that equation itself, or map the rhs() function over all the elements of the set.

> p1 := x^2+y;
                                        2
                                 p1 := x  + y

> e:=solve({p1}, {x});
                                    1/2             1/2
                      e := {x = (-y)   }, {x = -(-y)   }


> op(e[1]);
                                          1/2
                                  x = (-y)

> x1 := rhs(op(e[1]));
                                           1/2
                                 x1 := (-y)

> x1 := map(rhs,e[1]);
                                           1/2
                                x1 := {(-y)   }

> e[1][1];
                                          1/2
                                  x = (-y)

> x1 := rhs(e[1][1]);
                                           1/2
                                 x1 := (-y)

acer

appreciate it, acer!!!

appreciate it, acer!!!

JacquesC's picture

Why extract it?

Instead of fooling around with op, why not use the solution directly?  For example

x1 := eval(x, e[1]);

will work just as well. It will work over expressions in x too, which is the whole point of having the output of solve being in that format, it can be fed as input to eval as is.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
}