Error in following MATLAB codes: "Array indices must be positive integers or logical values."

1 view (last 30 days)
When I run following codes it show following error: Array indices must be positive integers or logical values.:
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 t = 1:48
if t<14 && t>40
x1Max = 4500;
elseif t>14 && t<=40
x1Max = 3000;
end
if t==1
x3 = 40000;
flag = 1;
end
if x1(t)>x1Max
x1(t) = x1Max;
elseif x1(t)<x1Min
x1(t) = x1Min;
end
if x2(t)>x2Max
x2(t) = x2Max;
elseif x2(t)<x2Min
x2(t) = x2Min;
end
if x3(t)>x3Max
x3(t) = x3Max;
elseif x3(t)<x3Min
x3(t) = x3Min;
end
if (flag == 1)&&(x3 <= 8000)
x2(t)= x5(t) - x4(t)
elseif x2(t)< x5(t) - x4(t)
x1(t)= x5(t) - x4(t) - x2(t)
y = t-1;
x3(t) = x3(y) - (x2(t)/E_batt_inst);
flag = 1;
elseif (x3 >= 72000)
flag = 0;
x2(t)= x5(t) - x4(t)
elseif x2(t)< x5(t) - x4(t)
x1(t)= x5(t) - x4(t) - x2(t)
y = t-1;
x3(t) = x3(y) - (x2(t)/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)).

Answers (1)

Image Analyst
Image Analyst on 3 Feb 2019
We get this question a lot.
See the FAQ: FAQ

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!