# Zero and pole

1. Apr 5, 2009

### ciekaloos

1. The problem statement, all variables and given/known data

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

2. Relevant equations

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

3. The attempt at a solution

2. Apr 6, 2009

### KLoux

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

3. Apr 7, 2009

### ciekaloos

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;

4. Apr 7, 2009

### KLoux

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

5. Apr 7, 2009

### ciekaloos

yes..
im new with control theory.
i can see the problem.
its because there i no zero in my function. righ?
how im suppose to correct that.

6. Apr 7, 2009

### KLoux

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