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

Home » Public Forums » archive » Gradient of two dimensional field
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Return to the default flat view Create a new topic Submit Reply
Re: Gradient of two dimensional field [message #8359 is a reply to message #8200] Mon, 03 March 1997 00:00 Go to previous message
Andy Loughe is currently offline  Andy Loughe
Messages: 174
Registered: November 1995
Senior Member
Wilpert_Martin wrote:
>
> Hi everybody,
>
> we want to determine the electrical field from a given potential,
> i.e. we have to calculate the gradient of a two dimensional array.
>
> Has anybody a idl-pvwave procedure to do this task?
>
> Thank you in advance
> Martin Wilpert

The gradient of a 2-D field is a vector field, this function computes
the
i,j vector components of the gradient, and returns the *magnitude* of
that vector field. It performs one-sided differences at the lateral
boundaries, which is an assumption that your field does not have a
cyclical boundary condition. Let me know if it works for you!


; Compute the magnitude of the vector gradient.
;
; Andrew F. Loughe (afl@cdc.noaa.gov)
;

function grad, data, x, y

sz = size(data) & im = sz(1) & jm = sz(2)

if ( N_params() eq 0 ) then message, ' grad_data = grad(data, x, y)'
if ( sz(0) ne 2 ) then message, 'Input data must be 2-D'
if ( N_elements(x) eq 0) then x = indgen(im) + 1
if ( N_elements(y) eq 0) then y = jm - indgen(jm)

; Begin here

x2 = x # replicate(1, jm)
y2 = replicate(1,jm) # y

; i-component
if (x(1) gt x(0)) then dx = shift(x2, -1, 0) - shift(x2, 1, 0)
if (x(1) lt x(0)) then dx = shift(x2, 1, 0) - shift(x2, -1, 0)
grad_x = ( shift(data, -1, 0) - shift(data, 1, 0) ) / dx


; j-component
if (y(1) lt y(0)) then dy = shift(y2, 0, 1) - shift(y2, 0, -1)
if (y(1) gt y(0)) then dy = shift(y2, 0, -1) - shift(y2, 0, 1)
grad_y = ( shift(data, 0, 1) - shift(data, 0, -1) ) / dy


; But for non-cyclic boundary values we still have a problem...
; Take care of the outter rows and columns
grad_y(*,jm-1) = ( data(*,jm-1) - data(*,jm-2) ) / $
( y2(*,jm-1) - y2(*,jm-2) )
grad_y(*,0) = ( data(*,1) - data(*,0) ) / ( y2(*,1) - y2(*,0) )


grad_x(im-1,*) = ( data(im-1,*) - data(im-2,*) ) / $
( x2(im-1,*) - x2(im-2,*) )
grad_x(0,*) = ( data(1,*) - data(0,*) ) / ( x2(1,*) - x2(0,*) )


grad = sqrt(grad_x^2 + grad_y^2)


return, grad

end



--
Andrew F. Loughe |
afl@cdc.noaa.gov
University of Colorado, CIRES Box 449 |
http://cdc.noaa.gov/~afl
Boulder, CO 80309-0449 | phn:(303)492-0707
fax:(303)497-7013
------------------------------------------------------------ ---------------
"I do not feel obliged to believe that the same God who has endowed us
with
sense, reason, and intellect has intended us to forego their use."
-Galileo
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Re: Printing array on one line
Next Topic: using GET_KBRD

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

Current Time: Sat Oct 11 11:52:53 PDT 2025

Total time taken to generate the page: 0.88260 seconds