Adding and substracting elements from different arrays element wise and repeatedly
Info
This question is closed. Reopen it to edit or answer.
Show older comments
Dear All
I'd like to do some operations between different arrays element wise and be able to repeat this a number of times.
This is my problem. I'm computing how much water is in a tank by doing as follows: (water available for the next hour in the tank) = (water stored in the tank this hour)+ (added water during this hour)- (water taken this hour (for irrigation))
I want to do this for a whole year and I now how much water is needed for every hour and how much I water I will add every hour. I also have a starting value for what's in the tank.
Tank = ones(8760,1);
Tank(1,1) = 10000;
for B = 2:8760;
Tank(B,1) = Tank(B-1,1)+ AddedWater(B-1,1) - Demand(B-1,1);
end
This does not work and does not really give an error message, it just says that the line where I do the math is faulty.. Anyone some advice?
Thanks in advance
Charles
4 Comments
Julian Rosker
on 5 Jul 2017
Would it be possible to run "size(AddedWater)" and "size(Demand)" just before the for loop and respond with those outputs?
Charles Colin
on 5 Jul 2017
Julian Rosker
on 5 Jul 2017
Demand actually looks fine, the problem seems to be in AddedWater. Since you are taking data for every hour other than the last one (one data point each iteration in the loop, from 1 to 8759 since you subtract 1 on your indexing variable), your AddedWater matrix should have at least 8759 data points. Ensure that AddedWater has the proper data and size, likely "8760 1", and try running again.
Charles Colin
on 5 Jul 2017
Answers (1)
Andrei Bobrov
on 5 Jul 2017
Tank = [10000;ones(8759,1)];
Tank_out = cumsum(Tank + AddedWater - Demand);
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!