# gradient

## Purpose

Approximate gradient.

## Synopsis

    [PX,PY] = gradient(Z)
    [PX,PY] = gradient(Z,x,y)
    [PX,PY] = gradient(Z,dx,dy)
    p = gradient(Z,...)

    d = gradient(y)
    d = gradient(y,x)
    d = gradient(y,dx)

## Description

`[PX,PY] = gradient(Z)`, with a single matrix argument, computes a numerical approximation to the gradient field of the function tabulated in `Z`. The result is ordinarily two matrices, the same size as `Z`, containing horizontal and vertical first differences. One-sided differences are used at the edges of the matrix and centered differences are used in the interior.

`[PX,PY] = gradient(Z,x,y)`, with one matrix and two vector arguments, uses divided differences involving the vector `x` in the horizontal direction and the vector `y` in the vertical direction.

`[PX,PY] = gradient(Z,dx,dy)`, with one matrix and two scalar arguments, divides the horizontal difference by the scalar `dx` and the vertical difference by the scalar `dy`.

`P = gradient(Z,...)`, with one output argument, returns a complex result, `P = PX + i*PY`.

`d = gradient(y)`, with a single vector argument, computes a numerical approximation to the first derivative of the function tabulated in `y`. The result is a vector, the same size as `y`, containing first differences. One-sided differences are used at the ends of the vector and centered differences are used in the interior.

`d = gradient(y,x)`, with two vector arguments, uses divided differences to approximate the derivative.

`d = gradient(y,dx)`, with one vector and one scalar argument, divides the first difference by the scalar `dx`.

## Examples

The statements

    x = -pi:pi/20:pi;
    y = -1:.05:1;
    [X,Y] = meshgrid(x,y);
    Z = sin(X) + Y.^3;
    [PX,PY] = gradient(Z,x,y);

produce a matrix `PX` approximating the partial derivative with respect to x, which is `cos(X)`, and a matrix `PY` approximating the partial derivative with respect to `y`, which is `3*Y.^2`.

The statements

    x = -pi:pi/500:pi;
    y = tan(sin(x)) - sin(tan(x));
    d = gradient(y,x);

produce a vector which approximates the derivative dy/dx.

## See Also

    contour, del2, diff, quiver

(c) Copyright 1994 by The MathWorks, Inc.