A for loop question
显示 更早的评论
i am simulating a solar panel and a battery attached to the solar panel. When the solar energy generated is greater than the demand the excess energy will be store in the battery up to a maximum charge rate of 3.333. The battery has a initial charge of 5kwh. i am unable to find the batterycharge at the end of the loop. All the data i get seem to be (5 + the solar generated at that numstep).
system.maxChargeRate = 3.3333
initialBatteryCharge=5
timestep=0.5
for i=1:numSteps
if solar(i) > demand(i,2)
batteryBehaviour.chargeRate(i) = min(system.maxChargeRate, solar(i) - demand(i,2));
batteryBehaviour.batteryCharge(i) = initialBatteryCharge + batteryBehaviour.chargeRate(i)*timestep
回答(1 个)
I assume, you do not want:
batteryBehaviour.batteryCharge(i) = initialBatteryCharge + ...
batteryBehaviour.chargeRate(i)*timestep
but
batteryBehaviour.batteryCharge(1) = initialBatteryCharge;
for i = 2:numSteps
...
batteryBehaviour.batteryCharge(i) = batteryBehaviour.batteryCharge(i - 1) + ...
batteryBehaviour.chargeRate(i)*timestep
...
By the way: The names of your variables are so long, that the code gets harder to read. Shadowing the built-in function "system" by a variable is a bad idea, because it can cause unexpected behavior.
类别
在 帮助中心 和 File Exchange 中查找有关 Sources 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!