# Mathematica derivative not matching when I take derivative by hand [closed]

I want to take the derivative of this with respect to eta:

fabc = 1/2 (-3 + 27/(2 + x)^2 + (1 - 1/(1 + (-1 + x) c[eta/cc])^2)/
c[eta/cc] + 2 Log[x])


Here I take the derivative:

totalderivativeb = FullSimplify[\!$$\*SubscriptBox[\(\[PartialD]$$, $$eta$$]fabc\)]


The result is:

((-1 + x)^2 (3 + (-1 + x) c[eta/cc]) Derivative[1][c][eta/
cc])/(2 cc (1 + (-1 + x) c[eta/cc])^3)


This result is different than what I get if I take the derivative by hand. Additionally, if I take the finite difference derivative of my function, it matches what I get by hand (numerically), but does not match at all what mathematica gives me.

I have pulling my hair out all day trying to figure out what I am doing incorrectly in mathematica and why it gives me a different answer.

Any ideas what I am doing wrong? Is it not handling some chain rule or something?

Note that "cc" is just a constant.

When I take the derivative by hand, I get below. sorry it is my C++ code. I know it's messy, but it is correct, whereas the mathematica result is not correct. I think mathematica is not doing the chain rule or something appropriately.

  double d = 1.0+(x-1.0)*c(rpf);
double u1 = c(rpf)* 2.0*d*(x-1.0)*cprime(rpf)/d/d/d/d  - (1.0 - 1.0/d/d)*cprime(rpf);
double u2 = 2.0*c(rpf)*c(rpf);

double first = u1/u2;


Note that "rpf" = eta/cc

• This result is different than what I get if I take the derivative by hand. what is your hand derivative looks like? – Nasser Dec 6 '19 at 21:44
• Just added to answer. The derivative by hand is given by "first" where you can see it defined above – Jackson Hart Dec 6 '19 at 21:46
• What do you expect D[f[x/a], x] to return? – Carl Woll Dec 6 '19 at 23:18
• In your handsolution cprime isn't defined? cprime=D[c[eta/cc],eta] or cprime=c'[eta/cc]And that's the point where the difference occurs, I think! – Ulrich Neumann Dec 7 '19 at 10:34
• @UlrichNeumann I think you may be right. In mathematica c'[eta/cc] means the derivative of c with respect to eta/cc, whereas in my C++ code, c' was derivative of c with respect to just eta. I think this is why I thought mathematica was wrong. It's not wrong, it just interprets the derivative differently – Jackson Hart Dec 9 '19 at 16:43

I would be shocked if Mathematica gave the wrong derivative. It's more or less a solved class of problems with very simple rules. If any part of the implementation was wrong, I have to imagine huge numbers of derivatives would be broken.

Your cc only ever appears in the denominator, so there should be no chance to cancel out. Working it out by hand, I get the same as Mathematica. I would recommend applying the derivative rules manually and asking Mathematica to evaluate the derivative of each part to see if you agree.

fabc = 1/2 (-3 + 27/(2 + x)^2 + (1 - 1/(1 + (-1 + x) c[eta/cc])^2)/c[eta/cc] + 2 Log[x])
f = 1 - 1/((x - 1) c[eta/cc] + 1)^2
g = (2 c[eta/cc])^-1
fp = D[f, eta]
gp = D[g, eta]


$$1-\frac{1}{\left((x-1) c\left(\frac{\text{eta} }{\text{cc}}\right)+1\right)^2}$$

$$\frac{1}{2 c\left(\frac{\text{eta} }{\text{cc}}\right)}$$

$$\frac{2 (x-1) c'\left(\frac{\text{eta} }{\text{cc}}\right)}{\text{cc} \left((x-1) c\left(\frac{\text{eta} }{\text{cc}}\right)+1\right)^3}$$

$$-\frac{c'\left(\frac{\text{eta} }{\text{cc}}\right)}{2 \text{cc} c\left(\frac{\text{eta} }{\text{cc}}\right)^2}$$

FullSimplify[f gp + g fp == D[fabc, eta]]
(* True *)


As Carl Woll pointed out, my guess is that you haven't accounted for the fact that

$$\frac{\partial }{\partial \text{eta} }c\left(\frac{\text{eta} }{\text{cc}}\right) = \frac{1}{\text{cc}}c'\left(\frac{\text{eta}}{cc}\right)$$