Same code, Different results on 3 computers - only simple multiplications and additions involved
8 次查看(过去 30 天)
显示 更早的评论
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.
3 个评论
Jordan Monthei
2013-5-9
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
回答(2 个)
Philip Borghesani
2013-5-9
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.
0 个评论
Matt J
2013-5-9
编辑:Matt J
2013-5-9
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!