Unable to perform two for loops, with a set value from first for loop into second loop

조회 수: 2 (최근 30일)
I am trying to calculate E_tot, E_inEl and E_comp for each p_limit (250, 500 and 750) and output a vector of 8760x3, i have limited experiense with matlab and cant seem to figure this out. Is there anyone that could help me? Excel file can be found in attachment.
I am getting this error when running the script.
Unable to perform assignment because the left and right sides have a different number of elements.
Error in test (line 104)
E_tot(E_tot>E_max)=E_max;
[B]=xlsread('Fullyear(Metnom)');
SI_y=B(:,1);
%parameters
n=0.2; %Efficientcy PV
P_outp=1*10^6; %Wp PV
Area=P_outp/(n*max(SI_y)); %Area [m^2] PV
m_h2=55; %kWh/kg H_2
n_comp=0.1; %Energy for compression 10% of total energy used in eletrolysis
n_coff=0.2; %Dynamic range, cut-off @ 20% (AWE)
p_limit=[250:250:750] %Electrolyzer Size [kWh]
h=1:1:8760; %Hours in year [t]
for i=p_limit
p_lim=p_limit*n_coff; %Electrolyzer production cut-off [kWh] @ (20%)
E_min=p_limit*n_coff+p_lim*n_comp;
E_max=p_limit*(1+n_comp);
for ii=h
E=n*SI_y*Area*10^-3; %Power output PV [kWh]
E_tot=E; %Total power [kWh]
E_tot(E_tot<E_min)=0;
E_tot(E_tot>E_max)=E_max;
E_inEl=E-E*n_comp; %Power input electrolyzer [kWh]
E_inEl(E_inEl<p_lim)=0;
E_inEl(E_inEl>p_limit)=p_limit;
E_comp=E_tot-E_inEl; %Power for compression [kWh]
end
end

채택된 답변

Kevin Phung
Kevin Phung 2019년 3월 26일
편집: Kevin Phung 2019년 3월 26일
The problem is that your Emax is defined as a 1x3 array. So I think you meant to index your p_limit vector.
Heres sort of what you should do, refer to my comments:
for i=1:numel(p_limit) % for each value in p_limit
p_lim=p_limit(i)*n_coff; %index plimit here
E_min=p_limit(i)*n_coff+p_lim*n_comp;
E_max=p_limit(i)*(1+n_comp);
for ii=h % I dont know what h is here
E=n*SI_y*Area*10^-3;
E_tot=E;
E_tot(E_tot<E_min)=0;
E_tot(E_tot>E_max)=E_max;
E_inEl=E-E*n_comp;
E_inEl(E_inEl<p_lim)=0;
E_inEl(E_inEl>p_limit)=p_limit(i); %indexed p_limit
E_comp=E_tot-E_inEl;
end
end
I'm not sure how your second forloop goes, but perhaps you can double check the indexing. Also, if you are multiplying two arrays, be sure to check if you need to do the element wise operator (.* or ./)

추가 답변 (2개)

Morten Fosstveit
Morten Fosstveit 2019년 3월 26일
Thanks, it was what i wanted. Just needed to add a line for not overwriting previous iteration.
E_comp=E_tot-E_inEl;
end
S(:,r) = E_inEl; %<------
end

MOHAMED-AMINE BABAY
MOHAMED-AMINE BABAY 2020년 10월 4일
Hii , Could you please send me this program

카테고리

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