Ho do I save the values of While at each hour?
54 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Umar
2024 年 8 月 4 日 14:58
編集済み: dpb
2024 年 8 月 4 日 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.
0 件のコメント
その他の回答 (1 件)
Walter Roberson
2024 年 8 月 3 日 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 件のコメント
Walter Roberson
2024 年 8 月 4 日 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.
参考
カテゴリ
Help Center および File Exchange で Model Verification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!