[Numpy-discussion] Second order gradient in numpy

RayS rays at blue-cove.com
Sat May 3 09:48:19 EDT 2014


I recently tried diff and gradient for some 
medical time domain data, and the result nearly looked like pure noise.

I just found this after seeing John Agosta's post
https://gist.github.com/mblondel/487187

"""
Find the solution for the second order differential equation

u'' = -u

with u(0) = 10 and u'(0) = -5

using the Euler and the Runge-Kutta methods.
This works by splitting the problem into 2 first order differential equations

u' = v
v' = f(t,u)

with u(0) = 10 and v(0) = -5
"""
- Ray

At 12:19 PM 5/2/2014, you wrote:
>On Thu, May 1, 2014 at 6:00 PM, Yuxiang Wang 
><<mailto:yw5aj at virginia.edu>yw5aj at virginia.edu> wrote:
>Thank you for your input! I prefer np.gradient because it takes
>mid-point finite difference estimation instead of one-sided estimates,
>but np.diff() is also a good idea. Just wondering why np.gradient does
>not have something similar, being curious :)
>
>
>well, according to the docs, the second order 
>diff() is just calling diff twice anyway, so 
>really the same as what you've done with 
>gradient anyway. I suspect it's just that no one 
>bothered to add that to the gradient API.
>
>BTW, I think that numy can handle piecewise 
>polynomials (i.e. splines), so depending on the 
>noisiness of your data, a cubic spline fit may 
>give better gradients -- and if your data are 
>noise second order gradients can get *really* noisy
>
>-CHB
>
>
>
>Â
>Shawn
>
>On Thu, May 1, 2014 at 6:42 PM, Christian K. 
><<mailto:ckkart at hoc.net>ckkart at hoc.net> wrote:
> > Am 01.05.14 18:45, schrieb Yuxiang Wang:
> >> Hi all,
> >>
> >> I am trying to calculate the 2nd-order gradient numerically of an
> >> array in numpy.
> >>
> >> Â  Â  Â import numpy as np
> >> Â  Â  Â a = np.sin(np.arange(0, 10, .01))
> >> Â  Â  Â da = np.gradient(a)
> >> Â  Â  Â dda = np.gradient(da)
> >
> > It looks like you are looking for the derivative rather than the
> > gradient. Have a look at:
> >
> > np.diff(a, n=1, axis=-1)
> >
> > n is the order if the derivative.
> >
> > Christian
> >
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > <mailto:NumPy-Discussion at scipy.org>NumPy-Discussion at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
>
>--
>Yuxiang "Shawn" Wang
>Gerling Research Lab
>University of Virginia
><mailto:yw5aj at virginia.edu>yw5aj at virginia.edu
><tel:%2B1%20%28434%29%20284-0836>+1 (434) 284-0836
>https://sites.google.com/a/virginia.edu/yw5aj/
>_______________________________________________
>NumPy-Discussion mailing list
><mailto:NumPy-Discussion at scipy.org>NumPy-Discussion at scipy.org
>http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
>
>
>--
>
>Christopher Barker, Ph.D.
>Oceanographer
>
>Emergency Response Division
>NOAA/NOS/OR&R Â  Â  Â  Â  Â  Â (206) 526-6959Â Â  voice
>7600 Sand Point Way NE Â Â (206) 526-6329Â Â  fax
>Seattle, WA Â 98115 Â  Â  Â Â (206) 526-6317Â Â  main reception
>
><mailto:Chris.Barker at noaa.gov>Chris.Barker at noaa.gov
>_______________________________________________
>NumPy-Discussion mailing list
>NumPy-Discussion at scipy.org
>http://mail.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140503/67930087/attachment.html>


More information about the NumPy-Discussion mailing list