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!