Troubleshooting an Index Error in Homework Equations

In summary, the conversation discusses a MATLAB script that is trying to access elements of a transfer function's zeros vector, but the script is returning an error because the transfer function does not have any zeros. The solution is to add a check in the code to see if any zeros exist before trying to access them.
  • #1
ciekaloos
3
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
  • #2
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
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
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
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.
 
  • #6
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
 

Related to Troubleshooting an Index Error in Homework Equations

1. What is an index error and why does it occur?

An index error occurs when you try to access an item in a list or string using an index that is out of range. This means that the index you are trying to use is either negative or larger than the length of the list or string.

2. How can I identify if my homework equation has an index error?

An index error will usually result in a specific error message, such as "IndexError: list index out of range". You can also check your code to see if you are trying to access an index that is larger or smaller than the length of your list or string.

3. What are some common causes of index errors in homework equations?

One common cause of index errors is using a loop to iterate through a list or string, but forgetting to take into account the index values. Another cause is using an incorrect index value when trying to access a specific item in a list or string.

4. How can I fix an index error in my homework equation?

To fix an index error, you will need to identify the root cause of the error. This may involve checking your code for any incorrect index values, making sure you are not trying to access an index that is out of range, and properly taking into account the length of your list or string when using loops.

5. Are there any tips for avoiding index errors when writing homework equations?

One helpful tip is to always double check your code for any potential index errors before running it. This can involve checking your index values and making sure they are within the range of your list or string, as well as properly accounting for the length of your list or string when using loops.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
994
  • Engineering and Comp Sci Homework Help
Replies
6
Views
934
  • Programming and Computer Science
Replies
4
Views
735
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
Back
Top