【问题标题】:program runs differently depending on the use of cos or cosd( matlab functions)程序运行方式因使用 cos 或 cosd(matlab 函数)而异
【发布时间】:2012-07-09 19:38:39
【问题描述】:

当角度为弧度时,我的 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 有不同的行为会影响我的程序吗?或者我的程序中有一个错误被暴露了?

【问题讨论】:

  • 看起来你有一些以弧度硬编码的角度值(例如qd(2) = pi/2)......这让我怀疑你没有将整个程序更改为使用度数而不是弧度.如果您不将 all 角度值更改为度数,您将遇到问题。不过,您不太可能在 sindcosd 中发现错误。

标签: matlab trigonometry


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    相关资源
    最近更新 更多