Can't run code because of sparse indexing expression is likely to be slow
2 views (last 30 days)
Show older comments
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
Daniel Shub
on 27 Apr 2012
I DID NOT provide a solution. I attempted to provide a way of getting a MWE.
K E
on 27 Apr 2012
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.
Answers (1)
Jan
on 27 Apr 2012
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!