Loop with Different Step Criteria

4 visualizaciones (últimos 30 días)
Tony Stianchie
Tony Stianchie el 25 de Feb. de 2022
Respondida: Simar el 18 de Oct. de 2023
Hi - I am trying to step through a loop taht goes:
  • on K=1 (first step), my conditions are known (temperature, T and enthalpy, h)
  • on K =2, I calculate my new enthalpy
  • on K=3 to infinity, I use my calculated enthalpy from K=2 to find my new enthalpies and temperatures.
I have my initial conditions and mesh made, but am getting lost in my loop calculations. It seems as though my enthalpy stays constant (not actually looping) . In my while loop, I need to calculate the new temperature based off (3) equations, each one is to be used when the enthalpy is within a certain range.
for K = 1
T(2:ny-1,2:nx-1)=675;
h(2:ny-1,2:nx-1)=hinitial;
end
for K = 2
h(i,j) = h(i,j)+[(k*dt)/(rho*(dx)^2)]*[(T(i+1,j))+(T(i-1,j))+(T(i,j+1))+(T(i,j-1))-4*(T(i,j))];
end
while T(i,j)>= Ts
K = K+1;
h(i,j) = h(i,j)+[(k*dt)/(rho*(dx)^2)]*[(T(i+1,j))+(T(i-1,j))+(T(i,j+1))+(T(i,j-1))-4*(T(i,j))]
if h(i,j)>= hl
T(i,j)=([h(i,j)-hl]/Cp)+Tl;
else if h(i,j)<= hs
T(i,j)=([h(i,j)-hs]/Cp)+Ts;
else if hs <= h(i,j) <= hl
T(i,j) = (1/(hl-hs))*[h(i,j)*(Tl-Ts)-(Tl*hs)+(Ts*hl)];
end
end
end
end

Respuestas (1)

Simar
Simar el 18 de Oct. de 2023
Hi Tony,
I understand that you are trying to implement a numerical method (like finite difference method) to solve a heat transfer problem. However, there are a few issues with the current implementation.
  • First, the loop indices i and j are not defined in the provided code. One needs to include loops over i and j to update h and T at each grid point.
  • Second, the condition in while loop T(i,j)>= Ts is only checking a single grid point, but you want to continue the loop until all grid points meet this condition.
  • Third, the if-else statements for updating T(i,j) based on h(i,j) are not correctly implemented. The conditions in the else if statements are not mutually exclusive, and the last condition hs <= h(i,j) <= hl is not a valid syntax in MATLAB.
Here is a corrected version of the code:
% Initial conditions
T(2:ny-1,2:nx-1) = 675;
h(2:ny-1,2:nx-1) = hinitial;
% Update h at K = 2
for i = 2:ny-1
for j = 2:nx-1
h(i,j) = h(i,j) + (k*dt)/(rho*dx^2) * (T(i+1,j) + T(i-1,j) + T(i,j+1) + T(i,j-1) - 4*T(i,j));
end
end
% Update h and T at K >= 3
while max(T(:)) >= Ts
for i = 2:ny-1
for j = 2:nx-1
h(i,j) = h(i,j) + (k*dt)/(rho*dx^2) * (T(i+1,j) + T(i-1,j) + T(i,j+1) + T(i,j-1) - 4*T(i,j));
if h(i,j) >= hl
T(i,j) = (h(i,j) - hl)/Cp + Tl;
elseif h(i,j) <= hs
T(i,j) = (h(i,j) - hs)/Cp + Ts;
elseif h(i,j) > hs && h(i,j) < hl
T(i,j) = 1/(hl - hs) * (h(i,j) * (Tl - Ts) - Tl*hs + Ts*hl);
end
end
end
end
This code is implementing a numerical method to solve a heat transfer problem.Here is what each section does:
1) Initial Conditions: The initial temperature and enthalpy are set for all points in the grid except for the boundaries. The temperature is set to 675 and the enthalpy is set to hinitial.
2) Update h at K = 2: This double for loop goes through each point in the grid (except the boundaries) and updates the enthalpy ‘h based on the surrounding temperatures. This is done using a finite difference approximation of the heat diffusion equation, which states that the change in enthalpy is proportional to the Laplacian (second spatial derivative) of the temperature.
3) Update ‘h and T at K >= 3: This part of the code updates both h and T values for time steps K >= 3. It follows a similar finite difference approximation to update h values. Then, it checks the updated h values to determine the corresponding temperature values (T) based on certain conditions.
  • If ‘h is >= hl,’ it calculates T using the equation: T(i,j) = (h(i,j) - hl)/Cp + Tl.’
  • If h <= hs,’ it calculates T using the equation: T(i,j) = (h(i,j) - hs)/Cp + Ts.’
  • If hs < h < hl,’ it calculates T using the equation: T(i,j) = 1/(hl - hs) * (h(i,j) * (Tl - Ts) - Tl*hs + Ts*hl).
4) While loop condition: The while loop continues until the maximum value of T in the entire array is greater than or equal to Ts.’ This condition ensures that the loop iterates until the desired temperature is reached.
Make sure that the variables ‘ny’,nx’, ‘k’, ‘dt’, ‘rho’, ‘dx’, ‘Ts’, ‘hl’,hs’, ‘Cp’, ‘Tl’, and ‘hinitial have been defined and assigned proper values before running the code.
Hope it helps!
Best Regards
Simar

Categorías

Más información sobre Composite Components en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by