根据cos或cosd(matlab函数)的使用,程序运行方式不同



当角度为弧度时,我的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有不同的行为会影响我的程序吗?或者我的程序中出现了一个漏洞?

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

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

相关内容

最新更新