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

 Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de Dic. de 2018
[gx(590,50), gy(590,50)]

7 comentarios

bilal javed
bilal javed el 17 de Dic. de 2018
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) ]
bilal javed
bilal javed el 17 de Dic. de 2018
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
Walter Roberson
Walter Roberson el 17 de Dic. de 2018
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.)
bilal javed
bilal javed el 18 de Dic. de 2018
(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 el 18 de Dic. de 2018
Editada: Walter Roberson el 22 de Dic. de 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 .
bilal javed
bilal javed el 22 de Dic. de 2018
Thank you Walter Roberson, it really worked

Iniciar sesión para comentar.

Más respuestas (1)

Mark Sherstan
Mark Sherstan el 17 de Dic. de 2018

0 votos

Refer to the post here.

1 comentario

bilal javed
bilal javed el 17 de Dic. de 2018
Thank you!
but my case is little bit different, the matrix f has all numerical values

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 17 de Dic. de 2018

Editada:

el 22 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by