accelerating code in matlab - for loop

조회 수: 1 (최근 30일)
Sara
Sara 2012년 7월 5일
Hi Guys,
int = zeros(1,length(x));
if k <= 0
int = x ;
return
end
for n = 3:length(x)
y1 = x(2:n) ;
t1 = ((n-2):-1:0)*dt ;
y2 = x(1:n-1) ;
t2 = t1 +dt;
int(n) = sum (t1.^(k-1)/factorial(k-1).*y1 + t2.^(k-1)/factorial(k-1).*y2)*dt/2 ;
end
is there any suggestion to write this part of code more optimal? It is used for computing Integration. The most time consuming line is int(n)= ... Whether I can change and remove the for loop here, and implement the for functionality in another way, e.g. vectors? Any suggestions would be appreciated ...
  댓글 수: 2
Ryan
Ryan 2012년 7월 5일
for n = 3:length(x )
y1 = x(2:n) ;
t1 = ((n-2):-1:0)*dt ;
y2 = x(1:n-1) ;
t2 = t1 +dt;
int(n) = sum (t1.^(k-1)/factorial(k-1).*y1 + t2.^(k-1)/factorial(k-...
1).*y2)*dt/2 ;
end
Jan
Jan 2012년 7월 11일
@Sara: You can delete your question using the "Delete" button left beside the question. If such a button does not appear although you are logged in, please contact an editor or files@mathworks.com and ask for deleteing the thread. Duplicate posts waste the time of the ones, who want to help you.

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

채택된 답변

Jan
Jan 2012년 7월 11일
At first I've clean up your code a bit:
if k <= 0
int = x ;
return
end
int = zeros(1, length(x));
fk1 = factorial(k - 1);
dt2 = dt / 2;
for n = 3:length(x)
y1 = x(2:n);
t1 = ((n-2):-1:0) * dt;
y2 = x(1:n-1);
t2 = t1 + dt;
int(n) = sum(t1 .^ (k-1) / fk1 .* y1 + t2 .^ (k-1) / fk1 .* y2) * dt2;
end
Now some improvements:
lenx = length(x);
fk1 = factorial(k - 1);
t1Vec = (((lenx - 2):-1:0) * dt) .^ (k-1) * dt / fk1;
int = zeros(1, lenx);
dt2 = dt / 2;
for n = 3:lenx
y1 = x(2:n);
t1 = t1vec(lenx-a-n:lenx-b); % **see comment**
y2 = x(1:n-1);
t2 = t1 + dt;
int(n) = sum(t1 .* y1 + t2 .^ (k-1) / fk1 .* y2) * dt2;
end
I do not have the time to find the right constantd for a and b. Without access to Matlab I cannot simply try it, but you can. The idea is to avoid the repeated expensive calculation of t1 .^(k-1), when all elements except for one have been processed already. The same works for t2. I leave it up to you to elaborate this.
  댓글 수: 3
Sara
Sara 2012년 7월 11일
t1Vec = (((lenx - 2):-1:0) * dt) .^ (k-1) * dt / fk1;
the second dt is required where .. (k-1)times dt...
Jan
Jan 2012년 7월 11일
I assume this is correct, Sara. "a" and "b" are used only to replicate the limits of t1 = ((n-2):-1:0) * dt.

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

추가 답변 (1개)

Matt Kindig
Matt Kindig 2012년 7월 5일
Could you please format your code properly? It is difficult to tell which lines are commented, etc. Please edit your question to include proper line breaks, and then apply the 'code' tag (see the {} Code icon in the editor window).
Also, as a first observation, you should pre-allocate your variable 'int'--that is the reason that it is taking so long. Prior to the loop, add this line:
int = NaN(length(x), 1);
I think that this change alone will substantially improve your performance.
  댓글 수: 2
Jan
Jan 2012년 7월 11일
int is pre-allocated by zeros already.
Matt Kindig
Matt Kindig 2012년 7월 12일
Ah yes, I think I missed that before.

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

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by