MATLAB: Is MATLAB running slow? 2013a vs 2011a

2011a2013arunning slowslow solving time

Hi everyone,
I am running an m-file in Matlab 2013a, which takes roughly 20sec to complete. However, when a friend runs the same code in 2011a it takes a fraction of a second to complete. What could be causing this in my 2013a installation?
Our PCs have the same components so that is not the culprit.
Just a note; the code I am testing is very simple, for the most part just for loops.
Thanks
clc; clear all; close all;
format compact
% Define number of cells in x and y
nx = 20;
ny = 20;
uw = 1;
% Define cell length in x and y
dx = 1.0/nx;
dy = 1.0/ny;
% Relaxation Parametres
relax1 = 0.2;
relax2 = 1.2;
relax3 = 0.2;
% Reynolds Number
re = 100;
nu = 1/re;
kappa = 1/re;
rex = 2.0/dx/dx + 2.0/dy/dy;
% Initialise the streamfunction and vorticity everywhere.
for i = 1:nx+1
for j = 1:ny+1
psi(i,j) = 0.0; % Streamfunction
w(i,j) = 0.0;
x(i,j) = (i-1)*dx;
y(i,j) = (j-1)*dy;
T(i,j) = 0.0;
if j == ny+1
T(i,j) = 1.0;
end
end;
end;
for k = 1:20000 %main loop - iterate until convergence.
adu = 0.0;
adv = 0.0;
psimin = 1000000.0;
psimax = -1000000.0;
for i = 2:nx %sweep through all internal points on the grid
for j = 2:ny %updating until convergence.
wimj0 = w(i-1,j); %for the vorticity equations these values
wipj0 = w(i+1,j); %may have to be modified at the points
wi0jm = w(i,j-1); %adjacent to the walls
wi0jp = w(i,j+1);
% Boundary Adjacent Conditions
if(i == 2)
wimj0 = -2.0*psi(i,j)/dx/dx;
end;
if(i == nx)
wipj0 = -2.0*psi(i,j)/dx/dx;
end;
if(j == 2)
wi0jm = -2.0*psi(i,j)/dy/dy;
end;
if(j == ny)
wi0jp = -2.0*(psi(i,j)+dy*uw)/dy/dy;
end;
%this is the update for w(i,j) from the FDE.
wij = (nu*((wipj0+wimj0)/dx/dx + (wi0jp+wi0jm)/dy/dy)...
+ (((psi(i+1,j)-psi(i-1,j))*(wi0jp-wi0jm)-((psi(i,j+1)...
-psi(i,j-1))*(wipj0-wimj0)))/(4*dx*dy)))/(nu*rex);
dw = wij - w(i,j);
%compute the difference between new and old value.
w(i,j) = w(i,j) + relax1*dw;
%update using relaxation
%update equation for the streamfunction
psiij = ((psi(i+1,j)+psi(i-1,j))/dx/dx + ...
(psi(i,j+1)+psi(i,j-1))/dy/dy + w(i,j))/rex;
dpsi = psiij - psi(i,j);
psi(i,j) = psi(i,j) + relax2*dpsi;
if(psi(i,j)>psimax) %determine maximum and minimum streamfunction
psimax = psi(i,j);
end;
if(psi(i,j)<psimin)
psimin = psi(i,j);
end;
ddu = abs(dpsi); %compute maximum change in solution over full sweep.
ddv = abs(dw);
if(ddu>adu)
adu = ddu;
end;
if(ddv>adv)
adv = ddv;
end;
end;
end;
% k
% psimax
% psimin
% adu
% adv
% Exit loop if converged
if(adu<1.0e-4 && adv<1.0e-4)
break;
end;
end;
% Compute the temperature distribution
for l = 1:10000
adT = 0.0;
for i = 1:nx+1
for j = 1:ny+1
if j ~= 1 && j ~= ny+1
if i == 1 % Using a forward difference scheme on the psi differential
Tij = (kappa*((T(i,j+1)+T(i,j-1))/dy/dy) + ...
(psi(i+1,j)-psi(i,j))*(T(i,j+1)-T(i,j-1))/(2*dx*dy))/(kappa*2/dy/dy);
elseif i == nx+1 % Using a backward difference scheme on the psi differential
Tij = (kappa*((T(i,j+1)+T(i,j-1))/dy/dy) + ...
(psi(i,j)-psi(i-1,j))*(T(i,j+1)-T(i,j-1))/(2*dx*dy))/(kappa*2/dy/dy);
else
Tij = (kappa*((T(i+1,j)+T(i-1,j))/dx/dx + ...
(T(i,j+1)+T(i,j-1))/dy/dy) + ...
(psi(i+1,j)-psi(i-1,j))*(T(i,j+1)-T(i,j-1))/(4*dx*dy)...
- (psi(i,j+1)-psi(i,j-1))*(T(i+1,j)-T(i-1,j))/(4*dx*dy))/(kappa*rex);
end;
dT = Tij - T(i,j);
T(i,j) = T(i,j) + dT*relax3;
ddT = abs(dT);
if(ddT>adT)
adT = ddT;
end;
end;
end;
end;
if adT < 1e-6
break
end
% l
% adT
end;
k
psimin
adu
adv
l
adT
% Plot solution.
for i = 1:10
v(i) = -(i-1)*0.01;
end;
for i = 11:20
v(i) = (i-10)*psimax/10;
end;
figure(1)
contourf(x,y,psi,v);
xlabel('x')
ylabel('y')
title('Streamfunction')
colorbar
figure(2)
contourf(x,y,T)
xlabel('x')
ylabel('y')
title('Temperature Distribution (\circC)')
colorbar

Best Answer

I tried your code on my machine in both R2011a and R2014a, and as posted it is indeed quite a bit slower in R2014a. You can get nearly all the speed back if you make your code a function rather than a script. See this page for more about functions if you're not already used to writing them.