Search code examples
matlabfor-loopgradientgeometry-surface

How do I find the average slope of a 3-D surface? MatLab


`2.4   2.34  2.21   1.90    1.4   0.83  0
 2.34  2.42  2.16   1.79    1.3   0.7   0
 2.21  2.16  1.99   1.64    1.16  0.51  0
 1.90  1.79  1.64   1.30    0.75   0    0
 1.4   1.34  1.16   0.75    0      0    0
 0.83  0.75  0.51   0       0      0    0
 0     0     0      0       0      0    0

`

enter image description here

This is what my matrix looks like at one point in time and Id like to calculate the average slope or gradient of the surface( An indicator of steepness).

It seems like MatLab should have a built in function for this sort of thing but I can find one. I have also tried a code that I wrote but it is not accurate. Best, Abid


Solution

  • the gradient is vectorial, so you can treat the two dimensions separately. Calculating the gradient consists of calculating the first derivative to every parameter of the function. In the discrete domain, this can be done by taking the finite differences: http://en.wikipedia.org/wiki/Finite_difference

    If you want it exact and calculate the gradient at each point, you can't just take the forward or backward differences, because at the edge, you don't always have a forward (or backward) point.

    But I think if you just want the mean of the gradient, you don't have to bother with the edges and you can just compute the finite differences for the central points between your original points by using diff:

    dx = diff(data,1,1);
    dy = diff(data,1,2);
    
    mean_gradient = [mean(dx(:)) mean(dy(:))]
    

    assuming equal spacing of the parameters of cours, else you'll have to divide dx and dy by the step sizes.

    For the data you provided, this results in:

    mean_gradient =
    
       -0.26381  -0.26381
    

    which matches the plot you've made that shows that the function is monotonically decreasing and symmetric in the parameters.