Foor loop; iteration
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I want to use a for-loop to iterate and adjust the value of x until either V_check equals V or the absolute difference between V_check and V becomes zero or close to zero. With this code, I'm getting the message "Solution found after 19 iterations" and the following values:
- x = -2.0817e-16
- V = -6.538079556362143e-17
- V_check = -0.009549534391159
which do not match.
%% &&& Cp for BL &&
clc, clear
xf = 0.08; T1 = 90;
Cpw = 4188; % at T1
Cp_BL = calculateCp(xf,T1, Cpw);
%% &&& Steam or T2 &&
% m_dot_BL*Cp_BL(T1-T2) = m_dot_s * Hvap
% If T2 is known set val = 0 if m_steam is known set val1 = 1
F = 8; % kg/s
T2 = T1 + 20;
H_vap = 2048.16; % vid 8 bar och 170 C
H_F = Cp_BL*T1;
S = 0;
val1 = 0;
if val1 == 0
S = F * Cp_BL * (T2 - T1) / H_vap;
end
if val1 == 1
T2 = (m_dot_BL * Cp_BL * T1 - S * H_vap) / (F * Cp_BL);
end
%% We do not know the value of the concentrated product and the evaporated steam from BL
%% We have to guess and iterate, Guess that V = 0.9*S
% Set an initial value for x
x= 0.9;
tolerance = 1e-2; % Tolerance for considering V_check close enough to zero
maxIterations = 100; % Maximum number of iterations if necessary
Cpw2 = 4201;
for iteration = 1:maxIterations
% Calculate V based on the current value of x
V = x * S;
L = F - V;
xl = xf * F / L;
Cp_L = calculateCp(xl, T2, Cpw2);
H_L = Cp_L * T2;
H_V = 1746; % Superheated steam at the given black liquor pressure at T2
% Check if the guessed V is correct within a certain tolerance
V_check = (F * H_F + S * H_vap - L * H_L) / H_V;
% If V_check is close enough to zero, exit the loop
% If the difference between V and V_check is close enough to zero, exit the loop
if abs(V - V_check) < tolerance
disp(['Solution found after ' num2str(iteration) ' iterations.']);
break;
end
% Adjust the value of x for the next iteration
x = x - 0.05;
end
V
V_check
% If maxIterations is reached without the solution being close enough to zero, handle it here
if iteration == maxIterations
disp('Maximum number of iterations reached without reaching the desired solution.');
end
1 Commento
Dyuman Joshi
il 6 Feb 2024
Modificato: Dyuman Joshi
il 6 Feb 2024
"... which do not match."
How exactly do they not match?
%Values taken from the question statement
V = -6.538079556362143e-17;
V_check = -0.009549534391159;
abs(V - V_check)
The absolute value of the difference, 0.0095, is less then 1e-2 or 0.01, which satisfies the condition to break the for loop.
You get the value according to the tolerance you have set. Adjust it according to your requirements.
Risposte (1)
Aquatris
il 6 Feb 2024
Modificato: Aquatris
il 6 Feb 2024
You set your tolerance to 1e-2 so your for loop terminates when abs(V-V_Check) becomes less than 1e-2.
% here you basically define how close you want V to V_Check before you
% say I found my answer and end all operations
if abs(V - V_check) < tolerance
disp(['Solution found after ' num2str(iteration) ' iterations.']);
break;
end
If you want to your for loop to terminate when V is a lot closer to V_Check, set your tolerance to a lot lower value, such as
tolerance = 1e-15;
0 Commenti
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!