
wrong gradient direction result
10 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
the cyclist
on 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 Comments
the cyclist
on 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.
See Also
Categories
Find more on Assembly in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!