save results of solve in variables

1.3k views Asked by At

I am solving the following equation for 2 variables.

Solve[{2*x1* y1 + 2*k*x3*y3 + (Sqrt[2 + q])*x1 == m1,
  2*x1*y3 - 2*x3*y1 - (Sqrt[2 + q])*x3 == m2}, {x1, x3}]

The output od the code is {{x1 -> 18/61, x3 -> -(15/61)}} How can I save these outputs in 2 separate variables.

1

There are 1 answers

0
Alan On

First of all, you have given the output for specific values of other variables, which you have not shown us. Evidentally, the way you are coding introduces some loss of transparency. You might better proceed with something like this:

eqn01 = 2*x1*y1 + 2*k*x3*y3 + (Sqrt[2 + q])*x1 == m1;
eqn02 = 2*x1*y3 - 2*x3*y1 - (Sqrt[2 + q])*x3 == m2;
params = {y1 -> 1., y3 -> 3, m1 -> 1, m2 -> 2, q -> 1, k -> 1}; (* yr vals here *)
solns = Solve[{eqn01, eqn02} /. params, {x1, x3}]

Second, WL supports multiple assignment by unpacking, so if you really mean to save your two solutions as they are you can just unpack them. E.g.,

{soln11, soln12} = First@solns

In general, there is really no need to do this. In fact, since these are two pieces of a single solution, in general will not make sense to separate them. However, it can be useful to get your hands on the actual values:

{x1, x3} /. First@solns

Naturally, you could store this list of values in a variable, but that is seldom needed. Finally, if you really insist on introducing variables solely for the purpose of storing these two separate values -- which will almost always create unwanted clutter -- you can again unpack them:

{val1, val3} = {x1, x3} /. First@solns