How to reduce computation time
이전 댓글 표시
Hi,
The function "funct" takes up almost 0.06 seconds and it is the first responsible for a slow execution of an integral (more than 3 hours) when it is used in it with regards to y.
Any help please to reduce the computation time of the following function
function [out] = funct(mu,lmax,wmax,hmax,lambdab,hu,hb,y,x)
nx=length(x);
out=zeros(1,nx);
A=(mu./pi).*(wmax+lmax);
B=(mu.*wmax.*lmax)./4;
C1=(hb-(2.*hmax))./(2*hmax);
C2=(hu+hb-(2.*hmax))./(2*hmax);
for i=1:nx
S1=@(u,theta) u.*(1-exp(C1.*((A.*sqrt(u.^2+x(i).^2-(2.*x(i).*u.*cos(theta))))+B)));
S2=@(theta) (1-exp(C1.*((A.*sqrt(y.^2+x(i).^2-(2.*x(i).*y.*cos(theta))))+B)));
out(i)=lambdab.*y.*integral(S2,0,2*pi,'ArrayValued',true).*exp(-lambdab.*quad2d(S1,0,y,0,2*pi))./(1-exp(-lambdab.*quad2d(S1,0,1000,0,2*pi)));
end
end
댓글 수: 4
per isakson
2020년 5월 24일
Help us to help by providing a typical set of input data for funct so we can run the profiler.
Yassine Hmamouche
2020년 5월 24일
편집: Yassine Hmamouche
2020년 5월 24일
Walter Roberson
2020년 5월 25일
On my system, I time as closer to 0.012 .
Yassine Hmamouche
2020년 5월 25일
채택된 답변
추가 답변 (1개)
per isakson
2020년 5월 24일
편집: per isakson
2020년 5월 25일
On R2018b and a new vanilla PC I found a significant decrease in computation time by replacing quad2d() by integral2() (with the same signature, i.e. defaults are used). With your input data this replacement didn't affect the result, out. (Caveat, I do this because I'm curious, not because I know much about the topic.) There are two posts on Double Integration in MATLAB ( and here ) at Loren on the Art of MATLAB. I don't find backing for the difference in speed that I've seen, which makes me a bit nervous. What's the catch? There ought to exist a discussion on how to chose between the two functions, but I fail to find it.
Have you tried to simplify the expressions with the help of the Symbolic Toolbox? See https://se.mathworks.com/matlabcentral/answers/33399-problem-with-double-integral-dblquad-and-quad2d#answer_42126 it's a bit old but shows the idea. Maybe that toolbox sees something that I don't see.
In response to comments
The difference in speed between integral2(), introduced in R2012a, and quad2d(), introduced in R2009a, that I see is irking.
I've compared your function, funct(), to funct_int2(), in which I replaced quad2d() by integral2(). (And split the long statement into three to make the code and the result of profile() easier to read.)
>> cssm % first call to cssm after starting Matlab
Elapsed time is 0.364935 seconds.
Elapsed time is 0.140186 seconds.
0
>> cssm
Elapsed time is 0.104307 seconds.
Elapsed time is 0.021930 seconds.
0
>> cssm
Elapsed time is 0.088423 seconds.
Elapsed time is 0.016356 seconds.
0
...
...
>> cssm
Elapsed time is 0.091209 seconds.
Elapsed time is 0.017180 seconds.
0
where cssm.m is a script containing
%%
mu=1; lmax=4; wmax=6; hmax=30; lambdab=1; hu=40; hb=10; y=4; x=2;
%%
tic
for jj = 1 : 10
out1 = funct(mu,lmax,wmax,hmax,lambdab,hu,hb,y,x);
end
toc
%%
tic
for jj = 1 : 10
out2 = funct_int2(mu,lmax,wmax,hmax,lambdab,hu,hb,y,x);
end
toc
disp( out2-out1 )
and
function [out] = funct_int2(mu,lmax,wmax,hmax,lambdab,hu,hb,y,x)
nx=length(x);
out=zeros(1,nx);
A=(mu./pi).*(wmax+lmax);
B=(mu.*wmax.*lmax)./4;
C1=(hb-(2.*hmax))./(2*hmax);
C2=(hu+hb-(2.*hmax))./(2*hmax); %#ok<NASGU>
for ii=1:nx
S1=@(u,theta) u.*(1-exp(C1.*((A.*sqrt(u.^2+x(ii).^2-(2.*x(ii).*u.*cos(theta))))+B)));
S2=@(theta) (1-exp(C1.*((A.*sqrt(y.^2+x(ii).^2-(2.*x(ii).*y.*cos(theta))))+B)));
f1 = lambdab.*y.*integral(S2,0,2*pi,'ArrayValued',true);
f2 = exp(-lambdab.*integral2(S1,0,y,0,2*pi));
f3 = (1-exp(-lambdab.*integral2(S1,0,1000,0,2*pi)));
out(ii) = f1.*f2./f3;
end
end
Comments
- Fine print; don't blame me for mistakes.
- In this comparison both integral2() and quad2d() use the "tiled method"
- The fact that the two return exactly the same result makes me believe that deep inside the same calculation is made.
댓글 수: 7
Yassine Hmamouche
2020년 5월 24일
Walter Roberson
2020년 5월 24일
편집: Walter Roberson
2020년 5월 24일
Vectorized loop would move from a 2d integral to 3d, but there is no quad3d. Also the integral() would have to be integral2() but integral2() does not have ArrayValued
Yassine Hmamouche
2020년 5월 24일
per isakson
2020년 5월 25일
@Yassine, which release of Matlab are you using?
I've added to my answer.
Yassine Hmamouche
2020년 5월 25일
per isakson
2020년 5월 25일
@Yassine Hmamouche, Then you have integral2() and should be able to rum "my" funct_int2. See integral2, Numerically evaluate double integral. I'm curious to hear whether you can reproduce my result.
Yassine Hmamouche
2020년 5월 25일
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
