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

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일

0 개 추천

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

If this is homework, make sure you "Tag" it as homework above.
What does the sum go over? If i (which would be better named numberOfLayers) is 4, then does n in the sum go from 1 to 3, so that ID(n+1) will finish at ID(4) and not ID(5) which does not exist?
Alright, let's get you a little closer. Try this code:
numberOfLayers = input('Number of Layers = '); % Say i=4
for n = 1 : numberOfLayers
% Create prompts
prompt1 = sprintf('Layer #%d Internal Diameter (m) = ', n);
prompt2 = sprintf('Layer #%d Outer Diameter (m) = ', n);
prompt3 = sprintf('Thermal Conductivity of Layer %d (W/(m*K)) = ', n);
% Ask user for three floating point numbers.
defaultValue = {'1', '2', '3'};
titleBar = 'Enter the values';
userPrompt = {prompt1, prompt2, prompt3};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),break,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
ID(n) = str2double(caUserInput{1})
OD(n) = str2double(caUserInput{2}) % Not used!!!!!!!
k_L(n) = str2double(caUserInput{3})
end
% Compute H
ratios = ID(2:numberOfLayers) ./ ID(1 : (numberOfLayers-1))
theFormula = log(ratios) ./ (2*k_L(n))
% Not sure the formula for H is correct since
% it refers to ID*, not ID(1)*, but anyway....
H = ID(1) * theFormula
% ID(1) is the innermost pipe layer which is ID at n=1
I was wondering why your k_L always asked for the value for the last layer - the i'th layer, instead of the n'th layer. Is this just a typo because you did not use descriptive enough variable names? I also don't quite understand that H seems to want the whole ID array multiplied by the sum, which would give another array, rather than ID(1) multiplied by the sum, which is what you did in your code.
Thank you very much. It worked great!
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

Hi Jos,
I tried it that way already and it is still giving me the same error message.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 4월 7일

댓글:

2015년 4월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by