MATLAB Answers

0

Cannot seem to correct the error with the function

Asked by Yavar Hayat on 22 Apr 2019
Latest activity Commented on by Stephen Cobeldick on 22 Apr 2019
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 Comment

Note: OP edited the code in the question and made the corrections suggested in the answers below.

Sign in to comment.

1 Answer

Answer by Adam Danz
on 22 Apr 2019

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 Comments

function fval=eneqpv(X,Pcell)
instead of
function eneqpv(X,Pcell)
was the solution
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.
+1 well explained.

Sign in to comment.