MATLAB: Index exceeds array bounds. Error in PID_optimized (line 14) gc = tf(K(i)*([1 2*a(j) a(j)^2]),[1 0]); %controller transfer fx Gc

can someone help me run this code please

Hi all
Trying to run this code. I am getting an error message 'index exceeds array bounds. Error in PID_optimized (line 14). The code is given below
clc
%G(s) = 1.2/0.36s^2+1.86s^2+2.5s+1
% Gc(s) = K(s+a)^2/s
% Required to test for K and a values
K = [2.0 2.2 2.4 2.6 2.8 3.0];
a = [0.5 0.7 0.9 1.1 1.3 1.5];
%evaluate closed-loop unit-response at each 'K' and 'a' combination that
%will yield the maximum overshoot less than 10%
t = 0:0.01:5;
g = tf([1.2],[0.36 1.86 2.5 1]);
K =0; %initialization
for i = 1:6
for j = 1:6
gc = tf(K(i)*([1 2*a(j) a(j)^2]),[1 0]); %controller transfer fx Gc
G =gc*g/(1+(gc*g)); %closde-loop transfer function
y = step(G,t);
m = max(y);
if m<1.10
K = K+1;
Solution(K,:) = [K(i) a(j) m];
end
end
end
solution; %print solution table

Best Answer

Greetings,
I believe your problem is on line 10 of your code. On line 10, you redefine K as a single value rather than a vector, as such, the only legal index value for K, after line 10, is 1. It appears that you are using K as a vector and as an indexing variable.
I made the changes for you (and fixed the spelling error on line 24 and removed the semicolon so that the variable would display in the command terminal), here you go!
clc
%G(s) = 1.2/0.36s^2+1.86s^2+2.5s+1
% Gc(s) = K(s+a)^2/s
% Required to test for K and a values
K = [2.0 2.2 2.4 2.6 2.8 3.0];
a = [0.5 0.7 0.9 1.1 1.3 1.5];
%evaluate closed-loop unit-response at each 'K' and 'a' combination that
%will yield the maximum overshoot less than 10%
t = 0:0.01:5;
g = tf([1.2],[0.36 1.86 2.5 1]);
Idx =0; %initialization
for i = 1:6
for j = 1:6
gc = tf(K(i)*([1 2*a(j) a(j)^2]),[1 0]); %controller transfer fx Gc
G =gc*g/(1+(gc*g)); %closde-loop transfer function
y = step(G,t);
m = max(y);
if m<1.10
Idx = Idx+1;
Solution(Idx,:) = [K(i) a(j) m];
end
end
end
Solution %print solution table