Message Boards Message Boards

Derivative wrt to a vector?

Posted 8 years ago

Hello,

I'm looking to take the derivative of the following function:

a = -mu/r^3 rvec

with respect to rvec. Where rvec is in R3 and r is the norm of rvec. I know the solution is

mu/r^3 [3 rhat rhat^T - I ]

where rhat is the unit vector of rvec, ^T is the transpose and I is a 3x3 identity matrix. How would I do this using mathematica though? Thanks in advance!

POSTED BY: Jeroen Geeraert
7 Replies
Posted 8 years ago

Do you refer to the the Jacobian matrix of a function f:R^n->R^m? In this case you can program this code for an arbitrary function:

J[fct_List, var_List] := 
 Module[{m, n}, m = Length[fct]; n = Length[var];
  Simplify[Table[D[fct[[i]], var[[j]]], {i, 1, m}, {j, 1, n}]]]

In the case of your function a(x,y,z) you define it as follows:

r:={x, y, z};
a:=-my r/(r.r)^(3/2);

An then you simply enter

J[a,r]
POSTED BY: Ulrich Utiger

You could try the following

rvec = {r1, r2, r3};

a = -my /Sqrt[rvec.rvec]^3  rvec

res1= D[a, {rvec}]

res1 // Together // MatrixForm

Now for your 2nd line ( rhat rhat^T is formed by the Outer-construct)

rhat = rvec/Sqrt[rvec.rvec]

res2 = my /Sqrt[rvec.rvec]^3  (3 Outer[Times, rhat, rhat] -  IdentityMatrix[3]) // FullSimplify

res2 // MatrixForm

res1 - res2 // Simplify
POSTED BY: Hans Dolhaine

Hi Hans,

Thanks for your response, this is what I was looking for. Now to take it one step further. Is it possible to get res1 into the following matrix form mu/r^3 [3 rhat rhat^T - I ] from components? This is of course the solution, and normally that wouldn't be known beforehand.

POSTED BY: Jeroen Geeraert

Hello Jeroen,

what exactly do you mean with "from components"?

This perhaps?

res2a = my Table[(3 r[i] r[j] - Sum[r[i]^2, {i, 1, 3}] KroneckerDelta[i, j])/(Sqrt[Sum[r[i]^2, {i, 1, 3}]])^5, {i, 1, 3}, {j, 1, 3}]

And for better comparison with former res2

res2a /. {r[1] -> r1, r[2] -> r2, r[3] -> r3} // MatrixForm
POSTED BY: Hans Dolhaine

By components I mean the solution is given in terms of r1, r2 and r3. Is there a way to get it into vector format. Essentially is there a way to take the derivative of a = -mu/r^3 rvec and get mu/r^3 [3 rhat rhat^T - I ] out, in exactly this form? Currently I'm doing the derivative by hand in vector form, and your answer allows me to check whether I did it right or not. But this was only the first derivative, I need to go up to the 6th so doing this by hand would take a while ... if mathematica could immediately provide a vector solution that'd be great. I've been looking into the package VEST, but haven't figured out how to use it properly.

POSTED BY: Jeroen Geeraert

Sorry, I don't understand your problem.

And what exactly do you mean with 6th derivative? a derived 6 times with respect to rvec? This should give a complicated tensor.....

In any case have a look at

D[ a, {{rvec}, n}] ;
% // MatrixForm

for n = 1, 2 , 3, .......

POSTED BY: Hans Dolhaine

As far as I know Mathematica does not have this functionality readily built-in. But you can program these definitions from e.g. https://en.wikipedia.org/wiki/Matrix_calculus

For example, the operators Del does not have a built-in meaning, but you can add one yourself.

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract