Main Content

gradient

Gradient vector of symbolic scalar field

Description

g = gradient(f,v) returns the gradient vector of symbolic scalar field f with respect to vector v in Cartesian coordinates.

example

g = gradient(f) returns the gradient vector of the scalar field f with respect to a default vector constructed from the symbolic variables in f.

Examples

collapse all

The gradient of a scalar function f with respect to the vector v is the vector of the first partial derivatives of f with respect to each element of v.

Find the gradient vector of f(x,y,z) with respect to vector [x,y,z]. The gradient is a vector with these components.

syms x y z
f(x,y,z) = 2*y*z*sin(x) + 3*x*sin(z)*cos(y);
v = [x,y,z];
gradient(f,v)
ans(x, y, z) = 

(3cos(y)sin(z)+2yzcos(x)2zsin(x)-3xsin(y)sin(z)2ysin(x)+3xcos(y)cos(z))

Find the gradient of a function f(x,y), and plot it as a quiver (velocity) plot.

Find the gradient vector of f(x,y) with respect to vector [x,y]. The gradient is vector g with these components.

syms x y
f = -(sin(x) + sin(y))^2;
v = [x y];
g = gradient(f,v)
g = 

(-2cos(x)sin(x)+sin(y)-2cos(y)sin(x)+sin(y))

Now plot the vector field defined by these components. MATLAB® provides the quiver plotting function for this task. The function does not accept symbolic arguments. First, replace symbolic variables in expressions for components of g with numeric values. Then use quiver.

[X,Y] = meshgrid(-1:.1:1,-1:.1:1);
G1 = subs(g(1),v,{X,Y});
G2 = subs(g(2),v,{X,Y});
quiver(X,Y,G1,G2)

Figure contains an axes object. The axes object contains an object of type quiver.

Since R2021b

Use symbolic matrix variables to define a matrix multiplication that returns a scalar.

syms X Y [3 1] matrix
A = Y.'*X
A = YTX

Find the gradient of the matrix multiplication with respect to X.

gX = gradient(A,X)
gX = Y

Find the gradient of the matrix multiplication with respect to Y.

gY = gradient(A,Y)
gY = X

Since R2021b

Find the gradient of the multivariable function

f(x)=sin2(x1,1)+sin2(x1,2)+sin2(x1,3)

with respect to the vector x=[x1,1,x1,2,x1,3].

Use a symbolic matrix variable to express the function f and its gradient in terms of the vector x.

syms x [1 3] matrix
f = sin(x)*sin(x).'
f = sin(x)sin(x)T
g = gradient(f,x)
g = 2cos(x)I3sin(x)T

To express the gradient in terms of the elements of x, convert the result to a vector of symbolic scalar variables using symmatrix2sym.

g = symmatrix2sym(g)
g = 

(2cos(x1,1)sin(x1,1)2cos(x1,2)sin(x1,2)2cos(x1,3)sin(x1,3))

Alternatively, you can convert f and x to symbolic expressions of scalar variables and use them as inputs to the gradient function.

g = gradient(symmatrix2sym(f),symmatrix2sym(x))
g = 

(2cos(x1,1)sin(x1,1)2cos(x1,2)sin(x1,2)2cos(x1,3)sin(x1,3))

Since R2023a

Create a 3-by-1 vector as a symbolic matrix variable X. Create a scalar field that is a function of X as a symbolic matrix function A(X), keeping the existing definition of X.

syms X [3 1] matrix
syms A(X) [1 1] matrix keepargs

Find the gradient of A(X) with respect to X. The gradient function returns an unevaluated formula.

gradA = gradient(A,X)
gradA(X) = X A(X)

Show that the divergence of the gradient of A(X) is equal to the Laplacian of A(X), that is XXA(X)=ΔXA(X).

divOfGradA = divergence(gradA,X)
divOfGradA(X) = ΔX A(X)
lapA = laplacian(A,X)
lapA(X) = ΔX A(X)

Show that the curl of the gradient of A(X) is zero, that is X×XA(X)=03,1.

curlOfGradA = curl(gradA,X)
curlOfGradA(X) = 03,1

Input Arguments

collapse all

Symbolic scalar field, specified as a symbolic expression, symbolic function, symbolic matrix variable, or symbolic matrix function.

  • If f is a function of symbolic scalar variables, where f is of type sym or symfun, then the vector v must be of type sym or symfun.

  • If f is a function of symbolic matrix variables, where f is of type symmatrix or symfunmatrix, then the vector v must be of type symmatrix or symfunmatrix.

Data Types: sym | symfun | symmatrix | symfunmatrix

Vector with respect to which you find the gradient, specified as a vector of symbolic scalar variables, symbolic function, symbolic matrix variable, or symbolic matrix function.

  • If you do not specify v and f is a function of symbolic scalar variables, then, by default, gradient constructs vector v from the symbolic scalar variables in f with the order of variables as defined by symvar(f).

  • If v is a symbolic matrix variable of type symmatrix, then v must have a size of 1-by-N or N-by-1.

  • If v is scalar, then gradient(f,v) = diff(f,v).

  • If v is an empty symbolic object, such as sym([]), then gradient returns an empty symbolic object.

When finding the gradient of a scalar function f with respect to a row or column vector v, gradient uses the convention of always returning the output as a column vector. For example, if f is a 1-by-1 scalar and v is a 1-by-3 row vector, then gradient(f,v) finds the derivative of f with respect to each element of v and returns the result as a 3-by-1 column vector.

Data Types: sym | symfun | symmatrix | symfunmatrix

Limitations

  • The gradient function does not support tensor derivatives. If the gradient is a tensor field or a matrix rather than a vector, then the gradient function returns an error.

  • Symbolic Math Toolbox™ currently does not support the dot or cross functions for symbolic matrix variables and functions of type symmatrix and symfunmatrix. If vector calculus identities involve dot or cross products, then the toolbox displays those identities in terms of other supported functions instead. To see a list of all the functions that support symbolic matrix variables and functions, use the commands methods symmatrix and methods symfunmatrix.

More About

collapse all

Gradient Vector

The gradient vector of f(x) with respect to the vector x=(x1,x2,,xn) is the vector of the first partial derivatives of f.

xf(x)=(fx1,fx2,,fxn)

Version History

Introduced in R2011b

expand all