1
votes

I have a problem in numerical evaluation of symbolic expression. To simplify my question. I write the follow lines for an example.

syms a b c d e f
x = [a+b*c+d^e,b+log(c+d);exp(c)*b/c+f,f*c+e^2];

a = 2;
b = 3;
c = 1.5;
d = 1;
e = -2;
f = -1;

fx = eval(x);

x is a matrix the elements of which are symbolic expressions. I need to evaluate x given different values of [a,b,c,d,e,f] to get a numerical matrix fx. In my real program, such evaluation needs to be implemented million times and both the size of matrix x and the number of parameters (a,b,...,d) are much larger.

In Matlab 7.1, my program runs well. However, when I run it in Matlab 2013b, it becomes quite slow. I find that "eval(x)" runs faster in 7.1 than in 2013b due to their difference in default symbolic computation packages (Maple for 7.1 and MuPAD for 2013b). So my question is that is there any more efficient way to evaluate a symbolic expression than using function "eval" in Matlab 2013b?

1

1 Answers

0
votes

For symbolic computation, I recommend using subs() instead of eval(). You get full power and symbolic accuracy if you use subs().
When you use eval(), the symbolic expression would be converted to a character string (such as is displayed) and then the MATLAB parser and full runtime would be applied to calculate the meaning of the character string, using normal MATLAB double precision (or appropriate MATLAB numeric type). This process involves MATLAB using its JIT analyzer and so on. It is a bit "heavy handed" computationally.
See the MATLAB documentation here for subs().