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

根据cos或cosd(matlab函数)的使用,程序运行方式不同,matlab,trigonometry,Matlab,Trigonometry,当角度以弧度表示时,我的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)

当角度以弧度表示时,我的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)));
这是辛克斯密码

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
函数[c,s]=sinCos(角度)
%获取角度的行数组(以度为单位),并返回所有
%sin(角度(1)+角度(2)+…+角度(i))和
%cos(角(1)+角(2)+…+角(i)),其中

%1通过差异,你是指小于0.00001的数量级的东西吗?因为非常小的错误可以由于浮点算术错误而被忽略。计算机不能像存储十进制数那样精确地计算十进制数。正是因为这个原因,你不应该直接比较两个浮点数;必须允许出现一定范围的错误。您可以在此处阅读更多关于此的信息:


如果你的错误大于0001左右,你可以考虑在程序中搜索bug。如果你还没有使用Matlab为你转换单元,那么考虑一下,我发现它可以消除许多“明显”的错误(并且在某些情况下提高精度)。看起来,你已经在弧度中编码了一些角值(<代码> QD(2)=π/2</代码>)…这让我怀疑您没有将整个程序更改为使用度而不是弧度。如果不将所有角度值更改为度,则会出现问题。不过,您不太可能在

sind
cosd
中发现bug。