Ho do I save the values of While at each hour?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alhussain
el 3 de Ag. de 2024
Comentada: Walter Roberson
el 4 de Ag. de 2024
For a while loop how I can calculate the values at each hour before do a break? As we know while loop only performs the condition statment and breaks the loop, then gives you the value at that specific hour only. I provide te example below :
for i=1:Time
while water_tank_soc(i-1) < Water_tank_capacity_max
m_net_o(i) =.........
m_net_i(i) = ......
% Update SOC
water_tank_soc(i) = water_tank_soc(i-1) + m_net_i(i) - m_net_o(i);
if water_tank_soc(i) >= Water_tank_capacity_max
break
end
% Update for next time step
water_tank_soc(i-1) = water_tank_soc(i); % Update previous state for next iteration
end
end
5 comentarios
Respuesta aceptada
Umar
el 4 de Ag. de 2024
Editada: dpb
el 4 de Ag. de 2024
Hi @ Alhussain,
To modify the simulation logic to track the hours taken for refilling until the tank reaches its maximum level, you need to increment a counter variable each hour the auxiliary pump operates. This counter will keep track of the total hours taken for the tank to refill from the minimum level to the maximum level.
% Initialize variables
tankLevel = 100; % Initial tank level
minLevel = 20; % Minimum tank level
maxLevel = 80; % Maximum tank level
hoursToRefill = 0; % Counter for hours taken to refill
% Simulate tank refilling process
while tankLevel < maxLevel
if tankLevel <= minLevel
% Refill tank using auxiliary pump
tankLevel = tankLevel + 1; % Increment tank level
hoursToRefill = hoursToRefill + 1; % Increment hours counter
else
% Normal operation
tankLevel = tankLevel - 1; % Simulate tank consumption
end
end
disp(['Tank refilled in ', num2str(hoursToRefill), ' hours.']);
So, as you can see the hoursToRefill variable keeps track of the total hours taken to refill the tank and while loop continues until the tank reaches the maximum level. During the refilling process, the hoursToRefill counter is incremented for each hour the auxiliary pump operates. Please let me know if you have any further questions.
0 comentarios
Más respuestas (1)
Walter Roberson
el 3 de Ag. de 2024
Adjusted to save each iteration. A cell array is used because different iterations might run the while loop for different number of steps.
saved_soc = cell(1, Time);
for i=1:Time
saved_soc{i} = water_tank_soc(i-1);
while water_tank_soc(i-1) < Water_tank_capacity_max
m_net_o(i) =.........
m_net_i(i) = ......
% Update SOC
water_tank_soc(i) = water_tank_soc(i-1) + m_net_i(i) - m_net_o(i);
if water_tank_soc(i) >= Water_tank_capacity_max
break
end
% Update for next time step
water_tank_soc(i-1) = water_tank_soc(i); % Update previous state for next iteration
saved_soc{i}(end+1) = water_tank_soc(i-1};
end
end
2 comentarios
Walter Roberson
el 4 de Ag. de 2024
No, the logic associated with saved_soc{i}(end+1) is saving every while loop iteration for every timestep, not just one hour.
Ver también
Categorías
Más información sobre Processor Software en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!