How can I make a constraint of particular value in if statement?

3 views (last 30 days)
Hi I have a problem about making a constraint of the b in the if statement.
Here is my code:
Q=rawdata(:,1)*0.001;
days=rawdata(:,2);
t=(1:3240)';
a=0.925;
q=zeros(size(Q));
b=zeros(size(Q));
for i=2:length(t)
if (q(i)>=0 && q(i)<=Q(i))
q(i)=a*q(i-1)+((1+a)/2)*abs((Q(i)-Q(i-1)));
end
if(b(i)>=0 && b(i) <=Q(i))
b(i)=Q(i)-q(i);
end
end
I have tried this loop several times, but the b value is always negative.
Could someone help me to make a constraint of the b, which should be greater than zero?
Thanks!

Answers (1)

Image Analyst
Image Analyst on 6 Jun 2014
You mean like adding this at the end of your loop?
if b(i) < 0
b(i) = 0;
end
or this:
b(i)=max([0, Q(i)-q(i)]);

Community Treasure Hunt

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

Start Hunting!