0

当角度为弧度时,我的 Matlab 程序可以正常工作,因此我在下面的代码中调用 cos 和 sin 函数。当角度以度为单位并因此我调用 cosd 和 sind 时,我的程序无法按预期工作。

%Initial configuration of robot manipulator
%There are 7DOF( degrees of freedom) - 1 prismatic, 6 revolute
%vector qd represents these DOF
%indexes    : d = gd( 1), q1 = qd( 2), ..., q6 = qd( 7)
qd( 1)      =       1;      % d  = 1 m
qd( 2)      =  pi / 2;      % q1 = 90 degrees
qd( 3 : 6)  =       0;      % q2 = ... = q6 = 0 degrees
qd( 7)      = -pi / 2;
%Initial position of each joint - the tool is manipulated separately
%calculate sinusoids and cosines
[ c, s] = sinCos( qd( 2 : length( qd)));

这是 sinCos 代码

function [ c, s] = sinCos( angles)
%takes a row array of angles in degrees and returns all the
%sin( angles( 1) + angles( 2) + ... + angles( i)) and
%cos( angles( 1) + angles( 2) + ... + angles( i)) where
%1 <= i <= length( angles)
sum = 0;
s   = zeros( 1, length( angles));       % preallocate for speed
c   = zeros( 1, length( angles));
for i = 1 : length( angles)
    sum = sum + angles( i);
    s( i) = sin( sum);     % s( i) = sin( angles( 1) + ... + angles( i))
    c( i) = cos( sum);     % c( i) = cos( angles( 1) + ... + angles( i))
end % for
% end function

整个程序大约有 700 行,所以我只显示了上面的部分。我的程序模拟了一个冗余机器人的运动,它试图在避开两个障碍物的同时达到一个目标。

那么,我的问题与 cos 和 cosd 有关吗?cos 和 cosd 有不同的行为会影响我的程序吗?或者我的程序中有一个错误被泄露?

4

1 回答 1

0

不同的是,您的意思是小于 0.00001 的量吗?因为极小的错误可以由于浮点算术错误而被忽略。计算机无法准确地计算十进制数,因为它们能够存储它们。正是出于这个原因,您永远不应该直接比较两个浮点数;必须允许一定范围的误差。您可以在此处阅读更多相关信息:http ://en.wikipedia.org/wiki/Floating_point#Machine_precision_and_backward_error_analysis

如果您的错误大于 0.0001 左右,您可能会考虑在程序中搜索错误。如果您还没有使用 matlab 为您转换单位,请考虑这样做,因为我发现它可以消除许多“明显”的错误(并且在某些情况下会提高精度)。

于 2012-07-09T19:51:27.723 回答