Output of matrices with symbolic values not being zero but instead being x*e^-17

7 views (last 30 days)

Doing some modelling of mechatronic systems in a course right now, where there are matrices of symbolic values of angles, lengths etc. The output in some locations in the matrices should be zero (my lab partners gets 0), but my output is something like "(684969180613545*I3z)/182687704666362864775460604089535377456991567872" which equals 3.7494e-33. Is there a setting or such that gives me 0 instead?

if true
%
  syms q1 q2 q3 real
syms L1 L2 real
q=[q1;q2;q3];
a=[0; L1; L2];
al=[-pi/2; 0; 0];
d=[0;0;0];
th=[q1; q2; q3-pi/2];
R=@(a,al,d,th)([cos(th), -sin(th)*cos(al), sin(th)*sin(al)
  sin(th), cos(th)*cos(al), -cos(th)*sin(al)
  0, sin(al), cos(al)]);
R0=eye(3);
R1= R(a(1),al(1),d(1),th(1));
R10=R0*R1;
end

Printing R10 gives the output

if true
  % code
R10 =
[ cos(q1), -(4967757600021511*sin(q1))/81129638414606681695789005144064,                                          -sin(q1)]
[ sin(q1),  (4967757600021511*cos(q1))/81129638414606681695789005144064,                                           cos(q1)]
[       0,                                                           -1, 4967757600021511/81129638414606681695789005144064]
end

Went through my lab partners preferences and mine and compared, but did not see any significant difference. Later on as the matrices become larger and more complex this becomes a real issue to provide answers to my report. Thankful for help!

// Erik

Answers (2)

Roy Kadesh
Roy Kadesh on 16 Feb 2018
The problem with computers is that they count in binary, which means they can have trouble storing some numbers. The standard data type in Matlab is a double precision float, which has a precision of about 2e-16. This means that all calculations that span more than 16 orders of magnitude can be off. It is the reason the eps and ismembertol functions exist.
  1 Comment
Erik Henning Larsson
Erik Henning Larsson on 17 Feb 2018
But how come others gets zero and I don't? Finding this very strange, and I don't really recall having this issue before (could be sinse my update from 2016b to 2017b, but not sure).

Sign in to comment.


Star Strider
Star Strider on 17 Feb 2018
Use the vpa (link) function:
R10=vpa(R)
R10 =
[ cos(th), -1.0*cos(al)*sin(th), sin(al)*sin(th)]
[ sin(th), cos(al)*cos(th), -1.0*sin(al)*cos(th)]
[ 0, sin(al), cos(al)]
  2 Comments
Erik Henning Larsson
Erik Henning Larsson on 17 Feb 2018
Using the vpa function gives me 0.00...0{random decimals}, which makes it somewhat easier to read, but not really enough. Seems like it's when using the anonymous function that stuff got wierd, tried again with hardcoded R10 and it turned out good.
R10 =
[ cos(q1), 0, -sin(q1)]
[ sin(q1), 0, cos(q1)]
[ 0, -1, 0]
Star Strider
Star Strider on 17 Feb 2018
The Symbolic Math Toolbox prefers symbolic functions (see the symfun (link) function, introduced in R2012a).
Using that convention, ‘R’ becomes:
R(a,al,d,th) = ([cos(th), -sin(th)*cos(al), sin(th)*sin(al)
sin(th), cos(th)*cos(al), -cos(th)*sin(al)
0, sin(al), cos(al)]);
Declaring ‘al’ to be symbolic variables would also help:
al = sym([-pi/2; 0; 0]);
The Symbolic Math Toolbox can use anonymous functions, however (at least in my experience) they force double-precision floating-point numeric computations, not the extended-precision symbolic computations that permit the symbolic engine to be fully functional.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!