Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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) ))

Risposta accettata

Image Analyst
Image Analyst il 1 Mar 2016
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 Commento
ABDULLA RAHIL
ABDULLA RAHIL il 1 Mar 2016
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

Accedi per commentare.

Più risposte (1)

Jan
Jan il 1 Mar 2016
Modificato: Jan il 1 Mar 2016
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.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by