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

If I have an equation

eq = f[x] + g[x] == 0

and I solve for f[x]

sol = Solve[eq,f[x]]

how do I use sol to replace both f[x] and its derivatives in another equation? For example

eq2 = f'[x]*g[x] + f[x] == 0

and then

eq2 /. sol


{-g[x] + g[x] f'[x] == 0}
share|improve this question
up vote 6 down vote accepted

The problem is caused by the internal structure of Derivative:

f'[x] // FullForm

(* ==> Derivative[1][f][x]  *)

There's no f[x] in there to replace. Using D[f[x], x] instead of f'[x] and Holding the whole equation works:

eq2 = Hold[D[f[x], x]*g[x] + f[x] == 0];

ReleaseHold[eq2 /. sol]

(*  ==> {-g[x] - g[x] Derivative[1][g][x] == 0}  *)
share|improve this answer
Thanks. This works great for the problem, but for what I'm doing I can't hold eq2 b/c I need to do some other calculations on it first. So I tried ReleaseHold[Hold[eq2 /. f'[x] -> D[f[x], x]] /. sol] which gives {f[x] - g[x] g'[x]}. But ReleaseHold[Hold[eq2 /. f'[x] -> D[f[x], x]] /. sol] /. sol does give the desired answer. – al0 May 28 '12 at 20:50

If you plan to use derivatives, or solve for functions, you could use DSolve instead of Solve.

sol = DSolve[eq, f, x];
eq2 /. sol

{-g[x] - g[x] Derivative[1][g][x] == 0}

If not, apart from avoiding the use of x overall, you could do something like this

parseRule = (f_[x_] -> sth_) :> (f -> (Evaluate[sth /. x -> #] &));

sol = Solve[eq, f[x]]

eq2 /. (sol /. parseRule)

{-g[x] - g[x] Derivative[1][g][x] == 0}

I have the feeling there are better options. We'll see

share|improve this answer

An approach that may in the long run help avoid confusion in your calculation is to use a different name for the function that is the solution to the equation eq = f[x] + g[x] == 0.

What I mean is this:

fSolved[x_] = f[x] /. First@Solve[eq, f[x]]

eq2 = fSolved'[x]*g[x] + fSolved[x] == 0

(* ==> {-g[x] - g[x] Derivative[1][g][x]} == 0 *)

So here I chose the name fSolved for the actual solution with which I want to work later.

This is mathematically more sane, I think, because Solve for eq could in principle give you several possible solutions in the form of a list of rules f[x] -> ..., and then you'd have to give each solution different names anyway if you want to keep working with both of them. As an example, consider

eq = f[x]^2 - g[x] == 0

Then by using First above we select the negative square root, and we could use a different name for the positive square root (Last@Solve...).

share|improve this answer
+1, definately more healthy. Any reason why you use :=Evaluate instead of plain = ? Syntax coloring? – Rojo May 28 '12 at 1:43
@Rojo Yes - just to make sure we can also use fSolved[y] in eq2 (i.e., be independent of the name x for the variable) if we feel like it... – Jens May 28 '12 at 2:28
I meant fSolved[x_]:=Evaluate[...] instead of fSolved[x_]=... – Rojo May 28 '12 at 2:34
@Rojo Oh I see, yes in this case that's the same thing. I don't remember now why I thought it's better... – Jens May 28 '12 at 3:40
@Rojo Thanks for curing one of my bad habits - I removed the unnecessary Evaluate. – Jens May 28 '12 at 3:55

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.