Search code examples
matlabsymbolic-math

How to insert numerical values in symbolical variables?


I want to compute a rotation matrix with symbolical values. After that some numerical values should be inserted and the result should be displayed. How to do this with matlab ? I tried "subs" but this does not compute the 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])

I just want a numerical value of this expression, but the workspace in matlab says "4x4 sym"..


Solution

  • You're very close. You need to encapsulate the multiple variables in a cell array, not a numeric array. Replace the [] to {}. Also, make sure you assign the result of your subs call back to something... so in your case, use M05 again:

    >> 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]
    

    However, this is still in symbolic format. If you want the numeric result, simply cast M05 to 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
    

    Small note (thanks to horchler)

    Since MATLAB R2012a+, you don't need to use {} to encapsulate the variables. You can just use [] as normal as this is more in tune with how MuPAD's version of subs works.