({{, (RefLink[Grad, paclet:ref/Grad])[f,{(Subscript[x, 1]),…,(Subscript[x, n])}]
gives the gradient (∂f/∂(Subscript[x, 1]),…,∂f/∂(Subscript[x, n])).}, {, (RefLink[Grad, paclet:ref/Grad])[f,{(Subscript[x, 1]),…,(Subscript[x, n])},chart]
gives the gradient in the coordinates chart.}})

OpenersNotesSection(Spacer1)Details

(RefLink[Grad, paclet:ref/Grad])[f,x] can be input as (Subscript[∇, x])f. The character ∇ can be typed as (Key0[esc])del(Key0[esc]) or (RefLink[\[Del], paclet:ref/character/Del]). The list of variables x is entered as a subscript.

An empty template (Subscript[∇, ■])□ can be entered as (Key0[esc])grad(Key0[esc]), and (Key0[tab]) moves the cursor from the subscript to the main body.

All quantities that do not explicitly depend on the variables given are taken to have zero partial derivative.

If f is an array of dimensions {(Subscript[n, 1]),…,(Subscript[n, k])}, then (RefLink[Grad, paclet:ref/Grad])[f,{(Subscript[x, 1]),…,(Subscript[x, m])}] yields an array of dimensions {(Subscript[n, 1]),…,(Subscript[n, k]),m}.

If f is a scalar, (RefLink[Grad, paclet:ref/Grad])[f,{(Subscript[x, 1]),(Subscript[x, 2]),…,(Subscript[x, n])},chart] returns a vector in the orthonormal basis associated with chart.

In (RefLink[Grad, paclet:ref/Grad])[f,{(Subscript[x, 1]),…,(Subscript[x, n])},chart], if f is an array, it must have dimensions {n,…,n}. The components of f are interpreted as being in the orthonormal basis associated with chart.

Coordinate charts in the third argument of (RefLink[Grad, paclet:ref/Grad]) can be specified as triples {coordsys,metric,dim} in the same way as in the first argument of (RefLink[CoordinateChartData, paclet:ref/CoordinateChartData]). The short form in which dim is omitted may be used.

(RefLink[Grad, paclet:ref/Grad]) works with (RefLink[SparseArray, paclet:ref/SparseArray]) and (RefLink[StructuredArray, paclet:ref/StructuredArray]) objects.

OpenersPrimaryExamplesSection(Spacer1)Examples  (21)

OpenersExampleSection0(Spacer1)Basic Examples  (4)

The gradient in three-dimensional Cartesian coordinates:

Grad[f[x,y,z],{x,y,z}]

The gradient using an orthonormal basis for three-dimensional cylindrical coordinates:

Grad[f[r,θ,z],{r,θ,z},"Cylindrical"]

The gradient in two dimensions:

Grad[Sin[x^2+y^2],{x,y}]

Use (Key0[esc]) del (Key0[esc]) to enter ∇ and (Key1[ctrl, -]) to enter the list of subscripted variables:

(Subscript[∇, {x,y}])f[x,y]

Use (Key0[esc]) grad (Key0[esc]) to enter the template (Subscript[∇, □])□; press (Key0[tab]) to move between inputs:

(Subscript[∇, {x,y}])f[x,y]

OpenersExampleSection1(Spacer1)Scope  (7)

The gradient of a vector field in Cartesian coordinates, the Jacobian matrix:

Grad[{f[x,y,z],g[x,y,z],h[x,y,z]},{x,y,z}]

Compute the Hessian of a scalar function:

grad=Grad[x*y*z,{x,y,z}]

Grad[grad,{x,y,z}]

In a curvilinear coordinate system, a vector with constant components may have a nonzero gradient:

Grad[{1,1,1},{r,θ,ϕ},"Spherical"]

Gradient specifying metric, coordinate system, and parameters:

Grad[f[ϕ,ψ],{ϕ,ψ},{{"Confocal",{a,b}},"Euclidean"}]//Simplify

(RefLink[Grad, paclet:ref/Grad]) works on curved spaces:

Grad[f[u,v,w],{u,v,w},{"Stereographic",{"Sphere",r}}]

The gradient of the coordinates with respect to themselves:

Grad[Inactive[Table][(Subscript[x, i]),{i,n}],Inactive[Table][(Subscript[x, i]),{i,n}]]

This is the identity matrix in n dimensions:

Activate[% /. n->3]//MatrixForm

The gradient of the norm in n dimensions:

Grad[Sqrt[Sum[((((Subscript[x, i])))^2),{i,n}]],Inactive[Table][(Subscript[x, j]),{j,n}]]

Specialize to the result in three dimensions:

Activate[% /. n->3]

OpenersExampleSection3(Spacer1)Applications  (4)

Compute the force from a potential function:

-Grad[k q/r,{r,θ,ϕ},"Spherical"]

Find the critical points of a function of two variables:

f[x_,y_] :=x^4+y^4-20x^2 -10x y-25

grad = Grad[f[x,y],{x,y}]

sol=NSolve[grad=={0,0},{x,y},Reals]

Compute the signs of (((∂^2)f(x,y))/(∂(x^2))) and the Hessian determinant:

hessian=Grad[grad,{x,y}]

Sign[hessian[[1,1]] /. sol]

Sign[Det[hessian] /.sol]

By the second derivative test, the first two points—red and blue in the plot—are minima, and the third—green in the plot—is a saddle point:

Show[Plot3D[f[x,y],{x,-5,5},{y,-5,5},PlotRange->200,ClippingStyle->None,Mesh->{5,0},PlotStyle->Opacity[.65],BoxRatios->{1, 1, 3/4},ViewPoint->{1.2,-2.5,0}],Graphics3D[{PointSize[.04],Riffle[{Red,Blue,Green},Point[{x,y,f[x,y]}]/.sol]}]]

Find the critical points of a function of three variables:

f[x_,y_,z_] :=25-(x^2)+2 (x^4)-x y-(y^2)+4 (x^2) (y^2)+2 (y^4)-2 x z-2 y z-(z^2)+4 (x^2) (z^2)+4 (y^2) (z^2)+2 (z^4)

grad = Grad[f[x,y,z],{x,y,z}]

sol=NSolve[grad=={0,0,0},{x,y,z},Reals]

Compute the Hessian matrix of f:

hessian=Grad[grad,{x,y,z}]

When the eigenvalues of a critical point all have the same sign, the point is a local extremum; if there are both positive and negative values, it is a saddle point:

Sign[Eigenvalues[hessian]/.sol]

Since the third and fourth points have all positive eigenvalues, they are local minima, and the global minimum can be determined by evaluating f at those two points:

f[x,y,z] /. Take[sol,{3,4}]

For this function, any three of the critical points are linearly dependent, so they all lie in a single plane:

Minors[{x,y,z} /. sol,3]

Compute the normal to that plane:

normal =Apply[Cross,{x,y,z} /. sol[[{1,3}]]]

Visualize the function, with the minima green and the non-extreme critical points red:

Show[SliceContourPlot3D[f[x,y,z],{{x,y,z}.normal==0},{x,-1,1},{y,-1,1},{z,-1,1},Contours->15,ViewVertical->{-0.2,0.7,0.6},ViewPoint->{-1.2,-1.2,2.9}],Graphics3D[{Riffle[{Red,Red,Green,Green,Red},Sphere[{x,y,z},0.05]/.sol]}]]

Compute the gradients at the origin of a vector-valued function up to order 6:

f[x_,y_,z_] := {Sin[x z],Sinh[y]+ Cos[ z]}
grads=NestList[Grad[#,{x,y,z}]&,f[x,y,z],6] /. {x->0,y->0,z->0};

View the value of the function and its first three derivatives at the origin:

MatrixForm/@Take[grads,4]

Compute the third-order Maclaurin series of f by hand:

𝕏={x,y,z};
(1/(0!))grads[[1]]+(1/(1!))grads[[2]].𝕏+(1/(2!))grads[[3]].𝕏.𝕏+(1/(3!))grads[[4]].𝕏.𝕏.𝕏

Programmatically compute the sixth-order Maclaurin series:

Sum[(1/((k-1)!))Dot[grads[[k]],##]&@@Table[𝕏,{k-1}] ,{k,7}]

OpenersExampleSection4(Spacer1)Properties & Relations  (4)

(RefLink[Grad, paclet:ref/Grad])[f,vars] is effectively equivalent to (RefLink[D, paclet:ref/D])[f,{vars}]:

Grad[x*y*Sin[z],{x,y,z}]

D[x*y*Sin[z],{{x,y,z}}]

Grad[{x*y,y*z,z*x},{x,y,z}]

D[{x*y,y*z,z*x},{{x,y,z}}]

(RefLink[Grad, paclet:ref/Grad]) adds a new tensor slot at the end, corresponding to a new innermost dimension:

postgrad=Grad[{f1[x,y],f2[x,y]},{x,y}]

To add the new slot at the beginning, use (RefLink[Transpose, paclet:ref/Transpose]) to exchange the first and last slots:

pregrad =Transpose[{{Derivative[1,0][f1][x,y],Derivative[0,1][f1][x,y]},{Derivative[1,0][f2][x,y],Derivative[0,1][f2][x,y]}}]

This is relevant when working with directional derivatives of arrays:

postgrad.{v1,v2}

{v1,v2}. pregrad

The gradient of an array equals the gradient of its components only in Cartesian coordinates:

Grad[{f[x,y],g[x,y]},{x,y},"Cartesian"]==Map[Grad[#,{x,y},"Cartesian"]&,{f[x,y],g[x,y]}]

Grad[{f[x,y],g[x,y]},{x,y},"Polar"]==Map[Grad[#,{x,y},"Polar"]&,{f[x,y],g[x,y]}]//Simplify

(RefLink[Grad, paclet:ref/Grad]) preserves the structure of (RefLink[StructuredArray, paclet:ref/StructuredArray]) objects created by (RefLink[SymmetrizedArray, paclet:ref/SymmetrizedArray]):

sa=SymmetrizedArray[{{1,1,1}->x^2,{1,1,2}->y^2,{2,1,2}->x y},{2,2,2},Symmetric]

The gradient has an additional dimension but the same symmetry as the input:

grad=Grad[sa,{x,y}]

OpenersExampleSection5(Spacer1)Interactive Examples  (2)

The normal vectors to the level contours of a function equal the normalized gradient of the function:

f[x_,y_]:=((x^2)/4)-2x^2y-3x y+y^4/16

grad[x_,y_]:=Grad[f[x,y],{x,y}]

normal[x_,y_]=Simplify[((grad[x,y])/(Sqrt[grad[x,y].grad[x,y]]))]

Create an interactive contour plot that displays the normal at a point:

Manipulate[ContourPlot[f[x,y],{x,-2,2},{y,-2,2}, Epilog->Arrow[{pt,pt+normal@@pt}],PerformanceGoal->"Quality",Contours->20,PlotRange->{{-2,2},{-2,2},{-30,30}},ImageSize->Medium],{{pt,{.01,-0.1}},Locator},FrameLabel->"Click a point to see its normal",SaveDefinitions->True]

View expressions for the gradient of a scalar function in different coordinate systems:

systems= {{"Cartesian", "Polar","Bipolar", "PlanarParabolic"}, {"Cartesian", "Cylindrical","Spherical", "CircularParabolic", "BipolarCylindrical"}, {"Cartesian","Hyperspherical"},{"Cartesian","Hyperspherical"}};

Manipulate[
If[Not@MemberQ[systems[[dimension-1]],system],system="Cartesian"];
DynamicModule[{varNames,vars,vec},
varNames=CoordinateChartData[{system,dimension},"StandardCoordinateNames"];
vars=ToExpression/@ varNames;
Inactive[Grad][f@@vars,vars]==Simplify@Grad[f@@vars,vars,system]],
{dimension,Range[2,5]}, 
{{system,"Cartesian"}, systems[[dimension-1]], ControlType->SetterBar},SaveDefinitions->True]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Div, paclet:ref/Div])  ▪  (RefLink[Curl, paclet:ref/Curl])  ▪  (RefLink[Laplacian, paclet:ref/Laplacian])  ▪  (RefLink[CoordinateChartData, paclet:ref/CoordinateChartData])  ▪  (RefLink[D, paclet:ref/D])  ▪  (RefLink[DSolve, paclet:ref/DSolve])  ▪  (RefLink[NDSolve, paclet:ref/NDSolve])  ▪  (RefLink[NDEigensystem, paclet:ref/NDEigensystem])  ▪  (RefLink[NDEigenvalues, paclet:ref/NDEigenvalues])  ▪  (RefLink[NetPortGradient, paclet:ref/NetPortGradient])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Vector Analysis, paclet:tutorial/VectorAnalysis])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Vector Analysis, paclet:guide/VectorAnalysis])}, {▪(RefLinkPlain[Differential Operators, paclet:guide/DifferentialOperators])}, {▪(RefLinkPlain[Calculus, paclet:guide/Calculus])}, {▪(RefLinkPlain[Partial Differential Equations, paclet:guide/PDEModelingAndAnalysis])}, {▪(RefLinkPlain[Differential Equations, paclet:guide/DifferentialEquations])}})}})