필터 지우기
필터 지우기

I have the code below and there is an error which couldn't catch any help please ?

조회 수: 2 (최근 30일)
hyd_dem=A/2.3; % each kg require 2.3kw ((54.6kwh/kg))
Ele_consum=B/2.3; %2.3kw/kg ((54.6kwh/kg))
Num_ele=Ele_consum/1500; % 1500 kg/day is rated electrolyser production
Num_ele = round( Num_ele,0); % convert the decimal numbers to integers
hydrogen_excess= Ele_consum-hyd_dem; % excess and deficiency of hydrogen
hydrogen_extra= hyd_dem-Ele_consum;
H2 =max(hydrogen_excess);
F=[0 0];
i=0;
while(i<=365)
i=i+1;
if(hydrogen_excess(i)>=0)
if (F(i)<=H2)
hydrogen_excess(i)=hydrogen_excess(i);
else
hydrogen_excess(i)=0;
end
else
Excess(i)=0;
end
end % [EDITED, Jan, ???]
if(hydrogen_extra(i)>=0)
if (F(i)>=hydrogen_extra(i))
hydrogen_extra(i)=hydrogen_extra(i);
else
hydrogen_extra(i)=F(i);
end
if(hydrogen_excess(i)>0)
F(i+1)=F(i)+hydrogen_excess(i);
elseif(hydrogen_extra(i)>0)
F(i+1)=F(i)-hydrogen_extra(i);
else
F(i+1)=F(i);
end
end
%%%% the error was
((Attempted to access F(6); index out of bounds because numel(F)=2.
Error in Untitled4 (line 17)
if (F(i)<=H2) ))

채택된 답변

Image Analyst
Image Analyst 2016년 3월 1일
Somehow i is getting up to 6, but you only defined F as having 2 values, both zero. Make F have at least as many values as the value that you expect i to reach.
  댓글 수: 1
ABDULLA RAHIL
ABDULLA RAHIL 2016년 3월 1일
I cant guess the F value since its change between the excess and extra so difficult to if u put any other values still the same error

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

추가 답변 (1개)

Jan
Jan 2016년 3월 1일
편집: Jan 2016년 3월 1일
I've edited the code in the your question and added a standard indentation. Then it gets obvious, that the loop runs without updating F. I assume, that the end marked by [???] is misplaced and should be moved to the end of the code.
Use the auto-indentation to see such problems immediately. Or mark the code in the editor and hit Ctrl-i.
But even if this is the problem, the error message would occur at i==3 already. The message for F(6) is impossible with the shown code. So please post the code, which causes the error to avoid yo confuse the readers.

카테고리

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