Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

Technical Solutions

Why do I obtain different results using the same MATLAB code across different computers?


Date Last Modified: Friday, June 26, 2009
Solution ID:   1-7AST0R
Product:   MATLAB
Reported in Release:   No Release
Platform:   All Platforms
Operating System:   All OS
 

Subject:

Why do I obtain different results using the same MATLAB code across different computers?

Problem Description:

I am executing the same MATLAB code across different computers, however I am getting different results from each of them.

Solution:

Occasionally the same code may lead to different results when it is run across different computers. While this is not unexpected for numerical computing across platforms, and not unique to MATLAB code, it is also a rare occurrence.

To understand this we need to clarify a few things:

1. MATLAB and most other technical computing environments use floating- point arithmetic, which involves a finite set of numbers with finite precision. This methodology leads to phenomena like roundoff errors, underflow, or overflow.

2. Although floating point operations follow a standard (commonly IEEE 754-1985), they are not associative. Thus, based on the order in which operations are performed, one may end up with different results.

3. MATLAB uses BLAS (Basic Linear Algebra Subprograms) routines for performing all its linear algebra operations, which at the core are a set of floating point operations. For improved performance from these BLAS routines depending on the architecture of the computer being used, we can optimize these libraries further. One of the ways we can obtain improved performance is by changing the order of operations depending on the architecture of the computer. In most cases, we might not see any significant difference in the final results, but for certain problems which are very sensitive to even minute perturbations we might see different results due to these optimization routines.

With the above clarifications, different results across different platforms with the same MATLAB code can be attributed to problems that are very sensitive to even minute perturbations, the perturbations in this case coming from the differences in the optimization of the BLAS libraries across different platforms.

For more information on what BLAS is and how the version of BLAS MATLAB uses can be changed, see the Related Solution at the bottom of the page.

As an example consider the following example code:
T = gallery('tridiag', 100, 1+2i, 5, 1-2i);
A = T + (1i/10)*diag(1:100);
c = condeig(A); % Condition Number for the eigen values of A
APerturbed = A;

% Perturb the first entry of A by the smallest amount possible.

APerturbed(1) = APerturbed(1) + eps(APerturbed(1));
lam = eig(A);
mu = eig(APerturbed);
figure; plot(lam, '*'); hold on; plot(mu, 'r+');
legend('Eigenvalues of A', 'Eigenvalues of APerturbed');

In the above example, eigenvalues of A are very sensitive to even minute perturbations. This can be ascertained by the large condition number for the eigenvalues of A. As seen in Figure-1, a few of the eigenvalues of A are sensitive to even minute perturbations. We have introduced these minute perturbations artificially, but the source of perturbation can be the differences due to optimization of the BLAS libraries. Figure-2 illustrates the differences in eigenvalues of A due to perturbations introduced by different BLAS versions.

The core question one needs to answer in this situation is not how we can obtain consistent results across the different platforms, but rather look at options that would make the problem robust against these perturbations. This entirely is dependent on the problem being solved.

Further Readings:

Floating point Arithmetic

http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf

Numeric Classes in MATLAB

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f2-12135.html

 

Related Documents/Files:

 

Related Solutions:

Please provide feedback to help us improve this Solution
Contact support
E-mail this page
Print this page