Troubleshooting an Index Error in Homework Equations

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting an index error encountered in a MATLAB script related to control theory. Participants explore the cause of the error, which arises from attempting to access elements of an empty array, specifically in the context of transfer functions and their zeros.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes an index out of bounds error when accessing elements of the array z, which has zero elements.
  • Another participant suggests that z and p need to be defined before the loops can execute properly, indicating that the for loop will run even if z is empty.
  • A participant provides the complete script, indicating that the error persists despite the script's length.
  • There is a discussion about the nature of the transfer function's zeros and how they relate to the error encountered.
  • One participant expresses uncertainty about how to correct the absence of zeros in the transfer function.
  • Another participant recommends adding a check for the existence of zeros before accessing elements of z, suggesting the use of numel for this purpose.

Areas of Agreement / Disagreement

Participants do not reach a consensus on how to resolve the issue, as there are differing views on whether the transfer function should have zeros and how to handle the situation if it does not.

Contextual Notes

The discussion highlights the importance of understanding the properties of transfer functions in control theory, particularly regarding zeros and their implications for MATLAB operations. There are unresolved assumptions about the intended behavior of the transfer function and the expectations for its zeros.

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

  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K