[SciPy-user] Numerical gradient approximation on matrix

guillem at torroja.dmt.upm.es guillem at torroja.dmt.upm.es
Fri Jul 29 13:18:54 CDT 2005

Hi all

On Fri, 29 Jul 2005, Alan G Isaac wrote:

> On Fri, 29 Jul 2005, Dimitri D'Or apparently wrote:
> > I have a two-dimensional array from which I wish to
> > compute the gradient (i.e. the slope against the first and
> > second dimension). With Matlab, I can do it easily using
> > the gradient.m function. Is there something similar in
> > Scipy or matplotlib? I've browsed the documentation but
> > couldn't found anything but approximate gradient
> > computations on functions in the optimize module. Nothing
> > about computations on matrices.
> Look at scipy.diff.
> E.g., for the two dimensions
> grad0=scipy.diff(x,axis=0)
> grad1=scipy.diff(x,axis=1)

I had the same problem and I was about to write an equivalent function.
The difference is that gradient.m computes the gradient and keeps the
array's shape:

EDU>> size(meshgrid(-2:.2:2,-2:.2:2))

ans =

    21    21

EDU>> size(gradient(meshgrid(-2:.2:2,-2:.2:2)))

ans =

    21    21

And ready to use it in a quiver plot.
I think that one equivalent function would be very useful in scipy_base

Just my oppinion


guillem at torroja.dmt.upm.es
guillem at peret.dmt.upm.es


More information about the SciPy-user mailing list