while loop won't run?
4 views (last 30 days)
Show older comments
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
Answers (1)
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.
See Also
Categories
Find more on Permanent Magnet in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!