Is there a method of significant preallocation beyond zeros?

1 view (last 30 days)
I have been calling a function with 83000 iterations (as below):
>>[t1data,t2data,Tdata,Ddata,lamdata] = example_run(2,0.1,25,1.01)
Yet I have let it run for the better part of a day without the program terminating. Is there a way (beyond preallocation with zeros, as I have already done) to decrease the run time significantly?
function [t1data,t2data,Tdata,Ddata,lamdata] = example_run(E0,sigma,F,tau)
%E0=2;sigma=0.1;F=25;tau=1.01; standard reference
%preallocation
Tdata = zeros(1, 100000);
t1data = zeros(1, 100000);
t2data = zeros(1, 100000);
kdata = zeros(1, 100000);
Ddata = zeros(1, 100000);
lamdata = zeros(1, 100000);
[Tc,Ts]=findTcTs(E0,sigma,F,tau);
%T=Ts+0.5;
%[t1,t2,lambda,D]=findt1t2lambdaD(E0,sigma,F,tau,T,Tc);
hold on;
T = Ts;
T0 = Tc;
for k = 1:83001
[t1,t2,lambda,D] = findt1t2lambdaD(E0,sigma,F,tau,T,T0);
Tdata = [Tdata,T];
t1data = [t1data,t1];
t2data = [t2data,t2];
kdata = [kdata,k];
Ddata = [Ddata,D];
lamdata = [lamdata, lambda];
T0 = t2;
T = T + 0.1;
plot(T,real(D), 'o')
%iterate by increments to use valid initial guess for t2 for each increment in findt1t2...
end
hold off;

Accepted Answer

Walter Roberson
Walter Roberson on 24 Mar 2018
Tdata = [Tdata,T];
Don't do that. You already used
Tdata = zeros(1, 100000);
so each iteration you are expanding Tdata by putting a single new element after all of the zeros. That is not preallocation! Preallocation would be if you had used
Tdata = zeros(1, 100000);
with
Tdata(k) = T;
(It is not obvious why your 100000 does not match your 83001)

More Answers (0)

Categories

Find more on Geographic Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!