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.
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.
14 views (last 30 days)
Show older comments
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/156230/image.png)
I wonder how can I save more time from "mupadmex" (seems this is the root of the problem).
Answers (1)
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 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.
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!