comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Check numerical derivatives
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Check numerical derivatives [message #94527] Tue, 27 June 2017 03:30 Go to next message
geo85c is currently offline  geo85c
Messages: 4
Registered: June 2017
Junior Member
Hi there,

I wanted to calculate numerical derivatives of a function. I would like to check if the following are correct.

Consider the function F in R^2.
F is a 2D image.
I did the following:
dims = size(F, /dimensions)
nx = dims[0]
ny = dims[1]
xc = findgen(nx)
yc = findgen(ny)

Derivative with respect to x:
F_x = F[xc[1:nx-1],*]-F[xc[0:nx-2],*] ;first order deriv
F_xx = F_x[xc[1:nx-1],*]-F[xc[0:nx-2],*] ;second order deriv
F_y = F[*,yc[1:ny-1]] - F[*,yc[0:ny-2]]
F_xy = F_x[*,yc[1:ny-1]] - F_x[*,yc[0:ny-2]]

Then I wanted to differentiate F_xy with respect to x. I did the following:
F_xxy = F_xy[xind[1:nx-1],*] - F_xy[xind[0:nx-2],*]

Are the above correct?

Thanks.
Re: Check numerical derivatives [message #94529 is a reply to message #94527] Tue, 27 June 2017 07:52 Go to previous messageGo to next message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
Yes, that looks correct to compute left-sided difference derivatives. But a few comments:

1. You don't need the xc,yc vectors, just use the function directly

F_x = F[1:nx-1,*]-F[0:nx-2,*]

2. You do not compute derivatives for the first column. Be careful because your derivative array has one less column than the original function.

3. Unless you need high compute speed, I would instead use the DERIV() function to obtain centered derivatives, with special treatment of the endpoints

F_x = fltarr(nx,ny)
for i=0,nx-1 do F_x[0,i] = $
DERIV(F[*,i])

--Wayne

On Tuesday, June 27, 2017 at 6:30:59 AM UTC-4, geo...@gmail.com wrote:
> Hi there,
>
> I wanted to calculate numerical derivatives of a function. I would like to check if the following are correct.
>
> Consider the function F in R^2.
> F is a 2D image.
> I did the following:
> dims = size(F, /dimensions)
> nx = dims[0]
> ny = dims[1]
> xc = findgen(nx)
> yc = findgen(ny)
>
> Derivative with respect to x:
> F_x = F[xc[1:nx-1],*]-F[xc[0:nx-2],*] ;first order deriv
> F_xx = F_x[xc[1:nx-1],*]-F[xc[0:nx-2],*] ;second order deriv
> F_y = F[*,yc[1:ny-1]] - F[*,yc[0:ny-2]]
> F_xy = F_x[*,yc[1:ny-1]] - F_x[*,yc[0:ny-2]]
>
> Then I wanted to differentiate F_xy with respect to x. I did the following:
> F_xxy = F_xy[xind[1:nx-1],*] - F_xy[xind[0:nx-2],*]
>
> Are the above correct?
>
> Thanks.
Re: Check numerical derivatives [message #94534 is a reply to message #94529] Wed, 28 June 2017 01:23 Go to previous messageGo to next message
geo85c is currently offline  geo85c
Messages: 4
Registered: June 2017
Junior Member
Speed matters that's why I implemented it this way.
Thank you for your comments.

Your comment 2 was interesting. Is any other way to make it calculate the derivatives of all columns tho?
Re: Check numerical derivatives [message #94535 is a reply to message #94534] Wed, 28 June 2017 07:46 Go to previous message
wlandsman is currently offline  wlandsman
Messages: 743
Registered: June 2000
Senior Member
On Wednesday, June 28, 2017 at 4:23:45 AM UTC-4, geo...@gmail.com wrote:

> Your comment 2 was interesting. Is any other way to make it calculate the derivatives of all columns tho?

Well, you can't take the left-sided derivative of the first column because there is no column to the left. But it might be useful to keep the derivative array the same size as the original array. One could use the right-sided derivative for the first column, which would be the same as the left-sided derivative of the second column. --Wayne

F_x = F[1:nx-1,*]-F[0:nx-2,*]
F_x = [F_x[0,*],F_x]

(Of course the three point Lagrangian interpolation of the DERIV() function would be preferable.)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Removing specific elements in an array
Next Topic: 2D histogram color plot

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 07:16:40 PDT 2025

Total time taken to generate the page: 0.00437 seconds