How to speed up calculations of integral and summation

11 visualizaciones (últimos 30 días)
Yuriy Yerin
Yuriy Yerin el 29 de Jun. de 2018
Comentada: Jeff Miller el 1 de Jul. de 2018
Hello everybody
Regarding to my previous question I'd like to speed up calculations
function z=self_energy_summation
tic
ww=linspace(0,10,100);
for l=1:100
S(l)=integral(@(q)Gamma_0(q,ww(l)),0,10,'AbsTol',1e-6,'RelTol',1e-3,'ArrayValued',true);
end
plot(ww,S)
function y1=Gamma_0(q,w)
z2=load('Tc_Tmatrix_ordered.txt');
tt=z2(741,1);
T_c=z2(741,2);
mu=z2(741,3);
k=1;
N=-1000:1000;
nn=10^4;
fun1=@(a,q) a.*tanh((a.^2-mu)./(2*T_c)).*log((2*a.^2+2*a.*q+q.^2-2*mu-1i*2*pi*N*T_c)./(2*a.^2-2*a.*q+q.^2-2*mu-1i*2*pi*N*T_c))./q-2;
R= T_c*q./k.*log((-k.^2+2*k.*q-q.^2+mu+1i.*(2*pi*N*T_c-w))./(-k.^2-2*k.*q-q.^2+mu+1i.*(2*pi*N*T_c-w))).*1./((tt+integral(@(a)fun1(a,q),0,nn,'AbsTol',1e-6,'RelTol',1e-3,'ArrayValued',true)));
y1=sum(R);
end
Time = toc
end
On the first step I reduced the relative tolerance of integral calculations. But even on my computer with 24 Gb RAM and Intel I7 processor I can't finish these calculations during the 24 hours. My question: is it possible to improve the performance of this code and reduced the time?

Respuesta aceptada

Jeff Miller
Jeff Miller el 30 de Jun. de 2018
It seems wasteful to do this
z2=load('Tc_Tmatrix_ordered.txt');
inside the function that you are integrating. Can't you load z2 just once inside self_energy_summation? The nest functions will have access to it.
  2 comentarios
Yuriy Yerin
Yuriy Yerin el 1 de Jul. de 2018
A lot of thanks. Now it works much faster. I apologize, could you explain briefly the origin of the slowdown of my code?
Jeff Miller
Jeff Miller el 1 de Jul. de 2018
Good that it is faster. The reason is this: MATLAB's integral function calls your Gamma_0 function many times (look for an explanation of how numerical integration works). Loading the file inside Gamma_0 means that your function has to load the file again every time it is called, which wastes a lot of time because loading a file is a relatively slow operation.
There is also the fact that you are doing the integration 100 times, and that multiplies by 100 the number of times that the file is loaded. Loading the file before starting the for loop could easily save 999 repetitions of the file loading step, maybe more depending on your function.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by