Alternative code that runs much faster?
Show older comments
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 Comments
Torsten
on 21 Oct 2016
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
on 21 Oct 2016
Have you use the profiler to find out where most of the time is spent?
Steven Lord
on 21 Oct 2016
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.
Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!