wrong gradient direction result

9 Ansichten (letzte 30 Tage)
djverhof
djverhof am 17 Jul. 2017
Kommentiert: the cyclist am 18 Jul. 2017
Hy
I have a question about the gradient funtion. Normally it should give me 2 matrices, giving x and y direction in which my function increases most strongly. However, the result I receive from the gradient function gives the direction for decreasing values. My question now is how this is possible?
My situation is building a map for obstacle avoidance. My map consists of potential values which indicate how close the robot would be to an obstacle.
Now, I will give an example of a part of this large map for which the gradient gives the wrong direction.
for instance: Consider the potential matrix A:
A=[51.2343,57.0290,63.9665,72.3547,82.6144;
29.6140,31.9091,34.5465,37.5890,41.1153;
19.9008,21.0137,22.2711,23.6926,25.3016;
14.7864,15.3928,16.0771,16.8473,17.7134;
11.8343,12.1874,12.5910,13.0494,13.5681]
The gradient(A) will give 2 matrices Jx and Jy. If I plot my result, I get the following image:
The commands used for plotting are:
[Jx1,Jx2]=gradient(A)
f=figure('Name','Plot of contour A and quiver of grad','NumberTitle','off');
ax=axes('Parent',f);
contour(ax,x,y,A);
hold(ax,'on');
quiver(ax,x,y,Jx1,Jx2);
For the plot, x and y are respectively:
x=[2.1500 2.2000 2.2500 2.3000 2.3500];
y=[-0.0500 -0.1000 -0.1500 -0.2000 -0.2500]
So, how is it possible that the result of the gradient function gives the direction for a decrease in potential instead of the strongest increase?
Note: If I used
[Jx1,Jx2]=gradient(A,0.05,0.05)
I got the same results, thus also the wrong direction.
Thank you

Antworten (1)

the cyclist
the cyclist am 17 Jul. 2017
gradient is clearly working properly, giving a positive number as it works left-to-right across the rows, and top-to-bottom down the columns (which is how MATLAB matrix indexing works).
I suspect that you were expecting MATLAB to work bottom-to-top on the gradient. If you instead do
[Jx1,Jx2]=gradient(flipud(A));
do you get the result you expect?
  2 Kommentare
djverhof
djverhof am 18 Jul. 2017
First of all thanks for your answer. Secondly, you indeed mentioned what my mistake was, or rather where the viewpoint of matlab differs than my viewpoint. I indeed have increasing x values along the columns going to the right and decreasing y values along the rows going to the bottom. But whith the way matlab calculates the gradient, the gradient along the x direction will be correct while the gradient along the y direction will be the negative of the true one.
I have tested your proposition, but even this is not completely correct. The dirrection however is correct now, but the length of the arrows is reversed. If I were to use flipud(A), the results would be small arrows where my function variation is high, and big arrows where the function variation is low.
But what you said solved my problem, since the correct solution to it is by taking the negative of the gradient along the y direction. The gradient(A) takes the difference of A(x,y_low)-A(x,y_high) which is the negative of the correct results, namely A(x,y_high)-A(x,y_low) (with y_low having a higher row number than y_high).
Thus if I correct my command to:
[Jx,-Jy]=gradient(A);
I get the following result for plotting -Jx and -Jy:
Thus now both the direction and the size of the arrows are correct.
Thank you very much for pointing this difference out to me!
the cyclist
the cyclist am 18 Jul. 2017
Happy to help, especially with someone who is thoughtful in both their question, and the followup.
FYI, the best form of thanks here is to upvote and/or accept useful answers. This rewards the contributor, and can point future users to the most helpful responses.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by