Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

It's often useful to get just one number from Solve. I use the following construction for acquiring one solution from the expression returned by Solve.

q = t /. Solve[p == 2 t + 1, t][[1]]

Mostly I don't like to use the different variable name within Solve. And the rule-applying seems very unnatural. I think a solution that is more elegant must exist. Can anyone show me this?

share|improve this question

marked as duplicate by Artes, b.gatessucks, m_goldberg, Yves Klett, Michael E2 Mar 9 '14 at 18:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Solve[p == 2 t + 1, t][[1, 1, 2]] ? – b.gatessucks Mar 9 '14 at 11:04
Wow, thank you, I'll serch for what does it mean... – Himura Mar 9 '14 at 11:06
Since you ask for the first solution you should use e.g. s[[1, 1, 2]] instead of s[[All, 1, 2]] etc. However you should remember that more flexible way is using ReplaceAll, e.g. here t /. First @ Solve[..., t]. – Artes Mar 9 '14 at 11:18
I feel closing this is fine, but I am glad I had the opportunity to post an answer. – Jacob Akkerboom Mar 9 '14 at 13:00

Just extract second argument from Rule function by

Solve[p == 2 t + 1, t][[1, All, 2]]

Use All in case of more then one solution.

share|improve this answer
Thank you. Now it's a bit better. But the problem of the variable names is still exist – Himura Mar 9 '14 at 11:12

I am not sure what exactly you mean by "the problem of the variable names" (in the comments). Anyway you could construct a function like this (ignore the red syntax highlighting in the definition)

SyntaxInformation[solveAndAssign] = {"LocalVariables" -> {"Solve", {2, 2}}};
SetAttributes[solveAndAssign, HoldAll];
solveAndAssign[eqn_, var_Symbol] :=
  var =
    var /.


q = 3;
solveAndAssign[p == 2 q + 1, q];


1/2 (-1+p)

In the example above you see that you can use the variable you want to assign to in your equation. The code still works if you put your equation in a variable (the attribute HoldAll might make you think otherwise). Of course the equation has to formulated in terms of q rather than the value of q, which is why we Clear q for the moment.

eqn = p == 2 q + 1;
q = 3;
solveAndAssign[eqn, q];


1/2 (-1+p)

About the code

I have made no effort to make this work with multiple variables. I suppose people might object to the variable having a color that implies it is local, while an assignment is made to that variable, which is valid. But I think the name of the function should make the intention clear.

The main point of this answer is to show how you could do something like this, I don't think the function is particularly useful.

share|improve this answer

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