writing and call a function in a script with for loops

1 次查看(过去 30 天)
Hi,
I have a question about a function I'm writing and that I do not manage to run in my script file. I enclose the code here:
%% Supply Network Calculation.
% Evaluate the temperature propagation inside a pipe
function [T_s,Q_Ls] = Supply_Network_Calculation (k, n, B_Pipe, C_Pipe, F_Pipe, m,Tg,Tf)
% DECLARE MATRIX U NEED
T=zeros(k,n); Q_Ls=zeros(k,n);
for i=1:k-1,
T(1,1)=Tg(1,1);
T(i+1,1)=Tg(1,1);
if m(j)==0.
T(1,j+1)=T(2,j);
else T(1,j+1)=Tf(j,1);
end
T(i+1,j+1) = (T(i+1,j)+C_Pipe(j)*(T(i,j+1))+(F_Pipe*2*Tg(j,1)))/B_Pipe(j);
Q_Ls(i,j)=U_p_Pipe*dA_Pipe*(((T(i,j)+T(i+1,j))/2)-Tg(j,1));
if Q_Ls(i,j)<0
Q_Ls(i,j)= 0;
end
end
end
T_s=T(k,:)';
end
I got this error: Subscript indices must either be real positive integers or logicals.
Error in Supply_Network_Calculation (line 10) if m(j)==0.
Error in FEM_Prova_PipeA_corrections_Working (line 282) [T_s2, Q_LsA]= Supply_Network_Calculation (k,n,B_A,C_A,F_A,m_2,Tg,Tf);
I have two questions:
1) Should I define the j index in the function script or would it be recognized within the script?
2) Also, is it a problem having most of my input as matrices?
Thanks for your help in advance
Michele

回答(1 个)

Michael Haderlein
Michael Haderlein 2014-8-20
It's generally not ideal to name variables "i" and "j" as they are also used for the imaginary unit. That's exactly what is happening in your function: j is not further defined and so it's used as 1i which of course cannot be an index. Coming to your questions:
1) Where else do you want to define j? I guess it's also some index you want to loop. So you need this loop in your function. If the entire function is inside the j-loop, you need to pass the value of j as argument into the function.
2) It's totally no problem to pass arguments as matrices. Only in case you have very large matrices and you are running out of memory, you should think about other concepts. I doubt that this is the case here.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by