|
27 | 27 | ## @deftypefn {} {@var{dx} =} gradient (@var{m})
|
28 | 28 | ## @deftypefnx {} {[@var{dx}, @var{dy}, @var{dz}, @dots{}] =} gradient (@var{m})
|
29 | 29 | ## @deftypefnx {} {[@dots{}] =} gradient (@var{m}, @var{s})
|
30 |
| -## @deftypefnx {} {[@dots{}] =} gradient (@var{m}, @var{x}, @var{y}, @var{z}, @dots{}) |
| 30 | +## @deftypefnx {} {[@dots{}] =} gradient (@var{m}, @var{sx}, @var{sy}, @var{sz}, @dots{}) |
31 | 31 | ## @deftypefnx {} {[@dots{}] =} gradient (@var{f}, @var{x0})
|
32 | 32 | ## @deftypefnx {} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{s})
|
33 |
| -## @deftypefnx {} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{x}, @var{y}, @dots{}) |
| 33 | +## @deftypefnx {} {[@dots{}] =} gradient (@var{f}, @var{x0}, @var{sx}, @var{sy}, @dots{}) |
34 | 34 | ##
|
35 | 35 | ## Calculate the gradient of sampled data or a function.
|
| 36 | +## @tex |
| 37 | +## $$ |
| 38 | +## {\rm grad} \ F(x,y,z) \equiv \nabla F = \frac{\partial F_x}{\partial x} \hat{i} + \frac{\partial F_y}{\partial y} \hat{j} + \frac{\partial F_z}{\partial z} \hat{k} |
| 39 | +## $$ |
| 40 | +## @end tex |
| 41 | +## @ifnottex |
36 | 42 | ##
|
37 |
| -## If @var{m} is a vector, calculate the one-dimensional gradient of @var{m}. |
38 |
| -## If @var{m} is a matrix the gradient is calculated for each dimension. |
| 43 | +## @group |
| 44 | +## @verbatim |
| 45 | +## d d d |
| 46 | +## grad F(x,y,z) = -- F(x,y,z) i + -- F(x,y,z) j + -- F(x,y,z) k |
| 47 | +## dx dy dz |
| 48 | +## @end verbatim |
| 49 | +## @end group |
39 | 50 | ##
|
40 |
| -## @code{[@var{dx}, @var{dy}] = gradient (@var{m})} calculates the |
41 |
| -## one-dimensional gradient for @var{x} and @var{y} direction if @var{m} is a |
42 |
| -## matrix. Additional return arguments can be use for multi-dimensional |
43 |
| -## matrices. |
| 51 | +## @end ifnottex |
44 | 52 | ##
|
45 |
| -## A constant spacing between two points can be provided by the @var{s} |
46 |
| -## parameter. If @var{s} is a scalar, it is assumed to be the spacing for all |
47 |
| -## dimensions. Otherwise, separate values of the spacing can be supplied by |
48 |
| -## the @var{x}, @dots{} arguments. Scalar values specify an equidistant |
49 |
| -## spacing. Vector values for the @var{x}, @dots{} arguments specify the |
50 |
| -## coordinate for that dimension. The length must match their respective |
51 |
| -## dimension of @var{m}. |
| 53 | +## If @var{m} is a vector, calculate the one-dimensional numerical gradient of |
| 54 | +## @var{m}. If @var{m} is a matrix the gradient is calculated for each |
| 55 | +## dimension. The return argument(s) are the estimated partial derivatives |
| 56 | +## for each dimension at the specified sample points. |
52 | 57 | ##
|
53 |
| -## At boundary points a linear extrapolation is applied. Interior points |
54 |
| -## are calculated with the first approximation of the numerical gradient |
| 58 | +## The default spacing of between data points is 1. A constant spacing between |
| 59 | +## points can be specified with the @var{s} parameter. If @var{s} is a scalar, |
| 60 | +## the single spacing value is used for all dimensions. Otherwise, separate |
| 61 | +## values of the spacing can be supplied by the @var{sx}, @dots{} arguments. |
| 62 | +## Scalar values specify an equidistant spacing. Vector values for the |
| 63 | +## @var{sx}, @dots{} arguments specify the coordinate for that dimension. The |
| 64 | +## length must match the respective dimension of @var{m}. |
| 65 | +## |
| 66 | +## If the first argument @var{f} is a function handle, the gradient of the |
| 67 | +## function is calculated for the points in @var{x0}. As with sampled data, |
| 68 | +## the spacing values between the points from which the gradient is estimated |
| 69 | +## can be set via the @var{s} or @var{dx}, @var{dy}, @dots{} arguments. By |
| 70 | +## default a spacing of 1 is used, however this is normally overly large unless |
| 71 | +## the function is very slowly varying, and it is often necessary to specify a |
| 72 | +## smaller sample spacing. |
| 73 | +## |
| 74 | +## Example: numerical gradient of @code{cos} (analytically = @code{-sin}) |
55 | 75 | ##
|
56 | 76 | ## @example
|
57 |
| -## y'(i) = 1/(x(i+1)-x(i-1)) * (y(i-1)-y(i+1)). |
| 77 | +## @group |
| 78 | +## gradient (@@cos, pi/2, .1) |
| 79 | +## @result{} -0.9983 |
| 80 | +## -sin (pi/2) |
| 81 | +## @result{} -1 |
| 82 | +## @end group |
| 83 | +## @end example |
| 84 | +## |
| 85 | +## Programming Notes: |
| 86 | +## The value for interior data points is approximated using the central |
| 87 | +## difference. |
| 88 | +## |
| 89 | +## @example |
| 90 | +## y'(i) = 1/2 * (y(i+1) - y(i-1)). |
| 91 | +## @end example |
| 92 | +## |
| 93 | +## At boundary points a linear extrapolation is applied. |
| 94 | +## |
| 95 | +## @example |
| 96 | +## y'(1) = y(2) - y(1). |
58 | 97 | ## @end example
|
59 | 98 | ##
|
60 |
| -## If the first argument @var{f} is a function handle, the gradient of the |
61 |
| -## function at the points in @var{x0} is approximated using central difference. |
62 |
| -## For example, @code{gradient (@@cos, 0)} approximates the gradient of the |
63 |
| -## cosine function in the point @math{x0 = 0}. As with sampled data, the |
64 |
| -## spacing values between the points from which the gradient is estimated can |
65 |
| -## be set via the @var{s} or @var{dx}, @var{dy}, @dots{} arguments. By default |
66 |
| -## a spacing of 1 is used. |
67 | 99 | ## @seealso{diff, del2}
|
68 | 100 | ## @end deftypefn
|
69 | 101 |
|
|
0 commit comments