user70299
user70299

Reputation: 87

From symbolic to numeric in matlab

In my problem, there are some matrices like

T_x=[ cos(q1) sin(q1+q2) cos(q1) -sin(q2);
      0       0           1      -1;
      sin(q4) 0           1      q1;
      0       0           0      1]

Moreover I have the q values such as: q=[0.2 0.05 -2 -3.5]

How can I insert the q values into T_x matrix?

Thanks

Upvotes: 1

Views: 301

Answers (2)

Ander Biguri
Ander Biguri

Reputation: 35525

You can use subs.

syms q1 q2 q3 q4

T_x = [ cos(q1) sin(q1+q2) cos(q1) -sin(q2);
        0       0          1       -1;
        sin(q4) 0          1       q1;
        0       0          0       1];

q = [.2 .05 -2 -3.5];

subs(T_x, [q1 q2 q3 q4], q)

ans =

[  cos(1/5), sin(1/4), cos(1/5), -sin(1/20)]
[         0,        0,        1,         -1]
[ -sin(7/2),        0,        1,        1/5]
[         0,        0,        0,          1]

double(ans)

ans =

    0.9801    0.2474    0.9801   -0.0500
         0         0    1.0000   -1.0000
    0.3508         0    1.0000    0.2000
         0         0         0    1.0000

Just do it for all the symbolic variables you want!

Upvotes: 1

One way would be to have a matrix-returning function that takes the values as an argument:

>> T_x = @(q) [ cos(q(1)) sin(q(1)+q(2)) cos(q(1)) -sin(q(2));
                0          0             1         -1;
                sin(q(4))  0             1         q(1);
                0          0             0         1];

>> T_x([.2 .05 -2 -3.5])

ans =

    0.9801    0.2474    0.9801   -0.0500
         0         0    1.0000   -1.0000
    0.3508         0    1.0000    0.2000
         0         0         0    1.0000

This has the benefit of not needing the symbolic package - it's portable to Octave.

Upvotes: 2

Related Questions