From: Adrian on 12 Aug 2010 17:14 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
From: Roger Stafford on 12 Aug 2010 18:45 "Adrian " <adrianb_remove_this(a)uga.edu> wrote in message <i41o6t$1l3$1(a)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
From: Adrian on 13 Aug 2010 13:13 "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i41thj$7q2$1(a)fred.mathworks.com>... > "Adrian " <adrianb_remove_this(a)uga.edu> wrote in message <i41o6t$1l3$1(a)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
From: someone on 13 Aug 2010 13:40 "Adrian " <adrianb_remove_this(a)uga.edu> wrote in message <i43uf0$i1r$1(a)fred.mathworks.com>... > "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i41thj$7q2$1(a)fred.mathworks.com>... > > "Adrian " <adrianb_remove_this(a)uga.edu> wrote in message <i41o6t$1l3$1(a)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.
From: Roger Stafford on 13 Aug 2010 18:21 "Adrian " <adrianb_remove_this(a)uga.edu> wrote in message <i43uf0$i1r$1(a)fred.mathworks.com>... > "Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i41thj$7q2$1(a)fred.mathworks.com>... > > "Adrian " <adrianb_remove_this(a)uga.edu> wrote in message <i41o6t$1l3$1(a)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
|
Pages: 1 Prev: textscan without delimiter Next: Creating a Music Player in Matlab |