How to make the below code with Symbolic variables run faster

조회 수: 2 (최근 30일)
Fan Li
Fan Li 2018년 12월 5일
댓글: Walter Roberson 2018년 12월 5일
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
Walter Roberson
Walter Roberson 2018년 12월 5일
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);

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by