• To: mathgroup at smc.vnet.net
• Subject: [mg82640] Re: Gradient of a List
• From: Valeri Astanoff <astanoff at gmail.com>
• Date: Fri, 26 Oct 2007 05:34:45 -0400 (EDT)
• References: <ffn0uv\$60k\$1@smc.vnet.net>

```On 24 oct, 10:49, olalla <operez... at ikasle.ehu.es> wrote:
> Hi everybody,
>
> Does anybody know how can I get the "gradient" of a list ofpoints?
>
> My real problem is:
>
> I have a scalar field previously obtained numerically that for a
> given point (xi,yi) takes a value f(xi,yi). What I want to do is an
> estimation of thegradientof this scalar field BUT I haven't got any
> analytical function that expresses  my field so I can't use the Grad
> function.
>
> How can I solve this using Mathematica?
>
>
> Olalla, Bilbao UPV/EHU

Good day,

Here is a DIY solution using ListInterpolation
(not very fast nor clever, but it works):

In[1]:= gridify[xyz_List?MatrixQ /;Dimensions[xyz][[2]]==3]:=
Module[{xi,yi,nx,ny,t,ff,u,v,z},
xi = xyz[[All,1]]//Union;
yi = xyz[[All,2]]//Union;
nx = Length[xi]; ny = Length[yi];
xmin = Min[xi]; xmax = Max[xi];
ymin = Min[yi]; ymax = Max[yi];
dx = (xmax-xmin)/(nx-1);
dy = (ymax-ymin)/(ny-1);
Print["xmin = ",xmin," xmax = ",xmax," dx = ",dx];
Print["ymin = ",ymin," ymax = ",ymax," dy = ",dy];
z[x_,y_]:=
(t= xyz[[Ordering[xyz,6,(x-#1[[1]])^2+(y-#1[[2]])^2<
(x-#2[[1]])^2+(y-#2[[2]])^2&]]];
ff[u_,v_]=Fit[t,{1,u,v,u^2,v^2,u v},{u,v}];
ff[x,y]);
Table[{{x,y}, z[x,y]} ,{x,xmin,xmax,dx},{y,ymin,ymax,dy}]
];

Toy example with 2x^2+3y^2 as test function with 100 points:

In[2]:= data=Table[{xr=RandomReal[],yr=RandomReal[],2xr^2+3yr^2},
{100}];
data //Short

Out[3]//Short= {{0.00503481,0.14094,0.0596427},<<98>>,
{0.638564,0.679141,2.19923}}

In[4]:= g=gridify[data];
During evaluation of In[4]:= xmin = 0.00503481 xmax = 0.960222 dx =
0.00964836
During evaluation of In[4]:= ymin = 0.00759144 ymax = 0.994708 dy =
0.00997087

In[5]:= f=ListInterpolation[ g];

Compute interpolated value at (.5,.6) :

In[6]:= f[.5,.6]

Out[6]= 1.58

Check value:

In[7]:= 2x^2+3y^2/.x->.5/.y->.6

Out[7]= 1.58

In[8]:= fx[f_,y0_] :=Interpolation[Table[{x,f[x,y0]},
{x,xmin,xmax,dx}]]  ;
fy[f_,x0_] :=Interpolation[Table[{y,f[x0,y]},{y,ymin,ymax,dy}]]  ;

Out[11]= {2.,3.6}

In[12]:= {D[2x^2+3y^2,x],D[2x^2+3y^2,y]}/.x->.5/.y->.6

Out[12]= {2.,3.6}

hth

Valeri Astanoff

```

• Prev by Date: Re: Converting notebooks to use Mathematica Player
• Next by Date: Re: binomial sum bug