http://www.mathworks.com/matlabcentral/newsreader/view_thread/289198
MATLAB Central Newsreader  Problems with Logs and the gradient function
Feed for thread: Problems with Logs and the gradient function
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 12 Aug 2010 21:14:05 +0000
Problems with Logs and the gradient function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/289198#771007
Adrian
I am trying to understand the gradient function in Matlab. As I understand it, <br>
<br>
gradient(y)<br>
<br>
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. <br>
<br>
This works as expected if I have <br>
<br>
x = 1:20;<br>
y = x.^2;<br>
dydx = 2*x;<br>
<br>
disp([dydx; gradient(y)]);<br>
<br>
in which case dydx and gradient(y) produce the same vales (except at the end points which is to be expected). <br>
<br>
Now let's take the same function but have the x values geometrically spaced<br>
<br>
n = 1:10;<br>
x = 2.^(n1);<br>
y = x.^2;<br>
<br>
If I use the gradient function, it will still assume a unit spacing of x between the data points. <br>
<br>
My question is: why isn't gradient(y) = dy/dlog2(x)?<br>
<br>
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)<br>
<br>
(dy/dlog2(x))./gradient(y) = 0.73936<br>
<br>
I'm obviously missing something somewhere, can someone help me out here?<br>
<br>
Thanks,<br>
<br>
Adrian<br>
<br>

Thu, 12 Aug 2010 22:45:07 +0000
Re: Problems with Logs and the gradient function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/289198#771029
Roger Stafford
"Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...<br>
> I am trying to understand the gradient function in Matlab. As I understand it, <br>
> <br>
> gradient(y)<br>
> <br>
> 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. <br>
> <br>
> This works as expected if I have <br>
> <br>
> x = 1:20;<br>
> y = x.^2;<br>
> dydx = 2*x;<br>
> <br>
> disp([dydx; gradient(y)]);<br>
> <br>
> in which case dydx and gradient(y) produce the same vales (except at the end points which is to be expected). <br>
> <br>
> Now let's take the same function but have the x values geometrically spaced<br>
> <br>
> n = 1:10;<br>
> x = 2.^(n1);<br>
> y = x.^2;<br>
> <br>
> If I use the gradient function, it will still assume a unit spacing of x between the data points. <br>
> <br>
> My question is: why isn't gradient(y) = dy/dlog2(x)?<br>
> <br>
> 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)<br>
> <br>
> (dy/dlog2(x))./gradient(y) = 0.73936<br>
> <br>
> I'm obviously missing something somewhere, can someone help me out here?<br>
> <br>
> Thanks,<br>
> <br>
> Adrian<br>
       <br>
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.<br>
<br>
Roger Stafford

Fri, 13 Aug 2010 17:13:04 +0000
Re: Problems with Logs and the gradient function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/289198#771246
Adrian
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i41thj$7q2$1@fred.mathworks.com>...<br>
> "Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...<br>
> > I am trying to understand the gradient function in Matlab. As I understand it, <br>
> > <br>
> > gradient(y)<br>
> > <br>
> > 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. <br>
> > <br>
> > This works as expected if I have <br>
> > <br>
> > x = 1:20;<br>
> > y = x.^2;<br>
> > dydx = 2*x;<br>
> > <br>
> > disp([dydx; gradient(y)]);<br>
> > <br>
> > in which case dydx and gradient(y) produce the same vales (except at the end points which is to be expected). <br>
> > <br>
> > Now let's take the same function but have the x values geometrically spaced<br>
> > <br>
> > n = 1:10;<br>
> > x = 2.^(n1);<br>
> > y = x.^2;<br>
> > <br>
> > If I use the gradient function, it will still assume a unit spacing of x between the data points. <br>
> > <br>
> > My question is: why isn't gradient(y) = dy/dlog2(x)?<br>
> > <br>
> > 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)<br>
> > <br>
> > (dy/dlog2(x))./gradient(y) = 0.73936<br>
> > <br>
> > I'm obviously missing something somewhere, can someone help me out here?<br>
> > <br>
> > Thanks,<br>
> > <br>
> > Adrian<br>
>        <br>
> 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.<br>
<br>
Hi Roger,<br>
<br>
Thanks for the reply, but I don't think that's the case. According to the help page for the function gradient<br>
<br>
[...] = gradient(F,h) where h is a scalar uses h as the spacing between points in each direction.<br>
<br>
[...] = gradient(F,h1,h2,...) with N spacing parameters specifies the spacing for each dimension of F.<br>
<br>
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 "xdirection" and one for the "ydirection". <br>
<br>
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.<br>
<br>
Adrian

Fri, 13 Aug 2010 17:40:20 +0000
Re: Problems with Logs and the gradient function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/289198#771249
someone
"Adrian " <adrianb_remove_this@uga.edu> wrote in message <i43uf0$i1r$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i41thj$7q2$1@fred.mathworks.com>...<br>
> > "Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...<br>
> > > I am trying to understand the gradient function in Matlab. As I understand it, <br>
> > > <br>
> > > gradient(y)<br>
> > > <br>
> > > 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. <br>
> > > <br>
> > > This works as expected if I have <br>
> > > <br>
> > > x = 1:20;<br>
> > > y = x.^2;<br>
> > > dydx = 2*x;<br>
> > > <br>
> > > disp([dydx; gradient(y)]);<br>
> > > <br>
> > > in which case dydx and gradient(y) produce the same vales (except at the end points which is to be expected). <br>
> > > <br>
> > > Now let's take the same function but have the x values geometrically spaced<br>
> > > <br>
> > > n = 1:10;<br>
> > > x = 2.^(n1);<br>
> > > y = x.^2;<br>
> > > <br>
> > > If I use the gradient function, it will still assume a unit spacing of x between the data points. <br>
> > > <br>
> > > My question is: why isn't gradient(y) = dy/dlog2(x)?<br>
> > > <br>
> > > 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)<br>
> > > <br>
> > > (dy/dlog2(x))./gradient(y) = 0.73936<br>
> > > <br>
> > > I'm obviously missing something somewhere, can someone help me out here?<br>
> > > <br>
> > > Thanks,<br>
> > > <br>
> > > Adrian<br>
> >        <br>
> > 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.<br>
> <br>
> Hi Roger,<br>
> <br>
> Thanks for the reply, but I don't think that's the case. According to the help page for the function gradient<br>
> <br>
> [...] = gradient(F,h) where h is a scalar uses h as the spacing between points in each direction.<br>
> <br>
> [...] = gradient(F,h1,h2,...) with N spacing parameters specifies the spacing for each dimension of F.<br>
> <br>
> 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 "xdirection" and one for the "ydirection". <br>
> <br>
> 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.<br>
> <br>
> Adrian<br>
<br>
Yes, that is what the documentation says. But did you try Rodger's suggestion?<br>
<br>
gradient(y, dydx)<br>
<br>
where dydx is a vector the same length as y seems to work for me.<br>
<br>
Perhaps the MATLAB documentation shoud be upgraded <br>
for the case(s) when h is NOT a vector.

Fri, 13 Aug 2010 22:21:05 +0000
Re: Problems with Logs and the gradient function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/289198#771356
Roger Stafford
"Adrian " <adrianb_remove_this@uga.edu> wrote in message <i43uf0$i1r$1@fred.mathworks.com>...<br>
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i41thj$7q2$1@fred.mathworks.com>...<br>
> > "Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...<br>
> > > I am trying to understand the gradient function in Matlab. As I understand it, <br>
> > > <br>
> > > gradient(y)<br>
> > > <br>
> > > 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. <br>
> > > <br>
> > > This works as expected if I have <br>
> > > <br>
> > > x = 1:20;<br>
> > > y = x.^2;<br>
> > > dydx = 2*x;<br>
> > > <br>
> > > disp([dydx; gradient(y)]);<br>
> > > <br>
> > > in which case dydx and gradient(y) produce the same vales (except at the end points which is to be expected). <br>
> > > <br>
> > > Now let's take the same function but have the x values geometrically spaced<br>
> > > <br>
> > > n = 1:10;<br>
> > > x = 2.^(n1);<br>
> > > y = x.^2;<br>
> > > <br>
> > > If I use the gradient function, it will still assume a unit spacing of x between the data points. <br>
> > > <br>
> > > My question is: why isn't gradient(y) = dy/dlog2(x)?<br>
> > > <br>
> > > 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)<br>
> > > <br>
> > > (dy/dlog2(x))./gradient(y) = 0.73936<br>
> > > <br>
> > > I'm obviously missing something somewhere, can someone help me out here?<br>
> > > <br>
> > > Thanks,<br>
> > > <br>
> > > Adrian<br>
> >        <br>
> > 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.<br>
> <br>
> Hi Roger,<br>
> <br>
> Thanks for the reply, but I don't think that's the case. According to the help page for the function gradient<br>
> <br>
> [...] = gradient(F,h) where h is a scalar uses h as the spacing between points in each direction.<br>
> <br>
> [...] = gradient(F,h1,h2,...) with N spacing parameters specifies the spacing for each dimension of F.<br>
> <br>
> 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 "xdirection" and one for the "ydirection". <br>
> <br>
> 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.<br>
> <br>
> Adrian<br>
          <br>
No Adrian, you are mistaken. You missed reading the part of the documentation just before the one you quote. It says there, "N spacing values (h1,h2,...) specifies the spacing for each dimension of F. Scalar spacing parameters specify a constant spacing for each dimension. Vector parameters specify the coordinates of the values along corresponding dimensions of F. In this case, the length of the vector must match the size of the corresponding dimension." In particular note the last two sentences. The gradient function allows you to enter a vector of the independent variable itself rather than a constant spacing.<br>
<br>
I know this to be the case because I have used the gradient function on my version of matlab with this feature and I have also made a study of the mfile code for it. For the gradient at the ith point (not at the two endpoints) the call d = gradient(y,x) will give you:<br>
<br>
d(i) = (y(i+1)y(i1))/(x(i+1)x(i1))<br>
<br>
This is a first order approximation of the derivative centered at x(i).<br>
<br>
If you are interested in a higher order approximation to a derivative, it can be obtained from the following formula:<br>
<br>
(y(i)y(i1))/(x(i)x(i1))*t + (y(i+1)y(i))/(x(i+1)x(i))*(1t)<br>
<br>
where<br>
<br>
t = (x(i+1)x(i))/(x(i+1)x(i1)).<br>
<br>
If y is a quadratic function of x, this will give its exact derivative.<br>
<br>
Roger Stafford

Sat, 14 Aug 2010 06:39:08 +0000
Re: Problems with Logs and the gradient function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/289198#771404
Steve Amphlett
"Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...<br>
> I am trying to understand the gradient function in Matlab. As I understand it, <br>
> <br>
> gradient(y)<br>
> <br>
> 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. <br>
> <br>
> This works as expected if I have <br>
> <br>
> x = 1:20;<br>
> y = x.^2;<br>
> dydx = 2*x;<br>
> <br>
> disp([dydx; gradient(y)]);<br>
> <br>
> in which case dydx and gradient(y) produce the same vales (except at the end points which is to be expected). <br>
> <br>
> Now let's take the same function but have the x values geometrically spaced<br>
> <br>
> n = 1:10;<br>
> x = 2.^(n1);<br>
> y = x.^2;<br>
> <br>
> If I use the gradient function, it will still assume a unit spacing of x between the data points. <br>
> <br>
> My question is: why isn't gradient(y) = dy/dlog2(x)?<br>
> <br>
> 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)<br>
> <br>
> (dy/dlog2(x))./gradient(y) = 0.73936<br>
> <br>
> I'm obviously missing something somewhere, can someone help me out here?<br>
<br>
I think you are missing the fact that the gradient() function supplied with Matlab is just a simple finite difference approximation, not a magical derivative calculator. It's not much more than a wrapper around a oneliner, with some careful end treatments. Do not expect it to be a close approximation to your analytical derivative unless your underlying function is 1st order or your xspacing is very small.<br>
<br>
If the supplied gradient() function doesn't support your requirements, write your own. It isn't difficult.