A for loop question
Show older comments
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
Answers (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.
Categories
Find more on Sources in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!