vpapats
vpapats

Reputation: 13

Vectors with constraints

I am working for my dissertation on MATLAB and I have the following problem: I have a vector (Pbat = zeros(8760,1);) and this vector, depending on two other vectors (Pgen and Pload), should change as follows:

for i=1:length(Pload) 
    if i==1
        Pbat(i) = 1000;
    else
        if Pload(i) < Pgen(i)     %// Battery charging
            if Pbat(i) < Pbat_max && Pbat(i) > Pbat_min
                Pbat(i) = (Pbat(i-1)+(Pgen(i)-Pload(i));
            else
                Pbat(i) = Pbat(i-1);
            end
        elseif Pload(i) > Pgen(i)   %// Battery discharging
            if Pbat(i) < Pbat_max && Pbat(i) > Pbat_min
                Pbat(i) = (Pbat(i-1)-(Pload(i)-Pgen(i));
            else
                Pbat(i) = Pbat(i-1);
            end
        else
            Pbat(i) = Pbat(i-1);
        end
    end
end

However, the Pbat should be for all the values that it could get in respect constraints like Pbat_min <= Pbat(i) <= Pbat_max.

When I run the above code I get a constant value. This value is 1000 (happens wheni==1).

Upvotes: 0

Views: 62

Answers (2)

hbaderts
hbaderts

Reputation: 14316

The problem I see with your code is that you check if Pbat(i) is within the limits [Pbat_min, Pbat_max] before you calculate Pbat(i). As Pbat is initialized as 0, in this check, Pbat(i) will always be zero and the test will probably fail (if Pbat_min>0).

Now a different comment: To calculate Pbat(i), you either add (Pgen(i) - Pload(i)) or subtract (Pload(i) - Pgen(i)). This is the exact same operation (simple mathematics). Further, if that difference is zero, you add nothing - still the same operation. You can thus calculate Pbat(i) by

Pbat(i) = Pbat(i-1) + (Pgen(i) - Pload(i));

To include the charging efficiency n as described in a comment to this answer, we need some more logic, as it only applies if Pgen(i) > Pload(i), i.e. when charging. For this we can e.g. introduce an augmented efficiency

nAug = [1, n];

which means: when discharging, the efficiency is 1, when charging it is n. Now we can multiply our difference by the first element (1) if Pgen(i) <= Pload(i) or multiply it by the second element (n) otherwise. For this we use the logical expression Pgen(i)>Pload(i), which returns 0 (first case) or 1 (second case) and add 1, so we can use it to logically index the augmented vector nAug:

Pbat(i) = Pbat(i-1) + nAug((Pgen(i)>Pload(i))+1) * (Pgen(i) - Pload(i));

After calculating this, you can check, if Pbat(i) is within the bounds and set it to the boundary value otherwise:

 if Pbat(i) > Pbat_max
     Pbat(i) = Pbat_max;
 elseif Pbat(i) < Pbat_min
     Pbat(i) = Pbat_min;
 end

Everything set together gives you:

for i=1:length(Pload) 
    if i==1
        Pbat(i) = 1000;
    else
        % Calculate next value
        Pbat(i) = Pbat(i-1) + nAug((Pgen(i)>Pload(i))+1) * (Pgen(i) - Pload(i));

        % Check bounds
        if Pbat(i) > Pbat_max
            Pbat(i) = Pbat_max;
        elseif Pbat(i) < Pbat_min
            Pbat(i) = Pbat_min;
        end
    end
end

Upvotes: 2

Tam&#225;s Szab&#243;
Tam&#225;s Szab&#243;

Reputation: 733

You initialise Pbat to zeros, then, for every i you check if the current Pbat(i) is within the allowed range:

if Pbat(i) < Pbat_max && Pbat(i) > Pbat_min

I'm not sure what value you have for Pbat_min, but I guess it's safe to assume that it is non-negative, so this statement will fail for all values of i.

Upvotes: 1

Related Questions