Alternative code that runs much faster?

3 次查看(过去 30 天)
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 个评论
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 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by