```Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Problems with Logs and the gradient function
Date: Fri, 13 Aug 2010 17:40:20 +0000 (UTC)
Organization: The Mitre Corp
Lines: 63
Message-ID: <i44024\$dv\$1@fred.mathworks.com>
References: <i41o6t\$1l3\$1@fred.mathworks.com> <i41thj\$7q2\$1@fred.mathworks.com> <i43uf0\$i1r\$1@fred.mathworks.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1281721220 447 172.30.248.37 (13 Aug 2010 17:40:20 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 13 Aug 2010 17:40:20 +0000 (UTC)
Xref: news.mathworks.com comp.soft-sys.matlab:661834

> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i41thj\$7q2\$1@fred.mathworks.com>...
> > > I am trying to understand the gradient function in Matlab. As I understand it,
> > >
> > >
> > > for y a vector, will calculate the gradient of y assuming a unit interval between the points. In other words, it will calculate dy/dx assuming dx=1 for all points in the series.
> > >
> > > This works as expected if I have
> > >
> > > x = 1:20;
> > > y = x.^2;
> > > dydx = 2*x;
> > >
> > >
> > > in which case dydx and gradient(y) produce the same vales (except at the end points which is to be expected).
> > >
> > > Now let's take the same function but have the x values geometrically spaced
> > >
> > > n = 1:10;
> > > x = 2.^(n-1);
> > > y = x.^2;
> > >
> > > If I use the gradient function, it will still assume a unit spacing of x between the data points.
> > >
> > > My question is: why isn't gradient(y) = dy/dlog2(x)?
> > >
> > > log2(x) is equally spaced with unit interval but the gradient function is producting something different. In this case, dy/dlog2(x) = 2*log(2)*x.*x but this is not equal to the values produced by gradient(y). However, the ratio between the two is constant (except at the endpoints)
> > >
> > > (dy/dlog2(x))./gradient(y) = 0.73936
> > >
> > > I'm obviously missing something somewhere, can someone help me out here?
> > >
> > > Thanks,
> > >
> > - - - - - - - -
> >   The gradient function has provision for a second argument that specifies the x spacing.  If it is a scalar, that scalar is the constant spacing assumed.  If it is a vector the same length as the first argument, that gives the actual values of your x variable.  Check it out.
>
> Hi Roger,
>
> Thanks for the reply, but I don't think that's the case. According to the help page for the function gradient
>
> [...] = gradient(F,h) where h is a scalar uses h as the spacing between points in each direction.
>
> [...] = gradient(F,h1,h2,...) with N spacing parameters specifies the spacing for each dimension of F.
>
> For for F being a vector, there is only one h and it is a scalar. If F were a 2 x 2 matrix, then I could specify two scalar values of h, one for the "x-direction" and one for the "y-direction".
>
> This still doesn't explain why gradient(y) in the above example is not   dy/dlog2(x). It seems it should be, but it isn't.
>