Clear Filters
Clear Filters

Ho do I save the values of While at each hour?

54 views (last 30 days)
Alhussain
Alhussain on 3 Aug 2024 at 18:46
Commented: Walter Roberson on 4 Aug 2024 at 19:11
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 Comments
Alhussain
Alhussain on 3 Aug 2024 at 21:31
Can you please show me an example on Matlab code

Sign in to comment.

Accepted Answer

Umar
Umar on 4 Aug 2024 at 14:58
Edited: dpb on 4 Aug 2024 at 15:12
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.

More Answers (1)

Walter Roberson
Walter Roberson on 3 Aug 2024 at 21:16
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 Comments
Alhussain
Alhussain on 4 Aug 2024 at 7:07
This is great Mr. Roberson but what I am doing is different. I have tried yoursbut this is only save the values for one hour. I want to you understand that I am doing hourly simulation of my code starting from 1 to 8760 and I have the refilling tank process so when the level of the tank reach to the minimum the auxilary pump start to operate to refill the tank untill reach to the maximum then stop, after that the process is normal. However, in my code when I am doing a while loop to refill the tank untill when it reach to the minimum then stop at the max. After the simulation the MAtlab only show m the auxilary pump refill the tank at one hour which is not correct it should give me the hours during refilling the tank untill reach to the maximum but this is happen to me because of the while loop which doing the iteration only for that hour.
Walter Roberson
Walter Roberson on 4 Aug 2024 at 19:11
No, the logic associated with saved_soc{i}(end+1) is saving every while loop iteration for every timestep, not just one hour.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!