Help on a particular case

Dear all
I'm having trouble computing something with a for loop and I have been looking for the problem for the past days.
I'm currently trying to simulate a heating installation coupled to a buffer, let's consider it a storage. I'm multiplying the produced power with a coefficient that I adapt in function of how full my buffer is at that moment. The buffer contains what it had last hour plus what was produced minus what is used by the system. Now I want to repeat this for a whole year (knowing what my demand is, let's consider that constant) and then continue computing using a new set of coefficients (all equal to one) but using the old values for produced power. This does not work. For a reason that eludes me, the buffer seems to have a huge leak when doing the second iteration and thus the power to be produced skyrockets. Since this doesn't happen the first time, I'm a bit perplex..
i = 5;
Data;
count = 0;
working = 0;
max = 1000;
start = 500;
for itration = 1:i
PowerProduced;
for B = 2:8736
c = C(ceil(B/24),1);
Buffr(B,1) = Buffr(B-1,1) + ProdP(B,itration)- Qd(B,1);
if Buffr(B,1) > max
while Buffr(B,1) > max
c = c-0.001;
Recompute;
end
end
if Buffr(B,1) < 0
while Buffr(B,1) < 0
c = c+0.001;
Recompute;
end
end
if rem(B,1000) == 0
working = working + 1
end
end
count = count+1
plot(ProdP)
end
The code for PowerProduced is here
C = ones(364,1);
if itration == 1
ProdP = zeros(8736,i);
ProdP(:,1) = repelem(times(C,Qp),24);
else
ProdP(:,itration) = times(repelem(C,24),ProdP(:,itration-1));
end
Buffr = zeros(8736,1);
Buffr(1,1) = start;
The code for recompute is
C(ceil(B/24),1) = c;
if itration == 1
ProdP(:,1) = repelem(times(C,Qp),24);
else
ProdP(B,itration) = times(c,ProdP(B,itration-1));
end
Buffr(B,1) = Buffr(B-1,1) + ProdP(B,itration)- Qd(B,1);
I'm using only 364 values for C since I do not want to act on the power to be produced every hour (which would result in 8736 values in C) but every day. Qp are the average per day of hourly production (364,1)
Qd is of size ((8736,1)
count and working are (as you will surely have understood) to see whether or not the program is stuck
Thanks a lot in advance and have a good day!
Charles

4 个评论

You should really be using functions for something like this. Calling one script from inside another tends to cause all these kind of confusions with no control over individual workspaces and everything done in the base workspace.
Thanks for the answer! So if I understand correctly you would put let's say "recompute" in function form and use it to fill the matrices? Because since I'm working with big matrices I don't really see where the advantage lies in using functions...
functions are pretty much always better than scripts for anything serious. I still use scripts, but only for quick preliminary work and then move the functionality out into functions or classes as appropriate once I have worked out what I want.
Functions don't solve all the problems, but they help you to understand them far better by reducing the clutter and possibility for parts of the program you hadn't considered making changes to variables.
superb! Will work on it and see how it develops! Thanks a lot Adam!

回答(0 个)

此问题已关闭。

标签

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by