Keep y value below certain value in a loop

Hey,
I am trying to run a function, where y can never be below zero and never above Capacity.
The function looks like that:
remainingCharge = Capacity - usage + ChargingPower
So remainingCharge = y and is not allowed to go below 0 or beyond Capacity.
I've tried:
for x = 1:1:tablesize
t.remainingCharge = Capacity - t.usage(x) + t.ChargingPower(x)
if t.remainingCharge(x) > Capacity
remainingCharge(x) == Capacity
end
end
This is not working, as Capacity needs to be as long as the tablerows - currently I am adding Capacity to the table - hopfeully this works.
In the meantime if anyone has a suggestion on how to solve this I am happily taking hints!

Respuestas (2)

Image Analyst
Image Analyst el 17 de Abr. de 2021
t.remainingCharge(x) = Capacity - t.usage(x) + t.ChargingPower(x) % x is the row number I think.
% Make it never be above Capacity
t.remainingCharge(x) = min([t.remainingCharge(x), Capacity]);
% Make it never be below 0
t.remainingCharge(x) = max([t.remainingCharge(x), 0]);

1 comentario

Image Analyst
Image Analyst el 19 de Abr. de 2021
Lukas, did min() and max() not work to clip the signal for some reason? Why not?
Also note I indexed t.remainingCharge so that you're not overwriting it each time.

Iniciar sesión para comentar.

Star Strider
Star Strider el 17 de Abr. de 2021
Editada: Star Strider el 17 de Abr. de 2021
Try something like this:
x = linspace(0, 5*pi, 250);
Capacity = 0.7;
limit_y = @(y,Capacity) (y<0).*0 + (y>Capacity).*Capacity + ((y>=0) & (y<Capacity)).*y;
y = sin(x);
figure
plot(x, y, 'LineWidth',1)
hold on
plot(x, limit_y(y,Capacity), '--', 'LineWidth',1.5)
hold off
grid
xlabel('x')
ylabel('y')
legend('Original','Limited', 'Location','best')
Note that no explicit loops are necessary, since the anonymous functrion does everything in one call.
The anonymous function is designed to work on vectors, however it is certainly possible to call the anonymous function in a loop with individual scalar values as the ‘y’ argument. It will work the same way, regardless.
Experiment with it to get the result you want.
EDIT — (17 Apr 2021 at 17:12)
Added plot image to illustrate function effect —
.

7 comentarios

Lukas Netzer
Lukas Netzer el 18 de Abr. de 2021
Thanks for your answer - this is looking promising, but for y(remainingCharge), I am still getting values above Capacity this way. I really want y(remainingCharge) in a table calculated with the limited values. Right now it is only in the plot and in the plot it does not seem to be right --> calculates graph without limit --> adds limit graph, but it does not affect the usage. Sorry my english is limited... graph looks like that, but actually I want a graph, where I start with full Capacity, then its depletting until charge possibility, where it uses a maximum but it can not go over Capacity - thanks for your help!
Star Strider
Star Strider el 18 de Abr. de 2021
My pleasure!
I would have to have at least a representative sample of your data, and a more detailed description of what you want to do.
My code addresses only the problem presented, not any others.
My data looks like that:
pCp = potential Chargingpower --> describes what could possibly be charged
nCl = needed Chargingload --> describes what power is needed
rCl = remaining Chargingload --> describes remaining Capacity
So at line 1 my car arrives at destination, at line 2 it departs from that location and used 2.1499kW there but could have loaded 993.8750. Line 3 it arrives at the next location and used 343.98kW and could not charge. At line 4 it departs from that location, used 30.4566kW there but could have loaded 537.5... and so on. Now I need to calculate rCl for every step:
rCl
1000 --> start
656.02 --> drives and needs 343.98
1000 --> arrives, needs 30.4566, but can charge 343.98+30.4566
661.3947 --> drives and needs 336.0971
...
I tried doing that:
for x = 2:2:2734
Cp(x) = min((nC(x-1)+nC(x)),pCp(x))
end
for x = 2:1:2734
rC(x) = rC(x-1) - nC(x-1) + Cp(x-1)
end
but this actually will only charge a maximum of (nC(x-1)+nC(x)) and will eventually reduce the charged load as it is not charging to full when possible.
Sorry, but I just can't get my head around it..
Thanks for still reading ;)
Star Strider
Star Strider el 18 de Abr. de 2021
I re-read this several times. I still do not understand what you want to do, or what the constraints are.
Lukas Netzer
Lukas Netzer el 19 de Abr. de 2021
Thanks for still hanging with me!
I am honestly struggling to explain it better, but will try again:
I have a car with an accumulator capacity of e.g. 1000kWh.
It starts with a full battery of 1000kWh in row 1 as it arrives at location 1.
In row 2 it is still at location 1 - needs 2.1499kWh but could potentially load the battery with 993.8750kWh (this obviously is not possible, as it can not charge over the 1000kWh capacity), therefor it loads 2.1499kWh at location 2.
In row 3 the remaining capacity is 1000kWh again (loaded to full in row 2) and it drives to location 2 and is using 343.98kWh - while on the road, it can not charge.
In row 4 it arrives at location 2 and has a remaining capacity of 656.02kWh (1000kWh-343.98kWh). At location 2 it needs 30.4566kWh but could potentially load 537.5kWh. As it cannot go over 1000kWh capacity it will load to 1000kWh again, as we have enough potential Chargingpower at this location.
In row 5 we yet again have a full battery and are on our way to location 3 and on our way there need 338.6053kWh and have no possibility to charge.
In row 6 we arrive at location 3 with a remaining load of 661.3947kWh. We will need 4.2998kWh at this location but could potentially charge 1.7*10^3 kWh. Yet again we have a surplus and we can charge to full.
And on it goes...
But somehow I can not figure out code that does that for me, as in row 16 we could potentially load to full again, but end up with 891kWh in row 17.
Is this explanation a little better this time?
I figured it out:
for x = 2:1:tablesize
rCl(x) = rCl(x-1)-nCl(x-1)+pCp(x-1)
if rCl(x) > 1000
rCl(x) = 1000
end
end
Star Strider
Star Strider el 19 de Abr. de 2021
O.K.
I still do not completely understand it, however I am happy you got it to work.

Iniciar sesión para comentar.

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

el 17 de Abr. de 2021

Comentada:

el 19 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by