Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

I want to modify the value that is returned by Solve. Example:

SolC1 = Solve[Sin[ π x / L] == 1, x]
{{x -> L/2}}

I need to add n to x so that x becomes n + L/2. Both of the following I tried yields unwanted result in the succeeding calculation.

SolC1 = (x -> L/2) + n
SolC1 = SolC1 + n

I particularly need the proper way to do this because x can have other variables depending on other Solve execution.

share|improve this question
Look carefully at your expectation since there should be 2n instead of n (Sin has a period 2 Pi). My answer gives a recommended way to tackle your problem. – Artes Jul 26 '12 at 12:00
It's recommended to start your own symbol names with a lowercase letter. MMA's internal symbols always start with an uppercase letter, and this way you'll avoid conflicts. – stevenvh Aug 12 '12 at 8:15
up vote 7 down vote accepted

Try using

x + n /. First[SolC1]

(* ==> L/2 + n *)

If you need a new rule as the result, then you can do

x -> (x + n /. First[SolC1])
share|improve this answer
@Artes Wrong is a little harsh in this context, isn't it? ReplaceAll is the standard way to substitute in variables, it's what a newcomer will learn first. I assume by "wrong" you mean that Solve didn't return the full solution set, but considering that this was not the focus of the question, and the asker seems to be a beginner, jumping right into complex details that are not the focus of the question is not very educational. – Szabolcs Jul 26 '12 at 12:40
Yes, it is. I meant only L/2 + n is not a correct solution. I like /. but one can do silly mistakes with it e.g. replacing values returned by Solve. – Artes Jul 26 '12 at 12:51

By default Solve returns generic solutions, nevertheless one can make use of its new options (in ver. 8) to make it return all solutions in an appropriate form :

  • MaxExtraConditions - additional (all) solutions can be returned specifying nondefault settings of MaxExtraConditions (default ...-> 0), determining how many equations/conditions on parameters are allowed.
  • GeneratedParameters (this option had been since ver. 5 in other functions (e.g. in Reduce) but in Solve it hadn't worked before ver. 8) to specify names of parameters of the solutions.

Solution in Mathematica 8

In our case we need MaxExtraConditions -> All and GeneratedParameters -> n:

Solve[ Sin[(π x)/L] == 1, x, MaxExtraConditions -> All, GeneratedParameters -> n]
 {{x -> ConditionalExpression[(L (π /2 + 2 π  n[1]))/π , n[1] ∈ Integers && L != 0]}}

ConditionalExpression (also new in ver. 8) is an adequate function since this expression is a solution if and only if n[1] is an integer number and L != 0. Nevertheless if we know that these conditions are true, then ConditionalExpression can be simplified under an adequate assumption (the second argument of Simplify) like e.g. Simplify[ %, n[1] ∈ Integers && L != 0].

It would be more convenient if we did not specify parameters in the assumption but used directly the output of Solve in Simplify. We can do it by taking an appropriate part of the result of Solve, therefore our preferred solution is the following :

Solve[ Sin[(π x)/L] == 1, x, GeneratedParameters -> n, MaxExtraConditions -> All] //
Simplify[#, #[[1, 1, 2, 2]]] &
{{x -> 1/2 L (1 + 4 n[1])}}


Equivalent result in Mathematica 7

In Mathematica 7 we couldn't use GeneratedParameters nor MaxExtraConditions in Solve. Nevertheless we could make (for fun) the output of Reduce similar to that of Solve in M 8. In this specific case an analogical expresion to ConditionalExpression is returned e.g. by

If @@ {And @@ Most @ #, Rule @@ Last @ #}& [
                 List @@ Reduce[ Sin[(π x)/L] == 1, x, GeneratedParameters -> n] ]
 If[n[1] ∈  Integers && L != 0, x -> (L (π/2 + 2 π n[1]))/π]

and to get an appropriately simplified expression we can do e.g.

{{ Simplify[ #, #[[1]] ] &[ If @@ {And @@ Most @ #, Rule @@ Last @ #} &[
      List @@ Reduce[ Sin[(π x)/L] == 1, x, GeneratedParameters -> n]  ] ] }}
{{x -> 1/2 L (1 + 4 n[1])}}

Making any replacements (as in other answers) in the output of Solve can be quite arbitrary and therefore it is not very convenient, nevertheless that way would provide simpler method to modify the output, on the other hand as stated before, in general Solve could not return all solutions in ver. 7.

share|improve this answer
+1 for better follow-up (I don't have time now to test though) – IsaacS Jul 13 '12 at 19:51
@IsaacS Thanks, it works in M 8, one could do something analogical to make it work in M 7 with Reduce though – Artes Jul 13 '12 at 19:56

You could use:

Solve[Sin[ π x / L] == 1, x]

% /. (x -> z_) :> (x -> z + n)
{{x -> L/2}}

{{x -> L/2 + n}}
share|improve this answer
Unfortunately this method often yields incorrect results as it does here. I upvoted your answer for a nicer approach than that in accepted one, however you should improve it. See other comments. – Artes Jul 26 '12 at 12:11
@Artes I'm sorry, I don't understand. The OP wanted n + L/2 and this returns L/2 + n -- how is that wrong? – Mr.Wizard Jul 26 '12 at 12:19
He really wanted a general solution which should be x -> L/2 + 2n instead of x -> L/2 + n where n is integer. – Artes Jul 26 '12 at 12:20
@Artes I read the question again and I'm still not seeing that; have you talked to IsaacS? A new question should be posted if he wants something different. – Mr.Wizard Jul 26 '12 at 12:22
I'm surprised because of the resistance of the matter. I'm giving up improving though I'm right with the remarks above. – Artes Jul 26 '12 at 12:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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