Troubleshooting an Index Error in Homework Equations

AI Thread Summary
The discussion centers on troubleshooting an index error in a MATLAB script related to control theory. The error arises because the variable 'z' is empty, leading to an "index out of bounds" message when attempting to access its elements. The solution involves ensuring that 'z' and 'p' are defined before their elements are accessed, and implementing checks to confirm their sizes using functions like numel. Users are advised to verify the transfer function's zeros and adjust the script accordingly to prevent the error. Proper handling of empty arrays is crucial for successful execution of the script.
ciekaloos
Messages
3
Reaction score
0

Homework Statement



for i=1: max (size(z)),
if real (z(i))>0,
z(i)=-2;
end
end
for i=2: max (size(p)),
if real (p(i))>0,
p(i)=-2;
end
end


Homework Equations



why there is always error..
it said that ? Attempted to access z(1); index out of bounds because numel(z)=0.


The Attempt at a Solution

 
Physics news on Phys.org
Is this your entire script? You need to define z and p before it will run. This line:

if real (z(i))>0

is trying to access the i'th component of z. If z is not defined, you will get the error that you described.

You have your for loop set up correctly, but the for loop will always run at least once. In your case, z has numel(z) = 0 (number of elements), so maybe you don't expect the loop to execute at all, but it will. If you want, you can use a while loop instead, which won't run if the case is false the first time through.

-Kerry
 
no..
i have long script for this..
but error still occur..
can you help check the error for me..
this is the whole script:

num=[ 1 ];
den=[1 3.236068 5.236068 5.236068 3.236068 1];
[z,p,k]=tf2zp (num,den);


for i=1: max (size(z)),
if real (z(i))>0,
z(i)=-2;
end
end
for i=2: max (size(p)),
if real (p(i))>0,
p(i)=-2;
end
end


[num1,den1]=zp2tf (z,p,k);
t=0:0.1:50;
[x,y,t]=step (num1,den1); grid
printsys (num1,den1);
num1/den1;
plot (t,x,'y'); grid
title ('plot unit langkah'); grid
hold;
pause;


[zm,pm]=minreal (z,p);
[a,b,c,d]=zp2ss (zm,pm,k);
[am,bm,cm,dm]=minreal (a,b,c,d);
[ab,bb,cb,g,t]=balreal (am,bm,cm);
elim=find (g<g(1)/10);
[ar,br,cr,dr]=modred (ab,bb,cb,d,elim);
elim=find (g<g(1)/5);
[ar1,br1,cr1,dr1]=modred (ab,bb,cb,d,elim);


[numm,denn]=ss2tf (ar,br,cr,dr);
[numm1,denn1]=ss2tf (ar1,br1,cr1,dr1);
[x,y,t]=step (ar,br,cr,dr); grid
printsys (numm,denn);
plot (t,x,'g'); grid
title ('plot unit langkah');
pause;


[x,y,t]=step (ar1,br1,cr1,dr1); grid
printsys (numm1,denn1);
numm1/denn1;
plot (t,x,'r'); grid
title ('plot unit langkah');
pause;
hold off

w=0:0.10:5;
[mag,phase,w]=bode (num1,den1);
subplot (211),semilogx (w/2*pi,20*log10(mag),'y'); grid
xlabel ('Frekuensi (Hz)'),ylabel ('dB');
title ('Bode Plot');
hold;
pause;
subplot (212),semilogx (w/2*pi,phase,'y'); grid
xlabel ('Frekuensi (Hz)'),ylabel ('Fasa');
title ('Bode Plot');
hold;
pause;


[mag,phase,w]=bode (numm,denn);
subplot (211),semilogx (w/2*pi,20*log10(mag),'g'); grid
xlabel ('Frekuensi (Hz)'),ylabel ('dB');
title ('Bode Plot');
pause;
subplot (212),semilogx (w/2*pi,phase,'g'); grid
xlabel ('Frekuensi (Hz)'),ylabel ('Fasa');
title ('Bode Plot');
pause;


[mag,phase,w]=bode (numm1,denn1);
subplot (211),semilogx (w/2*pi,20*log10(mag),'r'); grid
xlabel ('Frekuensi (Hz)'),ylabel ('dB');
title ('Bode Plot');
hold;
pause;
subplot (212),semilogx (w/2*pi,phase,'r'); grid
xlabel ('Frekuensi (Hz)'),ylabel ('Fasa');
title ('Bode Plot');
hold;
pause;
 
Look at your transfer function. What are it's zeros?

If you're new to control theory and don't know what a zero is, you can type the first few lines of your script at the MATLAB prompt, and see what z is, as output from tf2zp. The z vector will contain all of your transfer function's zeros.

Do you see the problem now?

-Kerry
 
yes..
im new with control theory.
i can see the problem.
its because there i no zero in my function. righ?
how I am suppose to correct that.
 
I'm not sure what you mean by "correct that." Is your transfer function supposed to have a zero? If not, then just add something that checks to see if any zeros exist before trying to access elements of z (I recommend numel, which is what MATLAB is using to determine that you have an error).

-Kerry
 

Similar threads

Back
Top