필터 지우기
필터 지우기

Keep y value below certain value in a loop

조회 수: 2 (최근 30일)
Lukas Netzer
Lukas Netzer 2021년 4월 17일
댓글: Image Analyst 2021년 4월 19일
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!

답변 (2개)

Image Analyst
Image Analyst 2021년 4월 17일
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
Image Analyst
Image Analyst 2021년 4월 19일
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.

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2021년 4월 17일
편집: Star Strider 2021년 4월 17일
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
Lukas Netzer
Lukas Netzer 2021년 4월 19일
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 2021년 4월 19일
O.K.
I still do not completely understand it, however I am happy you got it to work.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Smoothing and Denoising에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by