calculate the gradient at a point

I have a situation something like this
potential gradient field f (400*600)
[gx, gy] = gradient(f);
I have to find gradient at a point say (590,50). How can I calculate it?Untitled.png

 Accepted Answer

[gx(590,50), gy(590,50)]

7 Comments

But gx and gy are 400*600 matrices. I got this error
Index in position 1 exceeds array bounds (must not exceed 400).
Could you confirm that you have a numeric f, 400 x 600, but you want to find the gradient at a point outside of that grid, at (590, 50) ? Is it possible that you instead want to find at point (50, 590) ?
You need to be careful about what the coordinates refer to . If (590, 50) refer to x and y coordinates, then you need to know how x and y correspond to your rows and columns, with it being likely that x would resolve to a column index, and that y would resolve to a row index. For example, perhaps you have
xvals = linspace(178, 609, 600);
yvals = linspace(-99, 58, 400);
with any particular location f(R,C) corresponding to (xvals( C), yvals( R)) .
If so then to find the values at a bunch of locations, usually it would be easiest to use interp2().
For a single point you could use
[~, cidx] = min( abs(xvals - 590) );
[~, ridx] = min( abs(yvals - 50) );
[ gx(ridx, cidx), gy(ridx, cidx) ]
Actually I am taking an online course and this is a part of assignment. I wrote in the discussion forum there but didn't find any proper response, just some hints that I don't understand clearly.
here is more about my question
f : A 2D array containing the potential function values.
start_coords : An array specifying the coordinates of the start location the first entry is the x coordinate and the second the y
end_coords : An array specifying the coordinates of the goal the first entry is the x coordinate and the second the y
  1. On every iteration the planner should update the position of the robot based on the gradient values contained in the arrays gx and gy. Make sure you normalize the gradient vectors.
Hints: make sure you're using (x, y) coordinates and (i, j) coordinates correctly
For that purpose you can probably assume that the x and y coordinates are integers, and that x is a column number and that y is a row number.
MATLAB arrays are indexed by rows and then columns, and "rows" are considered to correspond to vertical dimension (which corresponds to y in cartesian geometry) and "columns" are considered to correspond to horizontal dimension (which corresponds to x in cartesian geometry.)
(590, 50) is in xy coordinate, not matrix ij coordinate.
So how can I find the correct value for [gx gy] corresponds to [590, 50]
Walter Roberson
Walter Roberson on 18 Dec 2018
Edited: Walter Roberson on 22 Dec 2018
x is a column number .y is a row number . Column 590 row 50 is gx(50,590) because rows are listed first in MATLAB as I explained earlier .
Thank you Walter Roberson, it really worked

Sign in to comment.

More Answers (1)

Mark Sherstan
Mark Sherstan on 17 Dec 2018
Refer to the post here.

1 Comment

Thank you!
but my case is little bit different, the matrix f has all numerical values

Sign in to comment.

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!