angdiff doesn't work as documented
11 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm using matlab 2015b. When used with a single variable, angdiff is documented to return the angular difference (smallest distance) between two adjacent angles.
Practically, it returns the distance of all the angles from zero. For example, this - angdiff(0:(pi/6):2*pi)
suppose to return [pi/6, pi/6 ... ] but it returns, [0, pi/6, 2*pi/6 ..]
Thank you, Eyal.
0 个评论
回答(2 个)
Guillaume
2016-8-31
编辑:Guillaume
2016-8-31
I don't have the robotics toolbox, so can't check. If it does truly behave as you say then it is a bug you should reports to mathworks.
In the meantime, the function is trivial to implement:
function delta = nonbuggy_angdiff(alpha, beta)
validateattributes(alpha, {'numeric'}, {'vector', 'nonsparse'}, 1);
if nargin > 1
validateattributes(beta, {'numeric'}, {'vector', 'nonsparse', 'size', size(alpha)}, 2);
alpha = beta - alpha;
else
alpha = diff(alpha);
end
delta = mod(alpha + pi, 2*pi) - pi; %constrain to [-pi, pi[. will prefer -pi over +pi
delta(delta == -pi & alpha >= 0) = pi; %so force -pi to +pi (only when the original angle was positive)
end
edit: bugfix. There may still be bugs lurking about.
2 个评论
Thorsten
2016-8-31
编辑:Thorsten
2016-8-31
This functions returns pi for angdiff(0,pi) and angdiff(pi, 0), which is wrong, or at least a bit strange.
And it does not work properly on arrays.
It return only a single pi if all values are pi:
angdiff([0 pi], [pi 0])
ans =
3.1416
and it gives inconsistent results if not all elements equal -pi:
angdiff([0 pi 0 ], [pi 0 0.1])
ans =
-3.1416 -3.1416 0.1000
because with single values angdiff(0,pi) and angdiff(pi,0) both return pi, not -pi.
And it does not work on some single matrices, as expected:
>> angdiff([0 pi 0])
ans =
3.1416
Should be pi, -pi.
Guillaume
2016-8-31
The first issue was a bug. Now fixed.
As to what angle should be returned for a pi or -pi angle, I find it puzzling that the official angdiff returns both since they're the same mod 2pi.
In any case, that was trivial to adapt to, so now my function should behave the same as the official angdiff.
As said, I don't have the robotics toolbox to check.
Thorsten
2016-8-31
Have a look which angdiff you use
which angdiff
it may be function different from the angdiff in the Robotics Toolbox.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Coordinate Transformations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!