# Higher derivative [closed]

Is there more efficient way to rewrite this code in order to compute 2nd and higher derivatives of r=sqr{x^2+y^2+(z-a)^2}?

Table[D[r[x, y, z], xIN[[i]]] -> xIN[[i]]/r[x, y, z], {i, 1, 3}]


Thank you for your help!

-

## closed as unclear what you're asking by Jens, RunnyKine, Michael E2, bobthechemist, m_goldbergAug 3 '14 at 4:19

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

What does xIN represent? Can you post your full code? –  blochwave Aug 2 '14 at 21:04
@blochwave, xIN stands for xIN = {x, y, z} –  Dilaton Aug 2 '14 at 21:06
Please use correct Mathematica syntax and be more specific as to the form of the result you expect. Also look at Quick Hessian matrix and gradient calculation - this may be a duplicate. –  Jens Aug 2 '14 at 22:55

To compute $\nabla^nr$ for arbitrary integer $n$, you can use the built-in tensor derivative syntax. For example, you can compute the second-derivative $\nabla^2r$ using

r = Sqrt[x^2 + y^2 + (z - a)^2];
X = {x, y, z};
D[r, {X, 2}]


To get an answer in terms of $r$, you can sort of cheat your way to the correct answer via the following modification:

r = Sqrt[x^2 + y^2 + (z - a)^2];
X = {x, y, z};
Simplify[D[r, {X, 2}] /. (x^2 + y^2 + (z - a)^2 -> R^2),
Assumptions -> R > 0]


yielding

$$\nabla^2R=\left( \begin{array}{ccc} \frac{R^2-x^2}{R^3} & -\frac{x y}{R^3} & \frac{x (a-z)}{R^3} \\ -\frac{x y}{R^3} & \frac{R^2-y^2}{R^3} & \frac{y (a-z)}{R^3} \\ \frac{x (a-z)}{R^3} & \frac{y (a-z)}{R^3} & -\frac{a^2-2 z a-R^2+z^2}{R^3} \\ \end{array} \right).$$

-
thanks for your answer! Please try to avoid this complicated tips. My motivation is to keep the code and outcome in more simple form in terms of r. –  Dilaton Aug 2 '14 at 22:06
@Dilaton: I edited the answer to help make it output the answer in terms of $R$. –  DumpsterDoofus Aug 2 '14 at 22:13
r[x_, y_, z_] = Sqrt[x^2 + y^2 + (z - a)^2];

D[r[x, y, z], #] & /@ {x, y, z}


{x/Sqrt[x^2 + y^2 + (-a + z)^2], y/Sqrt[ x^2 + y^2 + (-a + z)^2], (-a + z)/Sqrt[x^2 + y^2 + (-a + z)^2]}

or more simply,

% == D[r[x, y, z], {{x, y, z}}]


True

%% == {x, y, z - a}/r[x, y, z]


True

EDITED to add higher order partial derivatives

Second partial derivatives

D[r[x, y, z], {#, 2}] & /@ {x, y, z} // FullSimplify


{(y^2 + (a - z)^2)/(x^2 + y^2 + (a - z)^2)^(3/2), ( x^2 + (a - z)^2)/(x^2 + y^2 + (a - z)^2)^(3/2), ( x^2 + y^2)/(x^2 + y^2 + (a - z)^2)^(3/2)}

Third partial derivatives

D[r[x, y, z], {#, 3}] & /@ {x, y, z} // FullSimplify


{-((3 x (y^2 + (a - z)^2))/(x^2 + y^2 + (a - z)^2)^(5/2)), -(( 3 y (x^2 + (a - z)^2))/(x^2 + y^2 + (a - z)^2)^(5/2)), ( 3 (x^2 + y^2) (a - z))/(x^2 + y^2 + (a - z)^2)^(5/2)}

-
Hanion, thank you for your answer! Could you please teach me also how to code 2nd derivative? I would like to see pattern in order to understand computations for higher derivatives! –  Dilaton Aug 2 '14 at 22:00
@Dilaton - edited above –  Bob Hanlon Aug 2 '14 at 23:18