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 个评论

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.
Have you use the profiler to find out where most of the time is spent?
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