How to set maximum and minimum limit of signal in MATLAB code.

121 views (last 30 days)
Usman Taya on 18 Jan 2019
Edited: Usman Taya on 20 Jan 2019
I need to set maximum and minimum value of variables. For example,
x3= x1 + x2
where
maximum value of x1 = 3
minimum value of x1 = 0
maximum value of x2 = 2.5
minimum value of x2 = -1
I didn't understand that how i can implement it in MATLAB codes.

Omer Yasin Birey on 18 Jan 2019
Edited: Omer Yasin Birey on 18 Jan 2019
Hi Usman, you can use one of the codes below. However, I recommend the first one, because no loop is necessary here.
x1Max = 3;
x1Min = 0;
x2Max = 2.5;
x2Min = -1;
x1(x1>x1Max) = x1Max;
x1(x1<x1Min) = x1Min;
x2(x2>x2Max) = x2Max;
x2(x2<x2Min) = x2Min;
x3 = x1 + x2;
or
x1Max = 3;
x1Min = 0;
x2Max = 2.5;
x2Min = -1;
x3 = zeros(length(x1),1);
for i = 1:length(x1)%or length x2 since they must have the same dimensions.
if x1(i)>x1Max
x1(i) = x1Max;
elseif x1(i)<x1Min
x1(i) = x1Min;
end
if x2(i)>x2Max
x2(i) = x2Max;
elseif x2(i)<x2Min
x2(i) = x2Min;
end
x3(i) = x1(i)+x2(i);
end

Usman Taya on 20 Jan 2019
x1Max = 3;
x1Min = 0;
x2Max = 2.5;
x2Min = -1;
x1(x1>x1Max) = x1Max;
x1(x1<x1Min) = x1Min;
x2(x2>x2Max) = x2Max;
x2(x2<x2Min) = x2Min;
x3 = x1 + x2;
When I used above codes than it shows following error:
Undefined function or variable 'x1'.
Error in Untitled2 (line 5)
x1(x1>x1Max) = x1Max;
Omer Yasin Birey on 20 Jan 2019
Hi Usman, apparently you haven't assigned any value to x1. It is hard to understand from here what it should have been given to x1, x2 or x3. Is there any text you can share that explains what code should do? So I can understand fully and help.
Usman Taya on 20 Jan 2019
Here is my actual code Brother:
x4 = [0,0,0,0,0,0,0,0,0,0,20.10,386.22,1013.36,1688.74,2685.19,2905.13,3370.94,3730.35, 2232.83,4166.60,4404.63,4853.86,4953.58,3897.27,2996.94,4328.13,4293.91,3655.74,3593.10,3080.08,2633.71,2058.05,1374.24,697.60,213.83,41.52,0,0,0,0,0,0,0,0,0,0,0,0];
x5 = [3498.91,3506.20,3491.12,3515.54,3490.08,3546.83,3491.12,3631.95,3895.54,3647.16,3498.37,3548.29,3525.54,3415.20,3394.50,3426.41,3413.20,3409.79,6531.87,6317.20,6433.33,6075.83,5983.62,5525.04,5650,5276.16,5885.04,5169.33,5908.37,5376.66,5733.37,5243.58,5792,5384.25,5408.95,5408.66,3662.29,3701.54,3688.41,3648.87,3648.58,3594.5,3521.12,3653.87,3771.70,3767.20,3541.04,3532.16];
E_batt_inst = 80000;
x3Max = 72000;
x3Min = 8000;
x2Min = -5000;
x2Max = 5000;
x1Min = 0;
x1 = zeros(48,1);
x2 = zeros(48,1);
x3 = zeros(48,1);
for i = 1:48
if i<14 && i>40
x1Max = 4500;
elseif i>14 && i<=40
x1Max = 3000;
end
if i==1
x3 = 40000;
flag = 1;
end
if x1(i)>x1Max
x1(i) = x1Max;
elseif x1(i)<x1Min
x1(i) = x1Min;
end
if x2(i)>x2Max
x2(i) = x2Max;
elseif x2(i)<x2Min
x2(i) = x2Min;
end
if x3(i)>x3Max
x3(i) = x3Max;
elseif x3(i)<x3Min
x3(i) = x3Min;
end
if (flag == 1)&&(x3 <= 8000)
x2(i)= x5(i) - x4(i)
elseif x2(i)< x5(i) - x4(i)
x1(i)= x5(i) - x4(i) - x2(i)
t = i-1;
x3(i) = x3(t) - (x2(i)/E_batt_inst);
flag = 1;
elseif (x3 >= 72000)
flag = 0;
x2(i)= x5(i) - x4(i)
elseif x2(i)< x5(i) - x4(i)
x1(i)= x5(i) - x4(i) - x2(i)
t = i-1;
x3(i) = x3(t) - (x2(i)/E_batt_inst);
end
end
Here x1, x2 and x3 is measured by given equation. x2 is power of battery which is charging and dischargine with respect to maximum and minimum capacity (x3) of battery. x1 is power imported from diesel generator. Battery is not allowed to discharge when its charged and flag = 1. When charging than battery power is in negative (-5000 (x2Min)) while discharge it is positive (5000 (x2Max)). If you want further details than please share your email address so I can email u. or please send me email on ubana50@yahoo.com