Plotting inside for loop using if
Show older comments
Hello, I am trying to plot a formula in which if the input does not follow the constraint, calculation process change.I am trying to do this with if inside a for loop.My script is below.It works for single x values but It does not work for all the x values from 0 to 1 with increments of 0.01.Could anyone please fix the error I do here?
G=200; %mass flux[kg/m^2*s]
Pred=0.31052; %reduced pressure
M=44.01; %moleculer weight
q=10000; %heat flux
gv=60.72775526; %density of vapor phase
gl=1008.003926; %density of liquid phase
g=9.81; %gravitational acceleration
sig=7.53*10^-3; %surface tension
vv=1.34741*10^-5; %viscosity of vapor phase
vl=1.28*10^-4; %viscosity of liquid phase
kl=0.128582692; %conductivity of liqud phase
kv=0.0137; %conductivity of vapor phase
hlv=236272.0191; % enthalpy of vaporization
D=0.0061; %inside diameter
Pr=2.2231; % prandtl number for liquid flow
cpv=1387.633317; %specific heat of vapor phase
for x=0.1:0.01:0.9; %dry coefficient[-]
Bo=q./(G.*hlv);
Bd=(g.*(gl-gv).*D.^2)./sig;
Rel=(G.*(1.-x).*D)./vl;
x_critical=38.27.*Rel.^2.12.*(1000.*Bo).^1.64.*Bd.^-4.7;
if x< x_critical
E=(1.+9.36.*10^3.*x.*Pr.*((gl./gv)-1)).^0.11;
S=1./(1+0.00000162.*E.^0.69.*Rel.^1.11);
hl=0.023.*Rel.^0.8.*Pr.^0.4.*(kl./D);
hnb=55.*Pred.^0.12.*(-log10(Pred)).^-0.55.*M.^-0.5.*q.^0.67;
htp_yoon=((S*hnb).^2+(E.*hl).^2).^0.5;
plot(x,htp_yoon)
hold on
else
Rel=(G.*(1-x).*D)./vl;
Rev=(G.*x.*D./vv);
hv=0.023.*Rev.^0.8.*(cpv.*vv./kv).^(0.4).*(kv./D);
E=1+3000.*Bo.^0.86+1.12.*(x./(1-x)).^0.75.*(gl./gv).^0.41;
hl=0.023.*Rel.^0.8.*Pr.^0.4.*(kl./D);
Xtt=((1.-x)./x).^0.9.*(gv./gl).^0.5.*(vl./vv).^0.1;
theta=2*pi*(36.23.*Rel.^3.47.*Bo.^4.84.*Bd.^-0.27.*(1./Xtt).^2.6);
hwet=E.*hl;
htp_yoon=((theta.*hv)+(2*pi-theta).*hwet)./(2*pi);
plot(x,htp_yoon)
hold on
end
end
7 Comments
dpb
on 9 May 2017
Doesn't appear to be anything untoward in the code; what, specifically, do you think is a problem?
Use the debugger and step through at the point in question...
@Berkay Cinar: Just a note: 9.36e3 is a cheap constant while 9.36.*10^3 is an expensive power operation and a multiplication.
Please mention, what the problem is. How do you see that "It does not work for all the x values"?
The code does not work for "all the x values from 0 to 1" because the loop runs over "0.1:0.01:0.9" only.
dpb
on 9 May 2017
Good catch on the loop limits, Jan...didn't notice that.
KSSV
on 9 May 2017
Try plot(x,htp_yoon,'.')
dpb
on 9 May 2017
Oh, yeah, that too...he'd be better off building the whole output then plotting instead of inside the loop...
Berkay Cinar
on 9 May 2017
dpb
on 10 May 2017
Hmm....not sure where the code changed from what I copied prior time, but I also get that for the breakpoint now...
But, your plotting problem has to do with how you've structured the loop; by having only one point at a time, all you get is a dot on the screen that isn't visible because it isn't large enough. To see this, try changing your current plotting instruction to
plot(x,htp_yoon,'x','color','k')
and you'll see that before it did plot, you just couldn't seen the tiny little one-pixel dots.
But, the easier way is still to save up the computed array and plot in the end; you'll probably want those values for other purposes, anyway, won't you?
Anyway, the general idea is
- make the range x values a vector and iterate over it in counted loop instead of using it as the loop value
- create array for htp_yoon to match x
- modify all references to variable x to use the subscripted value instead of the whole array reference
- move the plotting to when you're otherwise done...
I fixed up the code in Answer to match; what I forgot before was that x was only a single value instead of the vector because you had used it for the iterator...
Accepted Answer
More Answers (0)
Categories
Find more on Thermal Liquid Library in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!