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

点点圈 提交于 2019-11-28 02:23:57

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!