# Derivative does not correspond to function

I have an implicitly defined function, f[p], and I plot it together with its derivative. As is clear from the picture, the function is increasing, but the derivative is negative. I suspect this is because the function might have multiple solutions, and that somehow the derivative comes from differentiation of a different solution than the plotted function. Could that be true? Otherwise, what's happening here?

ClearAll[r, p, lambda, a, A, c, eta, f, y, constant1, constant2, eta, \
etaOfR]
constant1[lambda_] := Exp[-lambda]/(1 - Exp[-lambda]);
constant2[lambda_] := constant1[lambda]*(Exp[lambda] - 1 - lambda);
eta[lambda_, f_] :=
1 - constant2[lambda] +
constant1[lambda]*(Exp[lambda*(1 - f)] - 1 - lambda*(1 - f)) ;
etaOfR[lambda_] := Limit[eta[lambda, f], f -> 1];

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[2, 1, 5];
Plot[{f[p], f'[p]}, {p, 0.1, r}]


(f[p] is in blue, f'[p] in yellow)

ClearAll[r, p, lambda, a, A, c, eta, f, y, constant1, constant2, eta, etaOfR]
constant1[lambda_] := Exp[-lambda]/(1 - Exp[-lambda]);
constant2[lambda_] := constant1[lambda]*(Exp[lambda] - 1 - lambda);
eta[lambda_, f_] :=
1 - constant2[lambda] +
constant1[lambda]*(Exp[lambda*(1 - f)] - 1 - lambda*(1 - f));
etaOfR[lambda_] := Limit[eta[lambda, f], f -> 1];

prof[p_] := (p - c)/p;

eqn = eta[lambda, f]*prof[p] == etaOfR[lambda]*prof[r] // Simplify;

f[p_] = f /. Solve[eqn, f][[1]] // FullSimplify // Quiet;

{r, c, lambda} = {2, 1, 5};

Plot[{f[p], f'[p]}, {p, 0.1, r},
PlotLegends -> Placed["Expressions", {.25, .75}]]


• Could you please elaborate on how/why you differ from my code, and why you're getting the same branch on the function and its derivative while i dont? – FooBar Nov 6 '19 at 15:54
• You solve the equation for each individual value of p. I solve the equation once for all values of p. That is why my code executes much faster. The individual solutions apparently do not share a common solution. Since Plot has the attribute HoldAll, you could also just change your code to Plot[Evaluate@{f[p], f'[p]}, {p, 0.1, r}] which would force evaluation of the Solve and derivative once. – Bob Hanlon Nov 6 '19 at 19:01