Gradient vector of symbolic scalar field

collapse all in page

## Syntax

`g = gradient(f,v)`

`g = gradient(f)`

## Description

example

returns the gradient vector of symbolic scalar field `g`

= gradient(f,v)`f`

with respect to vector `v`

in Cartesian coordinates.

returns the gradient vector of the scalar field `g`

= gradient(f)`f`

with respect to a default vector constructed from the symbolic variables in `f`

.

## Examples

collapse all

### Find Gradient of Function

Open Live Script

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 zf(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) =$$\left(\begin{array}{c}3\hspace{0.17em}\mathrm{cos}\left(y\right)\hspace{0.17em}\mathrm{sin}\left(z\right)+2\hspace{0.17em}y\hspace{0.17em}z\hspace{0.17em}\mathrm{cos}\left(x\right)\\ 2\hspace{0.17em}z\hspace{0.17em}\mathrm{sin}\left(x\right)-3\hspace{0.17em}x\hspace{0.17em}\mathrm{sin}\left(y\right)\hspace{0.17em}\mathrm{sin}\left(z\right)\\ 2\hspace{0.17em}y\hspace{0.17em}\mathrm{sin}\left(x\right)+3\hspace{0.17em}x\hspace{0.17em}\mathrm{cos}\left(y\right)\hspace{0.17em}\mathrm{cos}\left(z\right)\end{array}\right)$$

### Plot Gradient of Function

Open Live Script

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 yf = -(sin(x) + sin(y))^2;v = [x y];g = gradient(f,v)

g =$$\left(\begin{array}{c}-2\hspace{0.17em}\mathrm{cos}\left(x\right)\hspace{0.17em}\left(\mathrm{sin}\left(x\right)+\mathrm{sin}\left(y\right)\right)\\ -2\hspace{0.17em}\mathrm{cos}\left(y\right)\hspace{0.17em}\left(\mathrm{sin}\left(x\right)+\mathrm{sin}\left(y\right)\right)\end{array}\right)$$

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)

### Gradient of Matrix Multiplication

*Since R2021b*

Open Live Script

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

syms X Y [3 1] matrixA = Y.'*X

`A =$${Y}^{\mathrm{T}}\hspace{0.17em}X$$`

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$$`

### Gradient of Multivariable Function

*Since R2021b*

Open Live Script

Find the gradient of the multivariable function

$$f(x)={\mathrm{sin}}^{2}({x}_{1,1})+{\mathrm{sin}}^{2}({x}_{1,2})+{\mathrm{sin}}^{2}({x}_{1,3})$$

with respect to the vector $$x=[{x}_{1,1},{x}_{1,2},{x}_{1,3}]$$.

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

syms x [1 3] matrixf = sin(x)*sin(x).'

`f =$$\mathrm{sin}\left(x\right)\hspace{0.17em}{\mathrm{sin}\left(x\right)}^{\mathrm{T}}$$`

g = gradient(f,x)

`g =$$2\hspace{0.17em}\left(\mathrm{cos}\left(x\right)\odot {\mathrm{I}}_{3}\right)\hspace{0.17em}{\mathrm{sin}\left(x\right)}^{\mathrm{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 =$$\left(\begin{array}{c}2\hspace{0.17em}\mathrm{cos}\left({x}_{1,1}\right)\hspace{0.17em}\mathrm{sin}\left({x}_{1,1}\right)\\ 2\hspace{0.17em}\mathrm{cos}\left({x}_{1,2}\right)\hspace{0.17em}\mathrm{sin}\left({x}_{1,2}\right)\\ 2\hspace{0.17em}\mathrm{cos}\left({x}_{1,3}\right)\hspace{0.17em}\mathrm{sin}\left({x}_{1,3}\right)\end{array}\right)$$

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 =$$\left(\begin{array}{c}2\hspace{0.17em}\mathrm{cos}\left({x}_{1,1}\right)\hspace{0.17em}\mathrm{sin}\left({x}_{1,1}\right)\\ 2\hspace{0.17em}\mathrm{cos}\left({x}_{1,2}\right)\hspace{0.17em}\mathrm{sin}\left({x}_{1,2}\right)\\ 2\hspace{0.17em}\mathrm{cos}\left({x}_{1,3}\right)\hspace{0.17em}\mathrm{sin}\left({x}_{1,3}\right)\end{array}\right)$$

### Find Vector Calculus Identities Involving Gradients

*Since R2023a*

Open Live Script

Create a 3-by-1 vector as a symbolic matrix variable $\mathit{X}$. Create a scalar field that is a function of $\mathit{X}$ as a symbolic matrix function $A\left(\mathit{X}\right)$, keeping the existing definition of $\mathit{X}$.

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

Find the gradient of $A\left(\mathit{X}\right)$ with respect to $\mathit{X}$. The `gradient`

function returns an unevaluated formula.

gradA = gradient(A,X)

`gradA(X) =$${\nabla}_{X}\mathrm{}A\left(X\right)$$`

Show that the divergence of the gradient of $A\left(\mathit{X}\right)$ is equal to the Laplacian of $A\left(\mathit{X}\right)$, that is ${\nabla}_{\mathit{X}}\cdot {\nabla}_{\mathit{X}}\mathit{A}\left(\mathit{X}\right)={\Delta}_{\mathit{X}}\mathit{A}\left(\mathit{X}\right)$.

divOfGradA = divergence(gradA,X)

`divOfGradA(X) =$${\Delta}_{X}\mathrm{}A\left(X\right)$$`

lapA = laplacian(A,X)

`lapA(X) =$${\Delta}_{X}\mathrm{}A\left(X\right)$$`

Show that the curl of the gradient of $A\left(\mathit{X}\right)$ is zero, that is ${\nabla}_{\mathit{X}}\times \text{\hspace{0.17em}}{\nabla}_{\mathit{X}}\mathit{A}\left(\mathit{X}\right)={0}_{3,1}$.

curlOfGradA = curl(gradA,X)

`curlOfGradA(X) =$${\mathrm{0}}_{3,1}$$`

## Input Arguments

collapse all

`f`

— Symbolic scalar field

symbolic expression | symbolic function | symbolic matrix variable | symbolic matrix function

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`

`v`

— Vector with respect to which you find the gradient

vector of symbolic scalar variables | symbolic function | symbolic matrix variable | symbolic matrix function

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=\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)$$ is the vector of the first partial derivatives of

*f*.

$${\nabla}_{x}f(x)=\left(\frac{\partial f}{\partial {x}_{1}},\frac{\partial f}{\partial {x}_{2}},\dots ,\frac{\partial f}{\partial {x}_{n}}\right)$$

## Version History

**Introduced in R2011b**

expand all

### R2023a: Compute gradient of symbolic matrix functions

The `gradient`

function accepts symbolic matrix functions of type `symfunmatrix`

as input arguments. For example, see Find Vector Calculus Identities Involving Gradients.

### R2021b: Compute gradient of symbolic matrix variables

The `gradient`

function accepts symbolic matrix variables of type `symmatrix`

as input arguments. For examples, see Gradient of Matrix Multiplication and Gradient of Multivariable Function.

## See Also

curl | divergence | diff | hessian | jacobian | laplacian | potential | quiver | vectorPotential

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom (English)

Contact your local office