Alternative code that runs much faster?

Hi,
We built this code:
A = zeros(n,y);
A (:,1) = -3;
x1 = zeros(n,y);
x2 = zeros(n,y);
x3 = zeros(n,y);
B = zeros(n,y);
B (:,1) = 0.3;
Z = zeros(n,y);
Z (:,1) = 0;
for k=1:n
for j=2:y
max_index=j-1;
Powers_function=(0:max_index);
A (k,j) = C(k,j-1)*B(k,j-1) + D(k,j-1) - E (k,j-1);
f = @(x) sum(A(k,1:j)./(1+x).^Powers_function(1,1:j));
Z(k,j) = fzero(f,0,optimset('display','off'));
Z(isnan(Z)) = 0;
Z((Z<0)) = 0;
if Z (k,j)<0.05
x1(k,j-1) = 0.60;
else
x1(k,j-1) = 0;
end
if (Z(k,j) >= 0.05) && (Z(k,j) <= 0.20)
x2(k,j-1) = 0.60 - (Z(k,j)/0.35);
else
x2(k,j-1) = 0;
end
if Z(k,j) > 0.20
x3(k,j-1) = 0.45;
else
x3(k,j-1) = 0;
end
B(k,j) = max(x1(k,j-1),max(x2(k,j-1),x3(k,j-1)));
end
end
This is running for a long long time for n=10000 and y=28.
Isn't there an alternative code to compute the same in much less time?
Thanks,

댓글 수: 3

Torsten
Torsten 2016년 10월 21일
You are aware that f can have "max_index" distinct zeros, not only the one that fzero returns when calling it with an initial guess of 0 ?
Best wishes
Torsten.
Thorsten
Thorsten 2016년 10월 21일
Have you use the profiler to find out where most of the time is spent?
Steven Lord
Steven Lord 2016년 10월 21일
Can you describe in words (NOT equations or code) what problem you're trying to solve using this code? Perhaps knowing the problem will help people suggest a more efficient solution.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2016년 10월 21일

댓글:

2016년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by