My MATLAB loop is not ending

So for studying, I made the following loop, but the thing is it not ending. I think it's something wrong with the code, please help
clc;
clear all;
time = 1 ;
t_star = 0;
%Concentration
Cf(time) = 1;
Cd(time) = 2;
%Volume
Vf0 = 2;
Vd0 = 2;
%Assume 298K temperature
L = 0.077; %Channel Length in metre
W = 0.026; %Channel width in metre
H = 0.003; %Channel height in metre
%Cross-Flow Velocity
vf = 0.085;
vd = 0.085;
A = 3.08e-12; % Pure water permeability coefficient A in m/Pas
% B is negligible
S = 3.6e-04; %Structural Parameter of Membrane in m
Am = 0.014; %Area of Membrane in m2
dh = (2*W*H)/(W+H); %Hydrawlic Diameter in m
Cf0 = 1;
x2_pif = 42.6;
x3_pif = 7;
Pi_f0 = (x2_pif*Cf0 + x3_pif*Cf0^2)*10^5;
%Cf(time) = 0;
%Cd(time) = 3;
%Cf_new(time) = 0;
%Cd_new(time) = 3;
while(time >= 1)
%Viscosity of FS in Pas
x1_muf = 890.7;
x2_muf = 70;
x3_muf = 1.75;
mu_f = (x1_muf + x2_muf*Cf(time) + x3_muf*Cf(time)^2)*10^-06;
%Density of FS in kg/m^3
x1_rhof = 996.845;
x2_rhof = 40.5;
rho_f = x1_rhof + x2_rhof*Cf(time);
%Diffusivity of FS in m^2/s
x1_df = 1509;
x2_df = -94;
x3_df = -6;
D_f = (x1_df + x2_df*Cf(time) + x3_df*Cf(time)^2)*10^-12;
%Schmidt Number of FS
Sc_f = mu_f/(rho_f*D_f);
%Reynolds Number of FS
Re_f = (rho_f*vf*dh)/mu_f;
%Sherwood Number for FS as Reynold Number less than 2100
Sh_f = 1.85*(Re_f*Sc_f*(dh/L))^0.33;
%Mass Transfer coefficient of FS
k_f = (Sh_f*D_f)/dh;
%Diffusivity of DS in m^2/s
x1_dd = 1509;
x2_dd = -94;
x3_dd = -6;
D_d = (x1_dd + x2_dd*Cd(time) + x3_dd*Cd(time)^2)*10^-12;
%Mass Transfer coefficient of DS
k_d = S/D_d;
%Osmotic Pressures of FS and DS
x2_pif = 42.6;
x3_pif = 7;
Pi_f = (x2_pif*Cf(time) + x3_pif*Cf(time)^2)*10^5; %Osmotic Pressure of FS
x2_pid = 42.6;
x3_pid = 7;
Pi_d = (x2_pid*Cd(time) + x3_pid*Cd(time)^2)*10^5; %Osmotic Pressure of DS
syms Jw
eqn = Jw - A*(Pi_d*exp(-Jw*k_d)-Pi_f*exp(Jw/k_f)) == 0;
solJw = double(vpasolve(eqn, Jw));
%number of water moles transport fr om the FS side to the DS side
Mw = 18; %molar mass of water in grams/mol
xf_wat = (Cf(time)*Mw)/rho_f;
n = ((solJw*Am)*(rho_f*xf_wat)/Mw)*1000;
t_star = t_star + n;
%the number of water moles in the FS and DS sides
m_f = ((Vf0)*(rho_f*xf_wat)/Mw) - t_star ;
%Density of DS in kg/m^3
x1_rhod = 996.845;
x2_rhod = 40.5;
rho_d = x1_rhod + x2_rhod*Cd(time);
xd_wat = (Cd(time)*Mw)/rho_d;
m_d = ((Vd0)*(rho_d*xd_wat)/Mw) + t_star ;
%the new volume of FS and DS sides
Vf_new = Vf0 - ((t_star*Mw)/(rho_f*xf_wat));
Vd_new = Vd0 + ((t_star*Mw)/(rho_d*xd_wat));
%the new concentration of FS and DS
Cf_new(time) = (Cf(time)*Vf0)/Vf_new;
Cd_new(time) = (Cd(time)*Vd0)/Vd_new;
%if(D_f==D_d)
% time =0;
%else time = time+1;
% Cf(time) = Cf_new(time-1);
%Cd(time) = Cd_new(time-1);
%end
if ( Cf_new(time) ~= Cd_new(time))
time = time + 1 ;
Cf(time) = Cf_new(time-1);
Cd(time) = Cd_new(time-1);
elseif ( mod(Cf_new(time) - Cd_new(time))<=0.5)
Cf_final = Cf_new(time);
Cd_final = Cd(time);
time = 0;
end
end

 Accepted Answer

Cris LaPierre
Cris LaPierre on 5 Oct 2020
Either your 'if' condition at the end of your while loop is always true, or your 'elseif' condition is never true. I would try to rethink that part of your code.

3 Comments

In particular, comparing floating-point numbers for exact, down-to-the-last-bit equality (or inequality) may not be what you want. If you want to stop when the two quantities Cf_new and Cd_new are "close enough" compare with a tolerance.
if abs(Cf_new(time) - Cd_new(time)) < some_tolerance
% you're done
else
% you're not done
end
Yes , thank you I was looking for modulus subtraction, so I changed the code with conditional OR operator. Even now the loop is not ending. But I think I made a mistake in the logic somewhere and not a problem with the code itself
I suggest learning to use the debugger. It sounds like you need to explore your values in order to develop the necessary logic.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!