5
$\begingroup$

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

outputs

{-g[x] + g[x] f'[x] == 0}
$\endgroup$

3 Answers 3

7
$\begingroup$

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}  *)
$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$
    – al0
    May 28, 2012 at 20:50
3
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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...).

$\endgroup$
6
  • $\begingroup$ +1, definately more healthy. Any reason why you use :=Evaluate instead of plain = ? Syntax coloring? $\endgroup$
    – Rojo
    May 28, 2012 at 1:43
  • $\begingroup$ @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... $\endgroup$
    – Jens
    May 28, 2012 at 2:28
  • $\begingroup$ I meant fSolved[x_]:=Evaluate[...] instead of fSolved[x_]=... $\endgroup$
    – Rojo
    May 28, 2012 at 2:34
  • $\begingroup$ @Rojo Oh I see, yes in this case that's the same thing. I don't remember now why I thought it's better... $\endgroup$
    – Jens
    May 28, 2012 at 3:40
  • $\begingroup$ @Rojo Thanks for curing one of my bad habits - I removed the unnecessary Evaluate. $\endgroup$
    – Jens
    May 28, 2012 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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