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