How to run the following codes.

1 次查看(过去 30 天)
what is error in following codes
for i=1:12
if i = 12
x1 = 9
end
x1 = [1];
x2 = [3];
x1(i) = x1(i-1) - x2;
x = [x1 x2]
end
When i run it it shows following error please help me to solve error.
Array indices must be positive integers or logical values.

采纳的回答

Matt Gaidica
Matt Gaidica 2019-1-24
MATLAB uses one-based indexing. Therefore, in your code, on the first iteration where i = 1, the line marked is trying to index x1 at the zero position:
for i = 1:12
if i == 12
x1 = 9
end
x1 = [1];
x2 = [3];
x1(i) = x1(i-1) - x2; % <-- error here
x = [x1 x2]
end
There are still issues with your code, but it's hard to know what the fix is without more context. Also, note that i is reserved as an imaginary number in MATLAB, so it's recommended to use ii or another variable as your iterator.
  1 个评论
Usman Taya
Usman Taya 2019-1-24
编辑:Usman Taya 2019-1-24
Here is my actual code and when i run it than it show error as above i mention.
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)). 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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by