Main Content

gradient

Gradient vector of scalar function

Description

example

g = gradient(f,v) finds the gradient vector of the scalar function f with respect to vector v in Cartesian coordinates. The input f is a function of symbolic scalar variables and the vector v specifies the scalar differentiation variables.

g = gradient(f) finds the gradient vector of the scalar function f with respect to a vector constructed from all symbolic scalar variables found in f. The order of variables in this vector is defined by symvar.

gM = gradient(fM,vM) finds the gradient vector of the scalar function fM with respect to vector vM in Cartesian coordinates. The input function fM is a function of symbolic matrix variables and the vector vM is a symbolic matrix variable of size 1-by-N or N-by-1. (since R2021b)

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);
gradient(f,[x,y,z])
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;
g = gradient(f,[x,y])
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),[x y],{X,Y});
G2 = subs(g(2),[x y],{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 symbolic matrix variables 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 show 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))

Input Arguments

collapse all

Scalar function, specified as a symbolic expression or symbolic function that is a function of symbolic scalar variables.

Data Types: sym | symfun

Vector with respect to which you find gradient vector, specified as a symbolic vector. By default, v is a vector constructed from all symbolic scalar variables found in f. The order of variables in this vector is defined by symvar.

If v is a scalar, gradient(f,v) = diff(f,v). If v is an empty symbolic object, such as sym([]), then gradient returns an empty symbolic object.

Data Types: sym

Since R2021b

Scalar function, specified as a symbolic expression that is a function of symbolic matrix variables.

Data Types: symmatrix

Since R2021b

Vector with respect to which you find gradient vector, specified as a symbolic matrix variable of size 1-by-N or N-by-1.

Data Types: symmatrix

Output Arguments

collapse all

Gradient vector, returned as a symbolic expression or symbolic function that is a function of symbolic scalar variables.

Data Types: sym | symfun

Since R2021b

Gradient vector, returned as a symbolic expression that is a function of symbolic matrix variables.

Data Types: symmatrix

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)

Introduced in R2011b