<< diffxy Equations différentielles int2d >>

Scilab Help >> Equations différentielles > gradient

gradient

compute numerical gradient

Syntax

gx = gradient(f)
[gx, gy] = gradient(f)
[gx, gy, ..., gn] = gradient(f)

gx = gradient(f, h)
[gx, gy, ..., gn] = gradient(f, h)
[gx, gy, ..., gn] = gradient(f, hx, hy, ..., hn)

Arguments

f

matrix of doubles, representing sampled data

h, hx, hy, ..., hn

real scalar or vector, corresponding to step size(s) along each dimension. Default is 1.

gx, gy, ..., gn

matrix of doubles, corresponding to the gradient of f with respect to each dimension. Each matrix has the same size as f.

Description

The gradient function computes the partial derivatives of a matrix f using central differences for interior points and first-order differences at the boudaries.

gx = gradient(f) returns the gradient along rows, i.e x-direction. The step between each point of f is equal to 1.

[gx, gy] = gradient(f) returns two matrices: gx gradient along rows, i.e x-direction and gy, gradient along columns y-direction. The step between each point of f is equal to 1.

[gx, gy, ..., gn] = gradient(f) returns n matrices if f is a n-dimensional matrix.

If h is provided, it scales the differences accordingly. It can be:

Examples

2D gradient

f = [1 2 4; 
    3 6 9; 
    0 5 8];
[gx, gy] = gradient(f)

Gradient with step size

f = [1 2 4; 
    3 6 9; 
    0 5 8];
[gx, gy] = gradient(f, 0.5) // assumes 0.5 spacing in both directions

[x, y] = meshgrid(-2:0.5:2, -2:0.5:2);
f = x.^2 + y.^2;

[gx, gy] = gradient(f, 0.5);

// at point x0 = 1 and y0 = 1;
t = x == 1 & y == -1
// exact value of the gradient at the point (1,-1) is [2 -2]
[gx(t), gy(t)]

See also

History

VersionDescription
2026.0.0 Function added.

Report an issue
<< diffxy Equations différentielles int2d >>