필터 지우기
필터 지우기

Cannot seem to correct the error with the function

조회 수: 1 (최근 30일)
Yavar Hayat
Yavar Hayat 2019년 4월 22일
댓글: Stephen23 2019년 4월 22일
It warns that the value assigned to fval (check last line)might be unused. There is something woring with the function, I cannot figure it out quickly.
Says :Failure in initial objective function evaluation. FSOLVE cannot continue.
I reduced the number of values in the three arrays used because of character limit in MATLAB Answers.
function main
lambda=[280;280.500000000000;281;281.500000000000;282;282.500000000000;283;283.500000000000;284;284.500000000000];
EQE=[0;0.00590899000000000;0.0118151780000000;0.0177157650000000;0.0236079480000000;0.0294889270000000;0.0353559000000000;0.0412060670000000;0.0470366260000000;0.0528447770000000];
spec=[3.00000000000000e-26;1.09000000000000e-24;6.13000000000000e-24;2.74790000000000e-22;2.83460000000000e-21;1.32710000000000e-20;6.76460000000000e-20;1.46140000000000e-19;4.98380000000000e-18;2.16240000000000e-17];
nopt=.85;
alphapv=0.95;
Apv=0.005*0.005;
Aopt=1000*Apv;
h=6.626*10^-34;
q=1.602177*10^-19;
c=3*10^8;
kb=1.38065*10^-23;
theta=0.0002;
Eg=1.12;
nf=1.33;
C=Aopt/Apv;
Voc1=0.965;
Rs=12.16*10^-3;
Tref=298.15;
JscC=0;
lccell=10;
for i=1:1:lccell
term=nopt*alphapv*q*lambda(i)*EQE(i)*spec(i)/(h*c*10^9);
JscC=JscC+term;
end
lambdam=lambda(lccell);
%initial cond
Tcell0=300;
maxIter=1000;
tolTcell=1e-4;
%comp
Tcell=Tcell0;
Tcellold=Tcell0;
for i =1:maxIter
Voc=(h*c/lambdam)*Voc1/(q*Eg)+(nf*kb*Tcell*log(C))/q;
vo=q*Voc/kb*Tcell;
rs=Rs*JscC*C*Apv/Voc;
FF=(vo-log(vo+0.72))*(1-rs)/(1+vo);
Pcell=Voc*C*JscC*Apv*FF*(1-theta*(Tcell-Tref));
sol=fsolve(@(X)eneqpv(X,Pcell),Tcellold);
Tcell=sol;
err=abs(Tcellnew-Tcellold);
Tcellold=sol;
if(err<tolTcell)
break
end
end
end
function fval=eneqpv(X,Pcell)
Tcell=X;
spec=[3.00000000000000e-26;1.09000000000000e-24;6.13000000000000e-24;2.74790000000000e-22;2.83460000000000e-21;1.32710000000000e-20;6.76460000000000e-20;1.46140000000000e-19;4.98380000000000e-18;2.16240000000000e-17];
nopt=.85;
alphapv=0.95;
Apv=0.005*0.005;
Aopt=1000*Apv;
epsilon = 0.85;
sigma = 5.67*10^(-8);
C=Aopt/Apv;
hc= 8;
hcool=10000;
Ta=300;
Tf=293.15;
%Pcell=1;
epvC=0;
lccell=10;
for i=1:1:lccell
termpv = Apv*nopt*alphapv*spec(i);
epvC=epvC+termpv;
end
fval=epvC*C-hc*Apv*(Tcell-Ta)-epsilon*sigma*Apv*((Tcell.^4)-(Ta^4))-Pcell-hcool*Apv*(Tcell-Tf);
%says unused. This is the equation to be solved using fsolve.
end
  댓글 수: 1
Adam Danz
Adam Danz 2019년 4월 22일
Note: OP edited the code in the question and made the corrections suggested in the answers below.

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

답변 (1개)

Adam Danz
Adam Danz 2019년 4월 22일
In your call to fsolve(), the objective function contains two inputs
sol=fsolve(@(X)eneqpv(X,Pcell),Tcellold);
% 1, 2
but the objective function you defined only contains one input.
function eneqpv(X)
% 1
end
The variable Pcell looks like it's hard-coded in the objective function as Pcell=1. One solution is to remove the Pcell from the 2nd input in your call to fsolve(). Another solution is add Pcell as a second input in your objective function and remove the hard-coded variable.
Lastly, the reason you're getting the warning about the value assigned to fval is because that variable is not being used anywhere in the code. You might want to add that as an ouput to your main function.
  댓글 수: 3
Adam Danz
Adam Danz 2019년 4월 22일
편집: Adam Danz 2019년 4월 22일
That's right. That should eliminate the error you were getting. I didn't run your code so I don't know if there will be additional errors.
Stephen23
Stephen23 2019년 4월 22일
+1 well explained.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by