Matrix of symbols?

3 views (last 30 days)
Matteo Sodano
Matteo Sodano on 13 Oct 2018
Commented: Walter Roberson on 13 Oct 2018
I have an init script with some symbols declared:
syms q1 q2 q3 q4 q5 q6
This script calls a function, where I try to build a matrix that depends on those symbols:
function angular_jacobian(T,n,joint,q)
JA(:,1) = [0; 0; 1];
for i=2:n
if joint(i) == 'p'
JA(:,i) = [0; 0; 0];
else
disp(T(1:3,3,i-1));
JA(:,i) = simplify(T(1:3,3,i-1));
end
end
disp(JA);
end
where T is a vector of 4x4 matrices. I'm just trying to build a 3xN matrix, called JA, that has first column [0;0;1], and then all the others equal to a column of zeros or to T(1:3,3,i-1). The error I get is the following:
The following error occurred converting from sym to double:
Unable to convert expression into double array.
This happens only if T(1:3,3,i-1) has symbols in it (I tried with scalar T(1:3,3,i-1) and it works). Of course every matrix T(1:3,3,i-1) has only declared symbols (I built T with other functions).
Does anybody know what I am ignoring? I have actually no idea.
  2 Comments
madhan ravi
madhan ravi on 13 Oct 2018
Edited: madhan ravi on 13 Oct 2018
T,n,joint,q values? upload full code
Matteo Sodano
Matteo Sodano on 13 Oct 2018
You're right. I've answered below with the values of interest.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Oct 2018
JA = sym(zeros(3,n));
simplify always returns a sym even if given numeric input, so we can conclude that you always want a sym output.
  1 Comment
Walter Roberson
Walter Roberson on 13 Oct 2018
The data type for a variable is determined by what is first assigned to it. When you happen to assign numeric [0; 0; 0] first (because you did not pre-allocate the matrix) then the datatype becomes double. Numeric arrays cannot store symbolic expressions, and try to convert them to double precision numbers, but that will fail if the symbolic expressions contain unresolved symbolic variables.
So you work around the problem by initializing to symbolic in the first place.

Sign in to comment.

More Answers (1)

Matteo Sodano
Matteo Sodano on 13 Oct 2018
Values of interest:
n = 5;
q = [q1 q2 q3 q4 q5]; %we know that those are symbols from the code i posted before
joint = ['r' 'p' 'p' 'r' 'r'];
I attach an image of the Ts.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!