# Calculating gradients

**URL:** <https://scilab.discourse.group/t/calculating-gradients/420>\
**Category:** General usage\
**Created:** [March 2, 2024, 7:20pm UTC](https://scilab.discourse.group/t/calculating-gradients/420 "2024-03-02T19:20:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cfuttrup](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cfuttrup](https://scilab.discourse.group/u/cfuttrup)\
**Post date:** [March 2, 2024, 7:20pm UTC](https://scilab.discourse.group/t/calculating-gradients/420/1 "2024-03-02T19:20:33Z")

</div>

Hi

I am wondering if Scilab has a built-in function similar to Python numpy.gradient, which takes an ‘edge-order’ … I can manually create a first-order gradient function in Scilab like this:

```auto
function [grad]=dydx(y, x)
    k = length(x);
    grad = zeros(k); // initialize
    grad(1) = (y(2) - y(1))./(x(2) - x(1));
    grad(k) = (y(k) - y(k-1))./(x(k) - x(k-1));
    if k>2 then
      // for i = 2:k-1 do
      // grad(i) = ( (y(i+1) - y(i))./(x(i+1) - x(i)) + ..
      // (y(i) - y(i-1))./(x(i) - x(i-1)) ) / 2;
      // end
        grad(2:k-1) = ( (y(3:k) - y(2:k-1))./(x(3:k) - x(2:k-1)) + ..
                        (y(2:k-1) - y(1:k-2))./(x(2:k-1) - x(1:k-2)) ) / 2;
    end
endfunction

```

What I am asking for is a more advanced gradient calculation with Scilab.

With kind regards,  
Claus

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [March 4, 2024, 7:57am UTC](https://scilab.discourse.group/t/calculating-gradients/420/2 "2024-03-04T07:57:31Z")

</div>

Hello,

The output of splin: [splin - Cubic spline interpolation](https://help.scilab.org/docs/2024.0.0/en_US/splin.html) in “fast” mode will give you the same output as numpy.gradient when edge\_order=2:

_“fast”_  
_in this case a sub-spline is also computed by using a simple local scheme for the di : d(i) is the derivative at x(i) of the interpolation polynomial of (x(i-1),y(i-1)), (x(i),y(i)),(x(i+1),y(i+1)), except for the end points (d1 being computed from the 3 left most points and dn from the 3 right most points)._

```python
>>> import numpy as np
>>> x = np.array([0, 2, 4, 5, 10], dtype=float)
>>> y = np.array([1, 2, 4, 7, 11], dtype=float)
>>> 
>>> np.gradient(y,x,edge_order=2)
array([0.25 , 0.75 , 2.33333333, 2.63333333, -1.03333333])

```

```scilab
--> splin([0, 2, 4, 5, 10],[1, 2, 4, 7, 11],"fast")
 ans =

   0.25 0.75 2.3333333 2.6333333 -1.0333333

```

S.

---

<div class="post-metadata">

**Author:** ![cfuttrup](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cfuttrup](https://scilab.discourse.group/u/cfuttrup)\
**Post date:** [March 4, 2024, 7:09pm UTC](https://scilab.discourse.group/t/calculating-gradients/420/3 "2024-03-04T19:09:04Z")

</div>

Hi Stéphane

The Python docs says: The gradient is computed using second order accurate central differences in the interior points  
and it follows: [https://www.ams.org/journals/mcom/1988-51-184/S0025-5718-1988-0935077-0/S0025-5718-1988-0935077-0.pdf](https://www.ams.org/journals/mcom/1988-51-184/S0025-5718-1988-0935077-0/S0025-5718-1988-0935077-0.pdf)  
It is surprising to me to see this correlation with cubic spline interpolation. Simple, when you know…

Interesting! Thank you.

With kind regards,  
Claus

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [March 5, 2024, 7:40am UTC](https://scilab.discourse.group/t/calculating-gradients/420/4 "2024-03-05T07:40:19Z")

</div>

> [@cfuttrup](#):
>
> It is surprising to me to see this correlation with cubic spline interpolation

In “fast” mode the spline is not cubic but quadratic, hence is only C\_1 (the first derivative of the spline is continuous) instead of C\_2 (second derivative is continuous) in the cubic case. There is a connection between interpolation and numerical derivative/integration because approximate derivation/integration formulas always amounts to first consider an interpolation polynomial using some of your data points then derivate/integrate the polynomial.

S.

---

<div class="post-metadata">

**Author:** ![cfuttrup](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cfuttrup](https://scilab.discourse.group/u/cfuttrup)\
**Post date:** [August 12, 2024, 4:30pm UTC](https://scilab.discourse.group/t/calculating-gradients/420/5 "2024-08-12T16:30:56Z")

</div>

Hi

Since this post was referenced elsewhere, let me improve a bit on the dydx function, to have a bit more fail safe operation = a bit more input check:

```auto
function [grad]=dydx(y, x)
    k = length(x);
    grad = zeros(k); // initialize
    if k>=2 then // gradient calculation needs a minimum of two points
        grad(1) = (y(2) - y(1))./(x(2) - x(1));
        grad(k) = (y(k) - y(k-1))./(x(k) - x(k-1));
        if k>2 then
      // for i = 2:k-1 do
      // grad(i) = ( (y(i+1) - y(i))./(x(i+1) - x(i)) + ..
      // (y(i) - y(i-1))./(x(i) - x(i-1)) ) / 2;
      // end
            grad(2:k-1) = ( (y(3:k) - y(2:k-1))./(x(3:k) - x(2:k-1)) + ..
                        (y(2:k-1) - y(1:k-2))./(x(2:k-1) - x(1:k-2)) ) / 2;
        end
    end
endfunction

```

Also, although the idea by Stephane Mottelet that a spline can work, I have found out that it is not the same as the numpy.gradient edge\_order=2. The numpy.gradient implementation is somewhat more generic and the implementation is based on compact finite difference:

> **[Compact finite difference](https://en.wikipedia.org/wiki/Compact_finite_difference)**
>
> The compact finite difference formulation, or Hermitian formulation, is a numerical method to compute finite difference approximations. Such approximations tend to be more accurate for their stencil size (i.e. their compactness) and, for hyperbolic problems, have favorable dispersive error and dissipative error properties when compared to explicit schemes. A disadvantage is that compact schemes are implicit and require to solve a diagonal matrix system for the evaluation of interpolations or d Th...

In general you must solve a system of equation to evaluate the compact scheme. In other words, they are implicitly defined. These schemes are used in computational fluid dynamics because of certain desirable numerical advection properties.

Cheers,  
Claus
