I need help with fixing the error in my for loop equation

1 回表示 (過去 30 日間)
Fun Dan
Fun Dan 2015 年 4 月 7 日
コメント済み: Image Analyst 2015 年 4 月 8 日
Please how do I fix the error message: Attempted to access ID(3); index out of bounds because numel(ID)=2.
I am trying to get a sum of all the iteration (H) from the code below: The error is somewhere in the formula for H but I am not able to fix it.
i= input('Number of Layers = '); % Say i=4
for n=1:i
ID(n)= input(['Layer ' , num2str(n) , ' Internal Diameter (m)= ']);
OD(n)= input(['Layer ' , num2str(n) , ' Outer Diameter (m)= ']);
k_L(n)= input(['Thermal Conductivity of Layer ' , num2str(i) , '(W/(m*K))= ']);
*H= ID(1)*sum((log(ID(n+1)/ID(n))/(2*k_L(n))))* ;
%D(1) is the innermost pipe layer which is ID at n=1
end
The formula for H is given below.

採用された回答

Image Analyst
Image Analyst 2015 年 4 月 7 日
When you're on the n'th iteration of your loop, why do you think ID(n+1) will have a value yet? You have not assigned a value to that element yet - it's the next one. You won't get the user to specify a value for it until the next iteration.
  3 件のコメント
Fun Dan
Fun Dan 2015 年 4 月 8 日
Thank you very much. It worked great!
Image Analyst
Image Analyst 2015 年 4 月 8 日
You're welcome. If we're done here, then can you go ahead and mark my answer as "Accepted"?

サインインしてコメントする。

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2015 年 4 月 7 日
You want to put the formula for H out of the loop
for ..
% ask for values here
end
% summing formula here
  1 件のコメント
Fun Dan
Fun Dan 2015 年 4 月 7 日
Hi Jos,
I tried it that way already and it is still giving me the same error message.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by