Differentiation operators

There are also built-in functions to find the gradient, divergence, and curl (among other things):

Grad[x^2 + y Cos[z]]

{0, 0, 0}

Notice, we have a problem here.  The gradient of the given functions is certainly not the 0 vector.  So, what is going on?  This goes back to the issue I mentioned in the introduction.  This gradient was taken in the default coordinate system (Cartesian), but it was taken with the Mathematica default variables for that coordinate system.  Let's find out what those are:

CoordinateSystem

Cartesian

Coordinates[]

{Xx, Yy, Zz}

So, as far as Mathematica is concerned, the variables in this coordinate system are Xx, Yy, and Zz (strange but true).  That means that when we took the gradient above, it treated x, y, and z as constants.  There are two ways to fix this:

We could conform:

Grad[Xx^2 + Yy Cos[Zz]]

{2 Xx, Cos[Zz], -Yy Sin[Zz]}

Personally, I find this a bit bizarre to try to read.  The other way is to tell Mathematica to use our preferred variables:

Grad[x^2 + y Cos[z], Cartesian[x, y, z]]

{2 x, Cos[z], -y Sin[z]}

Or, if you need to do a lot of work in Cartesian coordinates with your preferred variables, you could set the default coordinate system this way:

SetCoordinates[Cartesian[x, y, z]]

Cartesian[x, y, z]

Grad[x^2 + y Cos[z]]

{2 x, Cos[z], -y Sin[z]}

Grad[x^2 + y Cos[z], Cartesian[x, y, z]]

{2 x, Cos[z], -y Sin[z]}

Thus, it is extremely important that, before you start doing computations in any coordinate system using Mathematica's built-in functions, you are aware of what variables Mathematica is expecting (or you set them like you want them).

Of course, a more interesting question is, how do these things work in other coordinate systems?  Let's look at the gradient in spherical coordinates:

Coordinates[Spherical]

{Rr, Ttheta, Pphi}

SetCoordinates[Spherical[ρ, ϕ, θ]]

Spherical[ρ, ϕ, θ]

Warning:  The default coordinate system is now set to spherical until you change it again.  The safe thing to do is to always specify the coordinate system when you perform a computation.  Also, notice that I am taking this opportunity to rename the variables so they fit the ones we are used to (though they still need to be entered in {ρ,φ,θ} order).

Grad[ρ^2Sin[ϕ] Cos[θ]]

{2 ρ Cos[θ] Sin[ϕ], ρ Cos[θ] Cos[ϕ], -ρ Sin[θ]}

Or, we can get a general formula for the gradient of a function f[ρ,φ,θ] whose coordinates are in spherical coordinates:

Grad[f[ρ, ϕ, θ]]

{f^(1, 0, 0)[ρ, ϕ, θ], f^(0, 1, 0)[ρ, ϕ, θ]/ρ, (Csc[ϕ] f^(0, 0, 1)[ρ, ϕ, θ])/ρ}

In this expression, f^(1, 0, 0)[ρ,φ,θ] stands for the partial derivative of f with respect to the first coordinate (ρ), etc.  Notice that, in addition to the expected partial derivatives, you get some extra coefficients, in this case 1/ρ and Csc[ϕ]/ρ (usually written as 1/(ρ Sin[ϕ])).  A complete treatment of where these factors come from is beyond the scope of this lab, but basically they are the reciprocals of the coefficients needed to convert a change in that variable into an actual change in arc-length (called the "Scale Factors").  So, in other words, if you increase ρ by , then it just gets longer by , so the scale factor is 1.  However, if you increase φ by , the actual change in distance (i.e., arc-length) covered is ρ dφ, so the scale factor is ρ.  (This is easier to see in polar coordinates.  If you increase θ by , the actual change in arc-length is r dθ, so the scale factor would be r.)  To see the scale factors for a particular coordinate system, you can use:

ScaleFactors[Spherical[ρ, ϕ, θ]]

{1, ρ, ρ Sin[ϕ]}

ScaleFactors[Cylindrical[r, θ, z]]

{1, r, 1}

Now, you only have to deal with this when you need your output vectors given in your non-Cartesian coordinate system.  In the cases we looked at above, the gradients are given as vectors in spherical coordinates (i.e., these are the components of unit vectors in the ρ, φ, and θ directions, respecitvely).  This is mainly useful if you are doing all your work in that coordinate system.  (The good news is that all the built-in differential operators, like grad, div, and curl, automatically take care of this for you, if you specify your coordinate system.)

On the other hand, if you really needed to work with a function from an alternate coordinate system in Cartesian coordinates (for example if you wanted to actually graph the function or its gradient vector field or tangent plane in Mathematica), one way to handle this is to convert the function to Cartesian coordintes to begin with.

If you had the function (in spherical coordinates):

f[{ρ_, ϕ_, θ_}] := ρ^2Sin[ϕ] Cos[θ]

You could first transform it into a function of {x,y,z}:

fCart[{x_, y_, z_}] = f[CoordinatesFromCartesian[{x, y, z}, Spherical]]//FullSimplify

((x^2 + y^2) Cos[ArcTan[x, y]])/(x^2 + y^2)/(x^2 + y^2 + z^2)^(1/2)

From then on, you would treat this as a standard Cartesian function (admittedly ugly).  Notice the liberal use of FullSimplify in this work; it is used to automatically apply things like trig identities, etc.

Grad[fCart[{x, y, z}], Cartesian[x, y, z]]//FullSimplify

{((x^2 + y^2)/(x^2 + y^2 + z^2)^(1/2) (2 x^2 + y^2 + z^2))/(x^2 + y^2)^(1/2), (x y (x^2 + y^ ...  + y^2 + z^2)^(1/2))/(x^2 + y^2)^(1/2), z (x^2 + y^2)/(x^2 + y^2 + z^2)^(1/2) Cos[ArcTan[x, y]]}

This gives you the gradient in terms of your standard i, j, and k unit vectors.  You can get a much nicer looking gradient by taking it directly in spherical:

sphGrad = Grad[f[{ρ, ϕ, θ}], Spherical[ρ, ϕ, θ]]//FullSimplify

{2 ρ Cos[θ] Sin[ϕ], ρ Cos[θ] Cos[ϕ], -ρ Sin[θ]}

But this gives you the gradient vector in terms of the unit vectors in the ρ, φ, and θ directions (and remember that the unit vectors in the φ and θ directions rotate, depending on what angle they are at, so they are not all parallel to each other).  Which method you use depends on what you are trying to do.  (Of course, with spherical and cylindrical coordinates, Mathematica is perfectly capable of graphing them "as-is", but that isn't true about other more esoteric coordinate systems.  Also, if you were working with different functions in different coordinate systems, converting everything to Cartesian makes it possible to work with them all together.)

SetCoordinates[Cartesian[x, y, z]]

Cartesian[x, y, z]


Created by Mathematica  (April 3, 2007) Valid XHTML 1.1!