Can't run code because of sparse indexing expression is likely to be slow

Dear Matlab users,
I'm a simulink user and since a couple of months i'm trying to understand matlab as it's a powerfull tool in solving differential equations.
So as a test of my skills i wrote a testcode to simulate the hot and cold fluids of a CF HX.
I wrote the code in notepad first because of the non-availabilty of Matlab on my laptop.
I ran the code on a pc with Matlab 2011 and i got the sparse indexing expression is slow warning.
I'm open for suggestions regarding this code
clear all;
m_dot_H=0.05; % hot-side mass flow rate (kg/s)
m_dot_C=0.05; % cold-side mass flow rate (kg/s)
c_H=1005; % hot-side specific heat capacity (J/kg-K)
c_C=1005; % cold-side specific heat capacity (J/kg-K)
T_H_in=400; % hot-side inlet temperature (K)
T_C_in=300; % cold-side inlet temperature (K)
UA=200; % conductance (W/K)
M=20; % number of grids in the cold-flow direction, x (-)
N=20; % number of grids in the hot-flow direction, y (-)
% grid locations for hot-side temperatures
for i=1:M
for j=1:(N+1)
x_H(i,j)=(i-1/2)/M;
y_H(i,j)=(j-1)/N;
end
end
% grid locations for cold-side temperatures
for i=1:(M+1)
for j=1:N
x_C(i,j)=(i-1)/M;
y_C(i,j)=(j-1/2)/N;
end
end
% initialize matrices
A=spalloc(M*N+2*M+1, M*N+2*M+1, 4*M*N+M*(2*N+2)+2);
b=zeros(M*N+2*M+1,1);
% hot-side energy balances
for i=1:M
for j=1:N
A((j-1)*M+i,(j-1)*M+i)=m_dot_H*c_H/M-UA/(M*N*2);
A((j-1)*M+i,(j+1-1)*M+i)=-m_dot_H*c_H/M-UA/(M*N*2);
A((j-1)*M+i,(N+1)*M+i)=UA/(M*N*2);
A((j-1)*M+i,(N+1)*M+i+1)=UA/(M*N*2);
end
end
% cold-side energy balances
for i=1:M
A(M*N+i,(N+1)*M+i+1)=m_dot_C*c_C+UA/(2*M);
A(M*N+i,(N+1)*M+i)=-m_dot_C*c_C+UA/(2*M);
A(M*N+i,(1-1)*M+i)=-UA/(2*M*N);
for j=2:N
A(M*N+i,(j-1)*M+i)=-UA/(M*N);
end
A(M*N+i,(N+1-1)*M+i)=-UA/(2*M*N);
end
% hot-side inlet fluid temperature boundary condition
for i=1:M
A(M*N+M+i,(1-1)*M+i)=1;
b(M*N+M+i,1)=T_H_in;
end
A(M*N+2*M+1,(N+1)*M+1)=1;
b(M*N+2*M+1,1)=T_C_in;
X=A\b;
for i=1:M
for j=1:(N+1)
T_H(i,j)=X((j-1)*M+i);
end
end
for i=1:(M+1)
for j=1:N
T_C(i,j)=X((N+1)*M+i);
end
end
% total hot-side heat transfer rate
q_dot_H=m_dot_H*c_H*sum(T_H_in-T_H(:,N+1))/M;
% total cold-side heat transfer rate
q_dot_C=m_dot_C*c_C*sum(T_C(M+1,:)-T_C_in)/N;
C_dot_min=min([m_dot_H*c_H,m_dot_C*c_C]); % minimum capacitance rate
C_dot_max=max([m_dot_H*c_H,m_dot_C*c_C]); % maximum capacitance rate
q_dot_max=C_dot_min*(T_H_in-T_C_in); % maximum possible capacitance rate
eff=q_dot_H/q_dot_max; % effectiveness
% eff-NTU solution
NTU=UA/C_dot_min; % number of transfer units
C_R=C_dot_min/C_dot_max; % capacitance ratio

16 Comments

That should be a warning, not an error.
Apologies it's a warning but the code still won't run
The code above runs fine on R2011b on my laptop.
"clear all" is not useful. Is there any reason to remove all loaded functions from the memory?
You could pre-allocate all arrays consequently. Instead of calculating "(j-1)*M+i" repeatedly, you can use a temporary variable.
Ran it in R2011b still receiving the same warning.
I'm not really very strong at data handling, but have you tried creating empty arrays to insert data into?
@Wely: Please post a complete copy of the warning message and the line, which causes it. Then post, if the program at least runs successfully.
Hmmm the program doesn't run succesfull at all and the line where the warning message appears is [code] A((j-1)*M+i,(j-1)*M+i)=m_dot_H*c_H/M-UA/(M*N*2);
Every line that starts with A gives a warning. Tried both versions of matlab 2011 a en b and both of them gave me the same warning.
So help us out here. All the code after A((j-1)*M+i,(j-1)*M+i)=m_dot_H*c_H/M-UA/(M*N*2); is irrelevant for finding the cause of the warning. I bet you can simplify the code before that line. Do you still get the warning if you change that line to A((j-1)*M+i,(j-1)*M+i)=0; What about if you change it to A(i,j)=0? the simpler the question, the easier it is to answer.
Indeed but this is a heat equation that uses iterative steps to calculate the definite answer. That's why if i change the equation e.g. to zero the energy balance will become pointless.
The energy bilance doesn't matter, Matlab does not even know it is processing energy: Matlab sees numbers only. And to find an error, the meaning of the values is not relevant. So please follow Daniel's suggestions. If the problem is solved, you can restore the original values to get real results, of course.
No more warnings thanks to the solution Daniel provided.
But the energy balances are offset now so need to figure out how to rewrite them
I DID NOT provide a solution. I attempted to provide a way of getting a MWE.
Wely requested more info about my laptop that this code runs successfully on:
MATLAB Version 7.13.0.564 (R2011b)
Operating System: Microsoft Windows 7 Version 6.1 (Build 7601: Service Pack 1)
Java VM Version: Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
Has the Signal Processing and Database toolboxes.

Sign in to comment.

Answers (1)

What exactly does "the code does not run" mean? It is surprising that it runs successfully on e.g. K E's laptop and that you mention a warning only. A warning does not mean, that the program has an error and therefore you did not post any explanations about the actual problem.
So please ignore the warning at first and explain the hard error, which prevents a successful run.

Asked:

on 25 Apr 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!