Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

matlab - Matrix with symbolic math gives a symbolic answer, not a numeric one

Consider the following matrix

Ja(t1, t2, t3, t4, t5, t6) =

[                                                                                                             (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)) - sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/5 - sin(t1)/100 - (219*sin(t1)*sin(t2))/1000 - (19*cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 - (21*cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000 + (21*sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000, (219*cos(t1)*cos(t2))/1000 + (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)) - sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/5 - (19*cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 - (21*cos(t3)*cos(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000 + (21*sin(t3)*sin(t4)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/1000,                                                                                                             (sin(t5)*(cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) + cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 - (19*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100 - (21*cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000,                                                                                 (sin(t5)*(cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) + cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 - (21*cos(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*cos(t4)*sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000,                                        -(cos(t5)*(cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) - sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5, 0]
[ cos(t1)/100 + (219*cos(t1)*sin(t2))/1000 + (29*cos(t1)*sin(t3))/1000 - (21*cos(t4)*(cos(t1)*sin(t3) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 - (21*sin(t4)*(cos(t1)*cos(t3) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/1000 + (sin(t5)*(cos(t4)*(cos(t1)*sin(t3) - cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))) + sin(t4)*(cos(t1)*cos(t3) + sin(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))))/5 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100, (219*cos(t2)*sin(t1))/1000 - (sin(t5)*(cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)) - sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2))))/5 + (19*cos(t3)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/100 + (21*cos(t3)*cos(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000 - (21*sin(t3)*sin(t4)*(cos(t1)*cos(t2) - sin(t1)*sin(t2)))/1000, (29*cos(t3)*sin(t1))/1000 - (21*cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 + (21*sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (19*sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))/100 + (sin(t5)*(cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (21*sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 - (21*cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))))/1000 + (sin(t5)*(cos(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) - sin(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, (cos(t5)*(cos(t4)*(sin(t1)*sin(t3) - cos(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1))) + sin(t4)*(cos(t3)*sin(t1) + sin(t3)*(cos(t1)*sin(t2) + cos(t2)*sin(t1)))))/5, 0]
[                                                                                                                                                                                                                                                                                                                                                                                                                                                                     0,                                                                                                                                                                                                                                                                                                                                           0,                                                                                                                                                                                                                                                                                                (21*cos(t3)*cos(t4))/1000 - (29*cos(t3))/1000 - (21*sin(t3)*sin(t4))/1000 - (sin(t5)*(cos(t3)*cos(t4) - sin(t3)*sin(t4)))/5,                                                                                                                                                                                                                                 (21*cos(t3)*cos(t4))/1000 - (21*sin(t3)*sin(t4))/1000 - (sin(t5)*(cos(t3)*cos(t4) - sin(t3)*sin(t4)))/5,                                                                                                                -(cos(t5)*(cos(t3)*sin(t4) + cos(t4)*sin(t3)))/5, 0]

The problem is that when I put my arguments, MATLAB doesn't calculate the matrix numerically, rather it leaves it symbolically.

This is the result:

Ja(q(1),q(2),q(3),q(4),q(5),q(6)) = 

[                                                                                                                                                          sin(63/100)/100 + (219*sin(528276371951843/1125899906842624)*sin(63/100))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000, (219*cos(528276371951843/1125899906842624)*cos(63/100))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/1000,                                                                                                                                                        - (19*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (sin(59/125)*(cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) + cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 - (21*cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000,                                                                                                                   - (sin(59/125)*(cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) + cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 - (21*cos(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*cos(157/250)*sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000,                                                           -(cos(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5, 0]
[ cos(63/100)/100 + (219*sin(528276371951843/1125899906842624)*cos(63/100))/1000 + (29*cos(63/100)*sin(157/125))/1000 + (19*cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (21*cos(157/250)*(cos(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/1000 - (sin(59/125)*(cos(157/250)*(cos(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))) + sin(157/250)*(cos(63/100)*cos(157/125) + sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))))/5 - (21*sin(157/250)*(cos(63/100)*cos(157/125) + sin(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/1000, (19*cos(157/125)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/100 - (219*cos(528276371951843/1125899906842624)*sin(63/100))/1000 + (sin(59/125)*(cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)) - sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100))))/5 + (21*cos(157/125)*cos(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000 - (21*sin(157/125)*sin(157/250)*(cos(528276371951843/1125899906842624)*cos(63/100) + sin(528276371951843/1125899906842624)*sin(63/100)))/1000, (19*sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))/100 - (29*cos(157/125)*sin(63/100))/1000 + (21*cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 - (21*sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 + (sin(59/125)*(cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))) - sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100)))))/5, (21*cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 - (21*sin(157/250)*(sin(63/100)*sin(157/125) - cos(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/1125899906842624)*cos(63/100))))/1000 + (sin(59/125)*(cos(157/250)*(cos(157/125)*sin(63/100) + sin(157/125)*(cos(528276371951843/1125899906842624)*sin(63/100) - sin(528276371951843/112

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Short answer: evaluate your symbolic expression numerically using eval or convert it to a specific type using one of these options, f.e. double or vpa.

Note that eval may be twice as slow as using double, but is sometimes slightly faster too

Explanation

The problem is that MATLAB does not evaluate your symbolic expression numerically, it only simplifies your expression mathematically.

Example:

syms x
my_function(x) = cos(x)
% exact algebraic solution is known:
my_function(0) % returns 1
my_function(pi) % returns -1
my_function(pi/2) % returns 0
my_function(pi/6) % returns 3^(1/2)/2
% result can only be numerically approximated:
my_function(3.1415) % returns cos(6283/2000)
my_function(1) % returns cos(1)

So, MATLAB is able to simplify the cos expression when the result is exactly known. In general, the result of cos can only be numerically evaluated, therefore MATLAB displays cos in it answer.

If you want a numerical result you can use one of the following options:

  • eval: evaluates your matrix numerically
  • double: converts to double precision
  • single: converts to single precision
  • int8 : converts to 8 bit integers (alternatives int16, int32, int64)
  • vpa: converts to variable-precision arithmetic, i.e. it allows you to specify the desired accuracy (number of significant digits) of the result

See Conversion Between Symbolic and Numeric for more information


Is using eval a good option?

As pointed out by Sardar Usama, using eval (to evaluate a string) is often bad practice:

But, is this the same eval?

No, I don't think so. help sym/eval returns (in contrast to help eval):

eval  Evaluate a symbolic expression.
    eval(S) evaluates the character representation of the
    symbolic expression S in the caller's workspace.

Also using the MATLAB debugger points out that it is a different function. However, the full explanation mentions that it evaluates the character representation of the expression, which can also be seen in the source code:

s = evalin('caller',vectorize(map2mat(char(x))));

So, it uses internally evalin, which is similar to eval, to evaluate a string. This may not be very efficient.

So, we should avoid sym/eval too?

Maybe not, also double uses eval internally to evaluate a string:

Xstr = mupadmex('symobj::double', S.s, 0);
X = eval(Xstr);

The difference is that sym/eval uses eval (evalin) for the original character representation, i.e. the whole expression, whereas double uses it to parse the final result, i.e. the numerically evaluated value.

Conclusion: for your example double seems to be the appropriate method as it is twice as fast as using eval. However, for the following example eval is somewhat faster (~15%):

my_function(x) = cos(x);
for i=2:100
  my_function(x) = my_function(x) + cos(i*x);
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...