"zhang " <xiaoc10@gmail.com> wrote in message <hsrn4j$o85$1@fred.mathworks.com>...
> I want to use function gradient to compute a matrix.
> For example:
> x = [2 4 5; 5 6 9; 0 1 3];
> [fx, fy] = gradient(x);
>
> fx and fy are both 3*3 matix.
>
> My question is how to get a 3*3 result? It seems that we can only
> get a 2*2 result?
>
> Thanks in advanced
With [fx,fy] = gradient(f), at the edge points of fx and fy, 'gradient' does a 'diff' of f with the next inside point, but in the interior it takes the difference of the two f values on either side, divided by 2. This way the fx and fy matrices are the same size as f. However, this can make edge gradient values somewhat skewed towards inner values.
Also note that with [fx,fy] = gradient(f,hx,hy), if hx and hy are vectors containing varying intervals, 'gradient' does not achieve second order approximations to gradient values. That is, for example, on interior points it uses the simpler
fx(i,j) = (f(i,j+1)f(i,j1))/(hx(j+1)hx(j1))
instead of
fx(i,j) =
(f(i,j+1)f(i,j))/(hx(j+1)hx(j)) * (hx(j)hx(j1))/(hx(j+1)hx(j1) +
(f(i,j)f(i,j1))/(hx(j)hx(j1)) * (hx(j+1)hx(j))/(hx(j+1)hx(j1)
which is the appropriate second order approximation for varying length intervals in x.
Roger Stafford
