Check numerical derivatives [message #94527] |
Tue, 27 June 2017 03:30  |
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   |
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 #94535 is a reply to message #94534] |
Wed, 28 June 2017 07:46  |
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.)
|
|
|