Index in position 1 is invalid. Array indices must be positive integers or logical values. Error in pid_optimized1 (line 17) solution(K,:) = [K a m ts];

조회 수: 1 (최근 30일)
Hi all
I am trying to run this code but I am getting an error 'Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in pid_optimized1 (line 17)
solution(K,:) = [K a m ts];'
Can someone help me to run this code.
The code is given by
clear all
clc
%Required to optimize a plant with a transfer function 4/s^3+6s^2+8s+4 with
%a PID controller whose transfer function is given by K(s+a)^2/s.
t = 0:0.01:8;
K = 0;
for K =3:0.2:5
for a =0.1:0.1:3
num =[4*K 8*K*a 4*K*a^2];
den = [1 6 8+4*K 4+8*K*a 4*K*a^2];
y = step(num,den,t);
s = 801;while y(s)>0.98&&y(s)<1.02;s = s-1;end
ts = (s-1)*0.01;%ts = settling time;
m = max(y);
if m<1.15 && m>1.10; if ts<3.00
K = K+1;
solution(K,:) = [K a m ts]; %this sorts the solution of K a m ts in a column array
end
end
end
end

채택된 답변

Alex Mcaulley
Alex Mcaulley 2019년 6월 10일
You are using K as a logical indexer, but K take non positive integers values, throwing, then, an error. I gues that the solution is:
t = 0:0.01:8;
k = 0;
for K =3:0.2:5
for a = 0.1:0.1:3
num = [4*K 8*K*a 4*K*a^2];
den = [1 6 8+4*K 4+8*K*a 4*K*a^2];
y = step(num,den,t);
s = 801;while y(s)>0.98&&y(s)<1.02;s = s-1;end
ts = (s-1)*0.01;%ts = settling time;
m = max(y);
if m < 1.15 && m > 1.10
if ts<3.00
k = k + 1;
solution(k,:) = [K a m ts]; %this sorts the solution of K a m ts in a column array
end
end
end
end
  댓글 수: 3
Bob Thompson
Bob Thompson 2019년 6월 10일
Did you make the adjustment he suggested? I copied your code in, changed it to be like Alex has posted and it ran fine for me.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Bob Thompson
Bob Thompson 2019년 6월 10일
편집: Bob Thompson 2019년 6월 10일
You are recieving the error because you are trying to use K as an index value, but K is not always a positive integer.
for K =3:0.2:5
solution(K,:) = [K a m ts];
Values of K include 3, 3.2, 3.4, 3.6, ...
Matlab cannot define an element as being in position 3.2, or any other value which isn't a positive integer.
As a side note, you should really not change the value of yoru loop index within the loop itself. It is generally considered bad practice, and is very prone to errors.

polycarp onyebuchi
polycarp onyebuchi 2019년 6월 10일
The error still persist. someone help please

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by