# Chain rule while differentiating

I am trying to find the derivative of a function defined in polar coordinates with respect to $x$ and $y$. My function is defined as follows:

$v_x(r, \theta ) = v_r \cos (\theta ) - v_{\theta }\sin (\theta )$

To do this, I start by defining the relation between Cartesian and Polar coordinates:

(* Define the mapping between Cartesian and Polar coordinate systems. *)
x[r_, θ_] = r Cos[θ];
y[r_, θ_] = r Sin[θ];


Then I define the function and find its derivative with respect to $x$:

Subscript[v, r][r_, θ_] = Subscript[v, r][r, θ] Cos[θ] - Subscript[v, θ][r, θ] Sin[θ];
D[Subscript[v, r][r, θ], x]


I am getting 0 because Mathematica is not considering the relation between $r$ and $x$. Is there anyway to tell Mathematica to use the chain rule to find the derivative of $v_x$ with respect to $x$?

The other problem is that Mathematica is considering the subscripts to be variables (which is reasonable), is there anyway to tell it that the subscripts are only notational symbols?

EDIT: The function is better defined as:

vx[r_, θ_] = vr[r, θ] Cos[θ] - vtheta[r, θ] Sin[θ];


to avoid evaluating subscripts and possibly having recursion.

-

In your case it might be more convenient to define the inverse transformation :

rho[x_, y_] = Sqrt[x^2 + y^2]
theta[x_, y_] = ArcTan[x, y]

vx[r_, \[Theta]_] = vr[r, \[Theta]] Cos[\[Theta]] - vtheta[r, \[Theta]] Sin[\[Theta]];


Then this will use the chain rule :

 D[vx[rho[x, y], theta[x, y]], x]


One can simplify the result in terms of the polar coordinated :

Simplify[D[vx[rho[x, y], theta[x, y]], x] /. {x^2 + y^2 -> rho^2, ArcTan[x, y] -> theta, x -> rho Cos[theta], y -> rho Sin[theta]}, Assumptions -> {rho >= 0}]

-
This works but it produces everything in terms of x and y, which is very complicated! –  Rafid Aug 4 '12 at 11:03
well if it works you could always upvote it to indicate that you appreciate his effort :) –  acl Aug 4 '12 at 11:21
@Rafid Please see edit for some additional simplification. –  b.gatessucks Aug 4 '12 at 11:25
@b.gatessucks, for total conversion back to polar coordinates you will need to add x -> rho Cos[theta], y -> rho Sin[theta] to the replacement rules. –  Simon Woods Aug 4 '12 at 11:41
@SimonWoods Thank you. –  b.gatessucks Aug 4 '12 at 12:06

You can use the total derivative Dt:

x[r_, \[Theta]_] = r Cos[\[Theta]];
y[r_, \[Theta]_] = r Sin[\[Theta]];


then for instance

Dt[a[r, \[Theta]]*Cos[\[Theta]] - b[r, \[Theta]]*Sin[\[Theta]], x]


does this

I can't test your example because Subscript[v, r][r_, \[Theta]_] = Subscript[v, r][r, \[Theta]] Cos[\[Theta]] - Subscript[v, \[Theta]][r, \[Theta]] Sin[\[Theta]] hits the recursion limit (because the way it's defined and the evaluation sequence works, it'll never finish). However, here's how to indicate that something is a constant. Suppose I try:

Dt[Sin[\[Theta]] + c, x]


but $c$ is a constant; I can indicate this like so:

Dt[Sin[\[Theta]] + c, x, Constants -> {c}]


-
The problem with this solution is that it is not substituting the values and dr/dx and dtheta/dx, so it is not really making use of the Cartesian-Polar equations! –  Rafid Aug 4 '12 at 12:46
@Rafid Right. So you expect to define x[r,Theta]:=etc and have Mathematica automatically insert the values for $\partial_x\theta$ etc? That's not going to be 2 lines of code. By the way, did you notice that the code in your question goes into infinite recursion? –  acl Aug 4 '12 at 12:53
Well, honestly, I am relatively new to Mathematica, so I thought this might be possible since I am already passing in the relation. Anyway, thanks, one vote :-) And yes, I did notice the recursion, I am going to edit my question now. –  Rafid Aug 4 '12 at 13:02