I have the code below and there is an error which couldn't catch any help please ?
1 次查看(过去 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) ))
0 个评论
采纳的回答
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 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scope Variables and Generate Names 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!