如何在符号变量中插入数值?

时间:2015-04-26 21:18:19

标签: matlab symbolic-math

我想用符号值计算旋转矩阵。之后,应插入一些数值,并显示结果。如何用matlab做到这一点?我尝试过“subs”,但这并没有计算sin / cos ..

syms d t1 t2 t3;
M01 = [cos(t1) 0 -sin(t1) 0;sin(t1) 0 cos(t1) 0;0 -1 0 d;0 0 0 1];
M12 = [cos(t2) -sin(t2) 0 0;sin(t2) cos(t2) 0 0;0 0 1 -10;0 0 0 1];
M23 = [1 0 0 30;0 1 0 0 ;0 0 1 0;0 0 0 1];
M34 = [cos(t3) -sin(t3) 0 0 ;sin(t3) cos(t3) 0 0 ;0 0 1 10;0 0 0 1];
M45 = [1 0 0 30;0 1 0 0 ;0 0 1 0;0 0 0 1];
disp('Transformation Matrix between S0 to S5 is:');
M05 = M01*M12*M23*M34*M45
disp('position of the end-effector with respect to base is:');
subs(M05,[t1 t2 t3 d],[degtorad(45) degtorad(-75) degtorad(130) 70])

我只想要这个表达式的数值,但matlab中的工作区说“4x4 sym”..

1 个答案:

答案 0 :(得分:2)

你非常接近。您需要将多个变量封装在单元格数组中,而不是数字数组中。将[]替换为{}。此外,请务必将subs来电的结果分配给某些内容......因此,在您的情况下,请再次使用M05

>> M05 = subs(M05,{t1,t2,t3,d},{degtorad(45),degtorad(-75),degtorad(130),70});

M05 =

[ (2^(1/2)*cos((5*pi)/18)*(2^(1/2)/4 - 6^(1/2)/4))/2 + (2^(1/2)*sin((5*pi)/18)*(2^(1/2)/4 + 6^(1/2)/4))/2, (2^(1/2)*sin((5*pi)/18)*(2^(1/2)/4 - 6^(1/2)/4))/2 - (2^(1/2)*cos((5*pi)/18)*(2^(1/2)/4 + 6^(1/2)/4))/2, -2^(1/2)/2, 15*2^(1/2)*cos((5*pi)/18)*(2^(1/2)/4 - 6^(1/2)/4) - 15*2^(1/2)*(2^(1/2)/4 - 6^(1/2)/4) + 15*2^(1/2)*sin((5*pi)/18)*(2^(1/2)/4 + 6^(1/2)/4)]
[ (2^(1/2)*cos((5*pi)/18)*(2^(1/2)/4 - 6^(1/2)/4))/2 + (2^(1/2)*sin((5*pi)/18)*(2^(1/2)/4 + 6^(1/2)/4))/2, (2^(1/2)*sin((5*pi)/18)*(2^(1/2)/4 - 6^(1/2)/4))/2 - (2^(1/2)*cos((5*pi)/18)*(2^(1/2)/4 + 6^(1/2)/4))/2,  2^(1/2)/2, 15*2^(1/2)*cos((5*pi)/18)*(2^(1/2)/4 - 6^(1/2)/4) - 15*2^(1/2)*(2^(1/2)/4 - 6^(1/2)/4) + 15*2^(1/2)*sin((5*pi)/18)*(2^(1/2)/4 + 6^(1/2)/4)]
[                         sin((5*pi)/18)*(2^(1/2)/4 - 6^(1/2)/4) - cos((5*pi)/18)*(2^(1/2)/4 + 6^(1/2)/4),                       - cos((5*pi)/18)*(2^(1/2)/4 - 6^(1/2)/4) - sin((5*pi)/18)*(2^(1/2)/4 + 6^(1/2)/4),          0,               (15*2^(1/2))/2 - 30*cos((5*pi)/18)*(2^(1/2)/4 + 6^(1/2)/4) + (15*6^(1/2))/2 + 30*sin((5*pi)/18)*(2^(1/2)/4 - 6^(1/2)/4) + 70]
[                                                                                                       0,                                                                                                       0,          0,                                                                                                                                          1]

但是,这仍然是符号格式。如果您想要数字结果,只需将M05投射到double

>> M05 = subs(M05,{t1 t2 t3 d},{degtorad(45) degtorad(-75) degtorad(130) 70});
>> M05 = double(M05)

M05 =

    0.4056   -0.5792   -0.7071   17.6578
    0.4056   -0.5792    0.7071   17.6578
   -0.8192   -0.5736         0   74.4032
         0         0         0    1.0000

小记(感谢horchler)

自MATLAB R2012a +以来,您无需使用{}来封装变量。您可以正常使用[],因为这更符合MuPAD的subs版本的工作原理。