Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problems with Logs and the gradient function

Subject: Problems with Logs and the gradient function

From: Adrian

Date: 12 Aug, 2010 21:14:05

Message: 1 of 6

I am trying to understand the gradient function in Matlab. As I understand it,

gradient(y)

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;

disp([dydx; gradient(y)]);

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,

Adrian

 

Subject: Problems with Logs and the gradient function

From: Roger Stafford

Date: 12 Aug, 2010 22:45:07

Message: 2 of 6

"Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...
> I am trying to understand the gradient function in Matlab. As I understand it,
>
> gradient(y)
>
> 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;
>
> disp([dydx; gradient(y)]);
>
> 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,
>
> Adrian
- - - - - - - -
  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.

Roger Stafford

Subject: Problems with Logs and the gradient function

From: Adrian

Date: 13 Aug, 2010 17:13:04

Message: 3 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i41thj$7q2$1@fred.mathworks.com>...
> "Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...
> > I am trying to understand the gradient function in Matlab. As I understand it,
> >
> > gradient(y)
> >
> > 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;
> >
> > disp([dydx; gradient(y)]);
> >
> > 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,
> >
> > Adrian
> - - - - - - - -
> 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.

Adrian

Subject: Problems with Logs and the gradient function

From: someone

Date: 13 Aug, 2010 17:40:20

Message: 4 of 6

"Adrian " <adrianb_remove_this@uga.edu> wrote in message <i43uf0$i1r$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i41thj$7q2$1@fred.mathworks.com>...
> > "Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...
> > > I am trying to understand the gradient function in Matlab. As I understand it,
> > >
> > > gradient(y)
> > >
> > > 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;
> > >
> > > disp([dydx; gradient(y)]);
> > >
> > > 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,
> > >
> > > Adrian
> > - - - - - - - -
> > 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.
>
> Adrian

Yes, that is what the documentation says. But did you try Rodger's suggestion?

gradient(y, dydx)

where dydx is a vector the same length as y seems to work for me.

Perhaps the MATLAB documentation shoud be upgraded
for the case(s) when h is NOT a vector.

Subject: Problems with Logs and the gradient function

From: Roger Stafford

Date: 13 Aug, 2010 22:21:05

Message: 5 of 6

"Adrian " <adrianb_remove_this@uga.edu> wrote in message <i43uf0$i1r$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i41thj$7q2$1@fred.mathworks.com>...
> > "Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...
> > > I am trying to understand the gradient function in Matlab. As I understand it,
> > >
> > > gradient(y)
> > >
> > > 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;
> > >
> > > disp([dydx; gradient(y)]);
> > >
> > > 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,
> > >
> > > Adrian
> > - - - - - - - -
> > 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.
>
> Adrian
- - - - - - - - - - -
  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.

  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 m-file code for it. For the gradient at the i-th point (not at the two endpoints) the call d = gradient(y,x) will give you:

 d(i) = (y(i+1)-y(i-1))/(x(i+1)-x(i-1))

This is a first order approximation of the derivative centered at x(i).

  If you are interested in a higher order approximation to a derivative, it can be obtained from the following formula:

 (y(i)-y(i-1))/(x(i)-x(i-1))*t + (y(i+1)-y(i))/(x(i+1)-x(i))*(1-t)

where

 t = (x(i+1)-x(i))/(x(i+1)-x(i-1)).

If y is a quadratic function of x, this will give its exact derivative.

Roger Stafford

Subject: Problems with Logs and the gradient function

From: Steve Amphlett

Date: 14 Aug, 2010 06:39:08

Message: 6 of 6

"Adrian " <adrianb_remove_this@uga.edu> wrote in message <i41o6t$1l3$1@fred.mathworks.com>...
> I am trying to understand the gradient function in Matlab. As I understand it,
>
> gradient(y)
>
> 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;
>
> disp([dydx; gradient(y)]);
>
> 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?

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 one-liner, 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 x-spacing is very small.

If the supplied gradient() function doesn't support your requirements, write your own. It isn't difficult.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us