Asked by Chandrakanth
on 9 May 2013

I have seen questions and answers posted earlier on why results vary with different versions of OS and libraries, but my problem is peculiar: it has got only iterative operations involving simple additions and multiplications. Code shown below. It involves randn, but I found all 3 versions producing the same random vector. Time taken to run also hugely differs although computer specs are comparable.

alpha = 0.05;beta = -1; delta = 1;gama=1;sigma = sqrt(.1); xzero = .1;yzero =.1;zzero =0; T = 2000; dt = 0.002; N = T/dt; randn('seed',66239); % use this for same set of random values. Nsamp = 1; dW = sqrt(dt)*randn(Nsamp,N); %Generate array of Brownian movments X = zeros(N+1,1);Y = zeros(N+1,1);Z = zeros(N+1,1); X(1) = xzero;Y(1) = yzero;Z(1) = zzero; for j =1:N Winc(j) = dW(:,j); X(j+1)=X(j)+Y(j)*dt; Y(j+1)=Y(j)+(-alpha*Y(j)-beta*X(j)-delta*X(j)^3+Z(j))*dt; Z(j+1)=Z(j)-gama*Z(j)*dt+sigma*Winc(j)+0.5*sigma^2*(Winc(j).^2-dt); end tt = [0:dt:T]; index = fix(0.5*N); DISP = sum(X(index:end,:).^2)/length(X(index:end,:)) VEL = sum(Y(index:end,:).^2)/length(Y(index:end,:))

On Computer 1 (MACI64,2.5Ghz i5, R2012b):

>> tic, EM_Duffing_Revised, toc DISP = 0.8953 VEL = 0.4929 Elapsed time is 1.767654 seconds.

On Computer 2: (MACI64, 2.4Ghz Core2Duo, R2010a):

>> tic, EM_Duffing_Revised, toc DISP = 0.92872 VEL = 0.50405 Elapsed time is 7375.1905 seconds.

On Computer 3: (PCWIN, 2.67Ghz Core-i5, R2010a):

>> EM_Duffing_Revised DISP = 0.8645 VEL = 0.3431

(time taken is ~ 2700 seconds).

I have another piece of code which does the same thing in a different way (with matrix calcs) and even that one has this weird behaviour. Firstly, what explains the difference in results. Secondly, how come the time taken differs so much. No way Comp1 is 1300 times faster than Comp3. Am I missing something very basic ? Thanks.

Answer by Philip Borghesani
on 9 May 2013

The performance difference is due to an optimization for array growth made in R2011a. Adding

Winc=zeros(N,1);

Before the for loop makes all MATLAB versions have similar times.

I am not sure about the different results. I ran a few versions on Windows and got the same answer posted here for Windows machines each time.

Answer by Matt J
on 9 May 2013

Edited by Matt J
on 9 May 2013

Just some guesses. First, what does the profiler say? Second, you should try changing

Winc(j) = dW(:,j);

to Winc(j) = dW(j) to see if the index parsing accounts for the differences in speed. Index parsing has changed drastically in performance between different versions of MATLAB. The line as you have it will produce an error anyway when Nsamp>1 and needs to be fixed. Better still would be to remove it from the loop altogether and just do Winc=dW.

As for the difference in results, did you verify by direct inspection that dW was the same on all machines? Aside from that, it is not clear how numerically stable this will be

Y(j+1)=Y(j)+(-alpha*Y(j)-beta*X(j)-delta*X(j)^3+Z(j))*dt; Z(j+1)=Z(j)-gama*Z(j)*dt+sigma*Winc(j)+0.5*sigma^2*(Winc(j).^2-dt)

If it's some kind of inverse filter, it could be a very singular inversion and very insensitive to round-off errors. What you call "simple additions and multiplications" are not really all that simple. Even addition is not numerically stable unless all the operands are the same sign.

Chandrakanth
on 9 May 2013

I directly compared dW from all machines. They were the same. >> any(dW-dWother) ans = 0

Stability has been my best guess, but I have little knowledge of the dynamics of the system to verify whether or not its stable.

Opportunities for recent engineering grads.

## 3 Comments

## John Doe (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75174#comment_148098

FYI: Win8, 1.70GHz, Core-i5, R2013a:

## Jordan Monthei (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75174#comment_148100

I can't tell you why the numbers are off on each one, but as MATLAB upgrades its software, they tend to optimize performance which would explain why your fastest time is from the most recent update.

A similar question was raised earlier about whether updates ran faster http://www.mathworks.com/matlabcentral/newsreader/view_thread/323753

## Chandrakanth (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/75174#comment_148102

Thats what perplexes me. My Comp1 does it in 1.7s and comp2 in 7300s. Anyways, what answer do you get. What os are you on?