Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2026.0.0 - English


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:

  • a scalar: uniform step size in all direction.
  • a vector: individual step sizes per dimension.

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

  • diff — Difference and discrete derivative
  • diffxy — derivative of y with respect to x
  • numderivative — approximate derivatives of a function (Jacobian or Hessian)

History

VersionDescription
2026.0.0 Function added.
Report an issue
<< diffxy Differential Equations int2d >>

Copyright (c) 2022-2025 (Dassault Systèmes S.E.)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 16 09:02:30 CEST 2025