0
$\begingroup$

I need to output the answers from solve into the variables named just as they were named in the solve equations. I have checked out this thread

Assign the results from a Solve to variable(s)

but this is more about one variable having different values being stored. So you extract one data at a time and all. What I want is basically have the output of this solve below stored to the variables i1, i2, i3 and i4 so I can use them in further calculations.

In: Solve[-I (i2 - i1) + 5 (i2 - i4) + I (i2 - i3) == 0 && I (i3 - i2) + i3 == 0 && 5 (i4 - i2) + 1 + I == 0 && i1 + 2 == 0, {i1, i2, i3, i4}]

Out: {{i1 -> -2, i2 -> -2, i3 -> -1 - I, i4 -> -(11/5) - I/5}}

I tried something like

{i1, i2, i3, i4} -> {-2, -2, -1 - I, -(11/5) - I/5}

but then

in: I (i1 - i3)
out: I (i1 - i3)

is what I got. The capital I are basically the imaginary operator but it becomes that when I copy paste here.

PS. I am not using linear solvers here or, better yet, MATLAB because I don't want to go through the troublesome arithmetics of simplification to get the coefficients.

$\endgroup$
  • $\begingroup$ If you are very certain that you obtain only one set of solutions from Solve, then you could use Solve[youreqautions, yourvariables] /. Rule -> Set. This will turn the list of rules returned by Solve into a list of assignments to the variables. Note that once those variables have values assigned, your Solve expression will no longer work until the values are cleared. Again, I really would not recommend using this: you should rather save the solutions / rules returned by Solve and apply them whenever you need to inject the variable values in expressions. $\endgroup$ – MarcoB Mar 22 '16 at 5:59
2
$\begingroup$

What you do is something like this

sols = First[Solve[-I (i2 - i1) + 5 (i2 - i4) + I (i2 - i3) == 0 && I (i3 - i2) + i3 == 0 && 5 (i4 - i2) + 1 + I == 0 && i1 + 2 == 0, {i1, i2, i3, i4}]];

{i1, i2, i3, i4} /. sols

to extract the required solutions.

If as you say you "don't want to go through the troublesome arithmetics of simplification to get the coefficients", then here is something for you:

eqns= {-I (i2 - i1) + 5 (i2 - i4) + I (i2 - i3) == 0 , I (i3 - i2) + i3 == 0 , 5 (i4 - i2) + 1 + I == 0 , i1 + 2 == 0};

{b, A} = Normal[CoefficientArrays[Subtract @@@ eqns, {i1, i2, i3, i4}]]

LinearSolve[A, -b]

$\endgroup$
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Mar 22 '16 at 6:25
  • $\begingroup$ Hi Louis, is this not a good answer already to a question? $\endgroup$ – Jokeur Mar 22 '16 at 6:32
  • $\begingroup$ Jokeur, I wouldn't worry about that comment. That's a boilerplate welcome to the site for first time question-writers. @Louis must have mistakenly positioned it on your answer. You can safely disregard it. Additionally, in order for the system to notify another user that you answered their comment, you should include @+their username, like I did above. Otherwise people have no way of knowing that you have replied to their comments. $\endgroup$ – MarcoB Mar 22 '16 at 7:04
  • $\begingroup$ OK, thanks @MarcoB. I am not new to Mathematica, but still getting hang of Stack Exchange. $\endgroup$ – Jokeur Mar 22 '16 at 7:06
  • $\begingroup$ Also, are you the same user that wrote this answer? If so, you may have two duplicate unregistered accounts. You should register for the site and merge the two accounts (and reputation!) as explained here: How to merge accidentally created accounts. $\endgroup$ – MarcoB Mar 22 '16 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.