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.
Mostrar comentarios más antiguos
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 comentario
Shawn Miller
el 28 de Feb. de 2016
Editada: Shawn Miller
el 28 de Feb. de 2016
Respuestas (1)
Walter Roberson
el 29 de Feb. de 2016
0 votos
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 comentario
Shawn Miller
el 29 de Feb. de 2016
Editada: Shawn Miller
el 29 de Feb. de 2016
Categorías
Más información sobre Code Performance en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!