Clear Filters
Clear Filters

How can I improve my code as below? I constructs an operator and repetitively call this operator, then I store the n times results in a vector.

9 views (last 30 days)
My code now suffers from slow speed when g is a complex function or when I specify n as a relatively large integer.
function A=Aoperator(g,n)
% Aoperator() returns a symbolic expression matrix
% Input: 'g' is a symbolic expression/function where the operation is
% applied; 'n' is the times of operations applied at most
% Output: 'A' is a symbolic expression matrix where A(g), A(A(g)) ... are
% stored.
syms x a b c;
assume([x a b c],'real');
uY=a*(b-x)/(c*sqrt(x))-c/(4*sqrt(x));
% Operate differential operator from 1 to n times
A=sym(zeros(n,1));
A(1)=uY*diff(g,x,1)+diff(g,x,2)/2;
A(1)=simplify(A(1));
for i=2:n
A(i)=uY*diff(A(i-1),x,1)+diff(A(i-1),x,2)/2;
A(i)=simplify(A(i));
end
end
For example, the runtime for this test script is about 37 seconds, its runtime profile is as follows,
syms x;
g=(5*x^5+3*x^2+x+1)*exp(x);
Aoperator(g,6)
I wonder how can I save more time from "mupadmex" (seems this is the root of the problem).
  1 Comment
Shawn Miller
Shawn Miller on 28 Feb 2016
Edited: Shawn Miller on 28 Feb 2016
Note that in the code I defined uY, a symbolic expression. This is a term in the operator. Also, for symbols a, b, c, they are parameters used in my other program. The code I posted here is one function I use in a large project. So please accept these symbols, they are just how my differential operator is defined.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 29 Feb 2016
mupadmex is the code that implements the symbolic engine. To spend less time in there, you would need expressions that are more efficient within the symbolic engine itself. Also there is the potential for call overhead to be slowing you down.
You might want to experiment with using feval(symengine) to use the MuPAD @@ operator
You should also experiment with whether it is worth while to simplify() on every iteration; it might be better to only invoke it every few iterations or perhaps only once at the end.
You should also consider using alternative simplification
Also you might think about putting a time limit on the simplification; http://www.mathworks.com/help/symbolic/simplify.html#input_argument_namevalue_seconds
  1 Comment
Shawn Miller
Shawn Miller on 29 Feb 2016
Edited: Shawn Miller on 29 Feb 2016
I wonder what is MuPAD after all. I heard MATLAB uses Maple kernel for its symbolic algebra. Is MuPAD something used by Maple? Also, what do you mean by " Also there is the potential for call overhead to be slowing you down."
In terms of simplify(), I decide to use command:
simplify(f, 'Steps',n, 'Seconds', m, 'IgnoreAnalyticConstraints', true, 'Criterion', 'preferReal').
But I guess I also need to test myself for the best m and n values using "Run and Time" option, right?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!