Note that there are some explanatory texts on larger screens.

plurals
  1. POMatlab's FOR loop iteration time increases
    text
    copied!<p>I'm trying to span values of a certain multivariable function <em>f(nu,delta,Omega,kappa,Gamma)</em>. The code is as follows:</p> <pre><code>% PREALLOCATE RESULT MATRIX N = 10; d = 0.1; M = zeros(4*N/d+(2*N+1)/d,6); % SET UP LOOP INDICES i=1; increm=1; % LOOP OVER MULTIPLE VARIABLE for nu=d:d:N for delta=-N:d:N for Omega=d:d:N for kappa=d:d:N tic for Gamma=d:d:N % CALCULATE THE FUNCTION mss = ((Gamma+kappa).*((Gamma+kappa).^2+4.*(delta+(-1).*nu).^2).^(-1)+( ... -1).*(Gamma+kappa).*((Gamma+kappa).^2+4.*(delta+nu).^2).^(-1)+( ... kappa.^2+4.*(delta+(-1).*nu).^2).^(-1).*(((2.*Gamma+kappa).^2+4.*( ... delta+(-1).*nu).^2).*(kappa.*(Gamma+kappa).*(2.*Gamma+kappa)+4.*(( ... -1).*Gamma+kappa).*(delta+(-1).*nu).^2)+4.*(kappa.*(2.*Gamma+ ... kappa).^2+4.*(4.*Gamma+kappa).*(delta+(-1).*nu).^2).*Omega.^2).*( ... 16.*(delta+(-1).*nu).^4+4.*(delta+(-1).*nu).^2.*((Gamma+kappa).^2+ ... (2.*Gamma+kappa).^2+(-8).*Omega.^2)+((Gamma+kappa).*(2.*Gamma+ ... kappa)+4.*Omega.^2).^2).^(-1)+(-1).*(kappa.^2+4.*(delta+nu).^2).^( ... -1).*(((2.*Gamma+kappa).^2+4.*(delta+nu).^2).*(kappa.*(Gamma+ ... kappa).*(2.*Gamma+kappa)+4.*((-1).*Gamma+kappa).*(delta+nu).^2)+ ... 4.*(kappa.*(2.*Gamma+kappa).^2+4.*(4.*Gamma+kappa).*(delta+nu).^2) ... .*Omega.^2).*(16.*(delta+nu).^4+4.*(delta+nu).^2.*((Gamma+kappa) ... .^2+(2.*Gamma+kappa).^2+(-8).*Omega.^2)+((Gamma+kappa).*(2.*Gamma+ ... kappa)+4.*Omega.^2).^2).^(-1)).^(-1).*((Gamma+kappa).*((Gamma+ ... kappa).^2+4.*(delta+nu).^2).^(-1)+(kappa.^2+4.*(delta+nu).^2).^( ... -1).*(((2.*Gamma+kappa).^2+4.*(delta+nu).^2).*(kappa.*(Gamma+ ... kappa).*(2.*Gamma+kappa)+4.*((-1).*Gamma+kappa).*(delta+nu).^2)+ ... 4.*(kappa.*(2.*Gamma+kappa).^2+4.*(4.*Gamma+kappa).*(delta+nu).^2) ... .*Omega.^2).*(16.*(delta+nu).^4+4.*(delta+nu).^2.*((Gamma+kappa) ... .^2+(2.*Gamma+kappa).^2+(-8).*Omega.^2)+((Gamma+kappa).*(2.*Gamma+ ... kappa)+4.*Omega.^2).^2).^(-1)); % STORE THE RESULT M(i,:) = [mss nu delta Omega kappa Gamma]; i = i+increm; end end toc end end end save M </code></pre> <p>However the preallocation does not help that each iteration takes longer time. When I run the code and interrupt it prematurely, the iterations take</p> <pre><code>Elapsed time is 0.003354 seconds. Elapsed time is 0.006374 seconds. Elapsed time is 0.009043 seconds. Elapsed time is 0.012092 seconds. Elapsed time is 0.015287 seconds. Elapsed time is 0.019239 seconds. Elapsed time is 0.023898 seconds. Elapsed time is 0.035345 seconds. Elapsed time is 0.046675 seconds. Elapsed time is 0.056000 seconds. Elapsed time is 0.066323 seconds. Elapsed time is 0.072178 seconds. Elapsed time is 0.075174 seconds. Elapsed time is 0.081095 seconds. Elapsed time is 0.095016 seconds. Elapsed time is 0.095214 seconds. Elapsed time is 0.100089 seconds. Elapsed time is 0.104286 seconds. Elapsed time is 0.109454 seconds. Elapsed time is 0.115368 seconds. Elapsed time is 0.124278 seconds. Elapsed time is 0.131521 seconds. Elapsed time is 0.135023 seconds. Elapsed time is 0.137370 seconds. Elapsed time is 0.145331 seconds. Elapsed time is 0.163449 seconds. Elapsed time is 0.162654 seconds. Elapsed time is 0.159628 seconds. Elapsed time is 0.166585 seconds. </code></pre> <p>I don't see how the change of variables itself could cause this, because the only vary by amount <em>d</em>, which shouldn't make it much harder to calculate the new value of mss than in the previous interation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload