How to make the below code with Symbolic variables run faster

2 visualizaciones (últimos 30 días)
Fan Li
Fan Li el 5 de Dic. de 2018
Comentada: Walter Roberson el 5 de Dic. de 2018
Dear matlab users
I have below code with Symbolic variables to evaluate a function. The a_n,b_n,c_n and d_n are numerical arrays which are calculated by a code which is not shown. And the variable "rho_interval" and "z_interval" are the numerical arrays as well. The most time consuming part is the loop and two matlabFunction. Are there any ways to make it run faster?
Thanks
syms zeta mu rho z eta
zeta=atanh(2*c.*z./(z.^2+rho.^2+c.^2));
% sin(eta)=(rho/z)*sinh(zeta);
cos(eta)=cosh(zeta)-(c/z)*sinh(zeta);
mu=cos(eta);
for n=0:N
poly_syms(n+1,:) =(a_n(n+1,:)*cosh((n-1/2)*zeta)...
+b_n(n+1,:)*sinh((n-1/2)*zeta)...
+c_n(n+1,:)*cosh((n+3/2)*zeta)...
+d_n(n+1,:)*sinh((n+3/2)*zeta))...
.*gegenbauerC(n+1,-1/2,mu);
end
poly_syms_sum=sum(poly_syms);
str_func_syms=(cosh(zeta)-mu).^(-3/2)*poly_syms_sum;
poly_func=matlabFunction(poly_syms);
str_func_func=matlabFunction(str_func_syms);
[domain_rho,domain_z]=meshgrid(rho_interval,z_interval);
str_func=str_func_func(domain_rho,domain_z);
  1 comentario
Walter Roberson
Walter Roberson el 5 de Dic. de 2018
The assignment can be vectorized. Instead of a for I suspect you can use
n = 0:N;
poly_syms = (a_n .* cosh((n-1/2)*zeta)...
+ b_n .* sinh((n-1/2)*zeta)...
+ c_n .* cosh((n+3/2)*zeta)...
+ d_n .* sinh((n+3/2)*zeta))...
.* gegenbauerC(n+1,-1/2,mu);

Iniciar sesión para comentar.

Respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by