Foor loop; iteration

조회 수: 2 (최근 30일)
Mohamed Asaad
Mohamed Asaad 2024년 2월 6일
편집: Dyuman Joshi 2024년 2월 6일
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
Solution found after 19 iterations.
V
V = -6.5381e-17
V_check
V_check = -0.0095
% 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
Dyuman Joshi
Dyuman Joshi 2024년 2월 6일
편집: Dyuman Joshi 2024년 2월 6일
"... 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)
ans = 0.0095
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Aquatris
Aquatris 2024년 2월 6일
편집: Aquatris 2024년 2월 6일
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;

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by