How to speed up calculations of integral and summation
4 views (last 30 days)
Show older comments
Hello everybody
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?
0 Comments
Accepted Answer
Jeff Miller
on 30 Jun 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 Comments
Jeff Miller
on 1 Jul 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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!