# Mathematica: Derivative appears to be wrong

I'm first getting f(x) by computing an implicit equation. Then I want to take the derivative. Later on, when I plot both f(x) and f'(x), they do not appear to correspond: the derivative is always negative. What am I doing wrong?

ClearAll[r, p, lambda, a, A, c, eta, f, y, constant1, constant2, eta, \
etaOfR]

eta[lambda_, f_] :=
1 - constant2[lambda] +
constant1[lambda]*(Exp[lambda*(1 - f)] - 1 - lambda*(1 - f)) ;
etaOfR[lambda_] := Limit[eta[lambda, f], f -> 1];
constant1[lambda_] := Exp[-lambda]/(1 - Exp[-lambda]);
constant2[lambda_] := constant1[lambda]*(Exp[lambda] - 1 - lambda);
prof[p_] := (p - c)/p;
f[p_] := f /.
Solve[eta[lambda, f]*prof[p] == etaOfR[lambda]*prof[r], f] //
First // FullSimplify
{r, c, lambda} = List[4, 0.1, 0.01];
fDeriv[p_] = f'[p] // FullSimplify;
Plot[f[p], {p, 0.1, r}]
Plot[fDeriv[p], {p, 0.1, r}]


Here's the two plots: first f, then f':

It looks the same when I first compute the derivative and then define {r, c, lambda}. I already swapped the order thinking that the unknowns were causing trouble, but the underlying issue appears to be something else.

• It looks to me that your fDeriv is using the wrong branch of the Lambert function. Try replacing all instances of ProductLog[x] with ProductLog[-1, x]. – J. M. will be back soon Dec 13 '15 at 16:28
• @J.M. is there any clean way of doing that, i.e. not explicitely rewriting f()? I tried f[p_] := f ... , f, Reals], but that keeps on going. – FooBar Dec 13 '15 at 16:32
• That should be fManuallyWritten'[p], no? – J. M. will be back soon Dec 13 '15 at 16:44
• You might consider using Reduce[] to help you with how to restrict the domain properly. Ponder the difference between Reduce[x == y Exp[y], y] and Reduce[{x == y Exp[y], y < -1}, y], for instance. – J. M. will be back soon Dec 13 '15 at 16:52
• Define ff[p_]=f[p] (no SetDelayed). Then define the derivative in terms of ff[p] and the results will be consistent. – Daniel Lichtblau Dec 14 '15 at 5:02