while loop won't run?

4 views (last 30 days)
maud
maud on 11 Nov 2016
Edited: per isakson on 14 Nov 2016
Hello,
I am programming the simulation of a thermoelectric device coupled to PV cells and for some reason the while loop I have coded won't seem to run. When I run my script it skips it and goes directly to end (which I can tell by putting breakpoints). If anyone has an idea as to how I can fix this, that would be of great help! Thank you :)
%%Task 5
clc;close all; clear all;
%%1. Guess Tpv:
Tpv=293; %K
%%2. Run Task 3 to compute the power output Pl and waste heat Qdotpv
Rlpv=.007; %Ohm
Rs=0; %Ohm
Apv=(.1)^2; %m^2
rc=12; %nounit
Id=1350; %J.s^-1.m^-2
Pin=rc*Id; %J.s^-1.m^-2
PL=powdev(Tpv, Rlpv);
display ('in W=J/s');
Qdotpv=Pin*Apv-PL;
display('in W=J/s');
%%3. Set TH and run Task 4 to get TC and QHdot
% Parameters
Sigcont=300; %W/K
Tsat=90; %K
Rlte=.10; %ohm
n=12; %nounit
rhoA=.002; %ohm.cm
rhoB=.003; %ohm.cm
lamA=.032; %W/(cm.K)
lamB=.021; %W/(cm.K)
alpha=.0017; %V/K
kweff=6.5; %W.m^-1.K^-1
tw=4.5*10^-3; %m
TCguess=90; %K
% To get TC
TH=Tpv-Qdotpv/Sigcont
%
[TC,Err2]=optTC(TCguess,TH,Tsat,Rlte,n,rhoA,rhoB,lamA,lamB,alpha,kweff,tw,Apv);
display(TC);
display('both in K');
Th=TH-273
Tc=TC-273
display('both in deg C');
%
% To get QHdot :
%a
RSigmin=((lamA*rhoA)^0.5+(lamB*rhoB)^0.5)^2; %W.Ohm.K^-1
%b
Z=alpha^2/RSigmin; %K^-1
%c
mmaxeff=(1+0.5.*(TH+TCguess)*Z).^0.5; %nounit
%d
R=Rlte./(n*mmaxeff); %ohm
%iii : efficiency
eta=((TH-TC)/TH).*((1+mmaxeff).^2./mmaxeff.*(Z.*TH)^-1+1+1./(2.*mmaxeff).*(1+TC./TH)).^-1; %nounit
%
%iv : power output
Rbatt=n.*R; %Ohm
Wdot=n.^2.*alpha.^2.*(TH-TC).^2.*Rlte./(Rlte+Rbatt).^2; %W.m^2
%
%v : heat input and rejection for the TE device
QHdot=Wdot./eta %W
display('in W=J/s');
%%4. Compute the error and optimize Tpv
Error=Qdotpv-QHdot; %W
i=10;
while Error>=10^-5
if sign(Error)==-1
Tpv=Tpv+i
PL=powdev(Tpv, Rlpv);% W
Qdotpv=Pin*Apv-PL; % W
TH=Tpv-Qdotpv/Sigcont; %K
TCguess=TC; %K
% [TC,Err2]=optTC(TCguess,TH,Tsat,Rlte,n,rhoA,rhoB,lamA,lamB,alpha,kweff,tw,Apv);
RSigmin=((lamA*rhoA)^0.5+(lamB*rhoB)^0.5)^2;%W.Ohm.K^-1
Z=alpha^2/RSigmin; %K^-1
mmaxeff=(1+0.5.*(TH+TCguess)*Z).^0.5; %nounit
R=Rlte./(n*mmaxeff); %ohm
eta=((TH-TC)/TH).*((1+mmaxeff).^2./mmaxeff.*(Z.*TH)^-1+1+1./(2.*mmaxeff).*(1+TC./TH)).^-1; %nounit
Rbatt=n.*R; %Ohm
Wdot=n.^2.*alpha.^2.*(TH-TC).^2.*Rlte./(Rlte+Rbatt).^2; %W.m^2
QHdot=Wdot./eta; %W
Error=abs(Qdotpv-QHdot) %W
else
Tpv=Tpv-i;
i=i*0.5;
Tpv=Tpv+i
PL=powdev(Tpv, Rlpv);% W
Qdotpv=Pin*Apv-PL; % W
TH=Tpv-Qdotpv/Sigcont; %K
TCguess=TC; %K
% [TC,Err2]=optTC(TCguess,TH,Tsat,Rlte,n,rhoA,rhoB,lamA,lamB,alpha,kweff,tw,Apv);
RSigmin=((lamA*rhoA)^0.5+(lamB*rhoB)^0.5)^2;%W.Ohm.K^-1
Z=alpha^2/RSigmin; %K^-1
mmaxeff=(1+0.5.*(TH+TCguess)*Z).^0.5; %nounit
R=Rlte./(n*mmaxeff); %ohm
eta=((TH-TC)/TH).*((1+mmaxeff).^2./mmaxeff.*(Z.*TH)^-1+1+1./(2.*mmaxeff).*(1+TC./TH)).^-1; %nounit
Rbatt=n.*R; %Ohm
Wdot=n.^2.*alpha.^2.*(TH-TC).^2.*Rlte./(Rlte+Rbatt).^2; %W.m^2
QHdot=Wdot./eta; %W
Error=abs(Qdotpv-QHdot) %W
end
end
display (Tpv);
  3 Comments
maud
maud on 14 Nov 2016
Edited: per isakson on 14 Nov 2016
I think the problem is coming from my function optTC. here's a copy of it, if you see anything wrong let me know! I think it may because of syms or the fact that there is two outputs, not sure...
%%Optimizing TC
function [TC,Err2]=optTC(TCguess,TH,Tsat,Rlte,n,rhoA,rhoB,lamA,lamB,alpha,kweff,tw,Apv)
%a
RSigmin=((lamA*rhoA)^0.5+(lamB*rhoB)^0.5)^2; %W.Ohm.K^-1
%b
Z=alpha^2/RSigmin; %K^-1
%c
mmaxeff=(1+0.5.*(TH+TCguess)*Z).^0.5; %nounit
%d
R=Rlte./(n*mmaxeff); %ohm
%e
Sigma=RSigmin./R; %W.K^-1
%ii : Qcboil
QCboil=Apv*kweff.*(TCguess-Tsat)/tw; %W.m^2
%iii : efficiency
eta=((TH-TCguess)/TH).*((1+mmaxeff).^2./mmaxeff.*(Z.*TH)^-1+1+1./(2.*mmaxeff).*(1+TCguess./TH)).^-1; %nounit
%iv : power output
Rbatt=n.*R; %Ohm
Wdot=n.^2.*alpha.^2.*(TH-TCguess).^2.*Rlte./(Rlte+Rbatt).^2; %W.m^2
%v : heat input and rejection for the TE device
QHdot=Wdot./eta; %W
QCdotTE=QHdot-Wdot; %W
%vi : error
Err1=double(vpa(QCdotTE-QCboil,10)); %W
Err2=1;
i=10;
TC=TCguess;
while abs(Err2)>=10^-5
if sign(Err1)==sign(Err2)
TC=TC+i;
%c
mmaxeff=(1+0.5.*(TH+TC)*Z).^0.5; %nounit
%d
R=Rlte./(n*mmaxeff); %ohm
%e
Sigma=RSigmin./R; %W.K^-1
%ii : Qcboil
QCboil=Apv*kweff.*(TC-Tsat)/tw; %W.m^2
%iii : efficiency
eta=((TH-TC)/TH).*((1+mmaxeff).^2./mmaxeff.*(Z.*TH)^-1+1+1./(2.*mmaxeff).*(1+TC./TH)).^-1; %nounit
%iv : power output
Rbatt=n.*R; %Ohm
Wdot=n.^2.*alpha.^2.*(TH-TC).^2.*Rlte./(Rlte+Rbatt).^2; %W.m^2
%v : heat input and rejection for the TE device
QHdot=Wdot./eta; %W
QCdotTE=QHdot-Wdot; %W
%vi : error
Err2=double(vpa(QCdotTE-QCboil,10)); %W
%
else
TC=TC-i;
i=0.5*i;
TC=TC+i;
%c
mmaxeff=(1+0.5.*(TH+TC)*Z).^0.5; %nounit
%d
R=Rlte./(n*mmaxeff); %ohm
%e
Sigma=RSigmin./R; %W.K^-1
%ii : Qcboil
QCboil=Apv*kweff.*(TC-Tsat)/tw; %W.m^2
%iii : efficiency
eta=((TH-TC)/TH).*((1+mmaxeff).^2./mmaxeff.*(Z.*TH)^-1+1+1./(2.*mmaxeff).*(1+TC./TH)).^-1; %nounit
%iv : power output
Rbatt=n.*R; %Ohm
Wdot=n.^2.*alpha.^2.*(TH-TC).^2.*Rlte./(Rlte+Rbatt).^2; %W.m^2
%v : heat input and rejection for the TE device
QHdot=Wdot./eta; %W
QCdotTE=QHdot-Wdot; %W
%vi : error
Err2=double(vpa(QCdotTE-QCboil,10)); %W
end
end
end
maud
maud on 14 Nov 2016
Edited: per isakson on 14 Nov 2016

here it the other function powder if you need it:

function [Pl,efficiency]=powdev(Tpv, Rlpv)
qe=1.6022*10^-19; %J
kB=1.38*10^-23; %J.K^-1
Tsource=6000; %K temperature of the sun
Apv=(10*10^-2)^2; %m^2
rc=12;
Id=1350; %J.s^-1.m^-2
Pin=rc*Id; %J.s^-1.m^-2
Phi=Pin*2.404*15/(pi^4*kB*Tsource); %m^-2.s^-1
Phig=0.416*1.3495429*Phi; %m^-2.s^-1
Inu=Phig*qe*Apv; %C.s^-1=A
Io=Inu*(3*10^-8.*Tpv.^3-3*10^-5.*Tpv.^2+0.0098.*Tpv-2.083); 
%A (see excel for details)
syms x;
solve (Rlpv*x==-Tpv*kB/qe*log(1+(Inu-x)/Io),x);
Il=eval(ans); %a=C.s^-1
Vl=Rlpv*Il; %V
Pl=Vl.*Inu-Vl.*Io.*(exp(qe.*Vl/(kB*Tsource))-1); %W=J/s
efficiency=Pl/(Pin*Apv);
end

Sign in to comment.

Answers (1)

Roger Stafford
Roger Stafford on 11 Nov 2016
I am guessing that you need to change the initial ‘Error’ to:
Error = abs(Qdotpv-QHdot); %W
As you have it, if the error is large but negative with Qdotpv<QHdot, the ‘while’ loop will never execute.
  1 Comment
maud
maud on 14 Nov 2016
thanks!! I have added the abs but still isn't working because of my optTC function apparently, I have posted it on the previous comment.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!