How do I set maximum summing value
Mostra commenti meno recenti
Hello. I really need help
I have variable P_bat type 2880x1. Values are from -100 to +100. Then these values addition(sum) with for loop and I get this graph (bat_sum)

Up to here everything is working well. So i want define maximum value of bat_sum.
When the value is equal to max_value , addition must stopped but subtraction must work (specifically addition negative values must work).
For easier to understand, imagine how "battery work"
This is my code
clc; close all; clear all
P_bat=importdata('P_bat.mat')
max_value=10000
for j=1:size(P_bat(1:(end),1));
bat_sum(j)=(sum(P_bat(1:j)));
end
figure (1)
hold on
grid on
title ('bat_sum')
plot(bat_sum)
OneDrive link https://onedrive.live.com/redir?resid=DD620BBD5358C89F!71076&authkey=!AKU8HYU0jT5iTaY&ithint=folder%2cmat
if anyone has any ideas? THANKS!
Risposte (2)
Azzi Abdelmalek
il 5 Ago 2015
max_value=10000
bat_sum(1)=P_bat(1)
for jj=2:numel(P_bat)
bat_sum(jj)=bat_sum(jj-1)+P_bat(jj);
if bat_sum(jj)>1000
bat_sum(jj)=bat_sum(jj)-P_bat(jj);
end
end
plot(bat_sum)
2 Commenti
Walter Roberson
il 5 Ago 2015
The "if" should result in
bat_sum(jj) = max_value;
The value that pushes it over max_value should not be fully removed -- e.g., 990 + 15 should not go to 1005 then back to 990, it should saturate at 1000
jlive
il 8 Ago 2015
Image Analyst
il 5 Ago 2015
Put this line in at the bottom of the for loop
if bat_sum(j) > max_value
break; % Exit the for loop
end
If you want it to saturate to the max value after it attains that value, then preallocate bat_sum before the for loop:
numElements = size(P_bat(1:(end),1));
bat_sum = zeros(1, numElements);
and then have the if block do this:
if bat_sum(j) > max_value
bat_sum(j:end) = max_value; % Same value until the end.
break; % Exit the for loop
end
5 Commenti
Walter Roberson
il 5 Ago 2015
This would not reduce the sum when negative values were encountered.
Image Analyst
il 6 Ago 2015
When he said "When the value is equal to max_value , addition must stopped" I thought that meant bail out of the loop so you're not adding anything - not adding positive numbers and not adding negative numbers. Maybe still wants to add in negative numbers. It's kind of ambiguous. Maybe this is what is wanted:
max_value = 10000
bat_sum(1) = P_bat(1)
hitTheMax = false;
for k = 2 : numel(P_bat)
if hitTheMax
% We've hit the target value.
% Only add in a value if it's negative.
if P_bat(k) < 0
% Negative, so it's okay to add in now.
nextValue = bat_sum(k-1) + P_bat(k);
else
% Positive value, so don't add it in,
% just maintain the same previous value.
nextValue = bat_sum(k-1);
end
else
% Not his the max yet, so either positive
% or neative P_bat values are acceptable.
nextValue = bat_sum(k-1) + P_bat(k);
end
% Once it's past max_value,
% only add in negative values, not positive ones
% so the signal only gets smaller monotonically.
if nextValue > max_value
% Clip to max_value
bat_sum(k) = max_Value;
% Flag that we will only add in negative numbers from now on.
hitTheMax = 0;
else
% We would be less than the max
bat_sum(k) = nextValue;
end
end
plot(bat_sum)
Walter Roberson
il 6 Ago 2015
Modificato: Walter Roberson
il 6 Ago 2015
"but subtraction must work (specifically addition negative values must work)."
With his reference to "battery" I think the model should be that the accumulator saturates at the given limit, that it can never be pushed above that point but that negative values can draw it down at which point positive values can push it back up. The same thing as the way MATLAB int8 or int16 work.
>> cumsum(int8([120,15,2,-5]))
120 127 127 122
Image Analyst
il 7 Ago 2015
I wasn't sure what to make of the battery reference. I thought of that - like positive values would bring it up like a battery was recharging or something. But I don't have the Mind Reading Toolbox like you :-) so I just went verbatim by what he said - that once it hits the max, it quits adding in positive values anymore ("addition must stopped"), though I could be wrong.
jlive
il 8 Ago 2015
Categorie
Scopri di più su Mathematics in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!