How to speed up calculations of integral and summation

조회 수: 9 (최근 30일)
Yuriy Yerin
Yuriy Yerin 2018년 6월 29일
댓글: Jeff Miller 2018년 7월 1일
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?

채택된 답변

Jeff Miller
Jeff Miller 2018년 6월 30일
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
Yuriy Yerin
Yuriy Yerin 2018년 7월 1일
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 2018년 7월 1일
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.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by