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

조회 수: 1 (최근 30일)
Alhussain 2024년 8월 3일
댓글: Walter Roberson 2024년 8월 4일
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이전 댓글 3개 표시이전 댓글 3개 숨기기
Alhussain 2024년 8월 3일
Can you please show me an example on Matlab code
dpb 2024년 8월 3일
@Walter Roberson did for you...

댓글을 달려면 로그인하십시오.

### 채택된 답변

Umar 2024년 8월 4일
편집: dpb 2024년 8월 4일
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.

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Walter Roberson 2024년 8월 3일
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없음 표시없음 숨기기
Alhussain 2024년 8월 4일
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 2024년 8월 4일
No, the logic associated with saved_soc{i}(end+1) is saving every while loop iteration for every timestep, not just one hour.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Processor Software에 대해 자세히 알아보기

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by