Need help with Matlab Script

tboo

Diamond Member
Jun 25, 2000
7,626
1
81
I need to write a Matlab function that will test a transfer function's poles. If any of the poles are in the right hand plane(ie are positive) the function should return a "1", if there are only poles in the left hand plane but one or more exist on the jw axis the function should return a "0", and if all the poles are in the left hand plane the function should return a "1". This is what I wrote so far:

function [c] =RHP_from_unity_fb(G)
% checks to make sure if system G has any poles in the RHP
c= pole(G);
if c>0
disp('1')
elseif c==0;
disp('0')

else
disp('-1')
end

Its not working as expected. For instance, it wont return a 1 if I have some poles in the LHP but also some in the RHP as needed. Any advice is appreciated.
 

sm625

Diamond Member
May 6, 2011
8,172
137
106
I need to write a Matlab function that will test a transfer function's poles. If any of the poles are in the right hand plane(ie are positive) the function should return a "1", if there are only poles in the left hand plane but one or more exist on the jw axis the function should return a "0", and if all the poles are in the left hand plane the function should return a "1". This is what I wrote so far:

function [c] =RHP_from_unity_fb(G)
% checks to make sure if system G has any poles in the RHP
c= pole(G);
if c>0
disp('1')
elseif c==0;
disp('0')

else
disp('-1')
end

Its not working as expected. For instance, it wont return a 1 if I have some poles in the LHP but also some in the RHP as needed. Any advice is appreciated.

I dont know matlab but that semicolon is generally bad form.

Also, can you use braces to make it more clear how you want your if statements to flow? In C, it would look something like this:

Code:
c= pole(G);
if (c>0)
{
    disp('1');
}
else
{
  if (c==0)
      disp('0');
  else
      disp('-1');
}

That way there is no ambiguity on the branches.
 
Last edited:

magomago

Lifer
Sep 28, 2002
10,973
14
76
Can you give a few examples of some functions(been a while since I did this....ie: a few years since college)?? It looks like it isn't an issue of your programming syntax. To the poster above, Matlab doesn't really care about semi colons for anything other than surpressing output, so that isn't his issue at all. Sure it may come down to clean code, but that is irrelevant here.

The problem probably isn't that you aren't writing matlab correctly, but you probably aren't thinking about what you want to acheive, and you aren't thinking about the actual inputs and outputs from each function.

function [c] =RHP_from_unity_fb(G)
% checks to make sure if system G has any poles in the RHP
c= pole(G);
if c>0
disp('1')
elseif c==0;
disp('0')

else
disp('-1')
end

I bet you 100 dollars that the bolded line is where you have your problem. Actually, its the FIRST problem you have. What is the output of the 'POLE' function? Pulling up matlab right now, this is what I'm seeing:

Code:
 POLE  Compute the poles of LTI models.
 
    P = POLE(SYS) computes the poles P of the LTI model SYS (P is 
    a column vector).
 
    For state-space models, the poles are the eigenvalues of the A 
    matrix or the generalized eigenvalues of the (A,E) pair in the 
    descriptor case.
 
    P = POLE(SYS,J1,...,JN) computes the poles P of the model with 
    subscripts (J1,...,JN) in the model array SYS.

So it looks like the output of P is a column vector (multiple rows, one column). I’m not positive EXACTLY how the output should be interpreted, but I can take you thiss

What you probably need to do is as follows: test each element (use the FIND function) to see if any of the elements exist in either plane.

Your cases are as follows:
Are any poles in the right hand plane?
Are any poles in the left hand plane?
Are poles only the left hand plane (but have an imaginary component)?

For the first you can go (FIND c > 0 ) % assumes that if element of vector C (nx1 vector) is > 0, it is on the right side
For the second you can go (FIND c 0) %assumes that if element of vector C (nx1 vector)< 0, it is on the left right
For the last you can proceed similarly.

Here is a quick code check for the first

Code:
cat = FIND(c > 0)  % if returns an array that isn't empty, then some elements are on the right side
cat2 = isempty(cat);
if cat2 ==1
disp('fuck yeah, something exists on the right side')
end

My only concern is based on the output of the POLE function (which I&#8217;m not clear about) I don&#8217;t see where it captures both axes (I would have assumed an n &#8211;by- 2 axis). If you give an example so I can run it through, I could probably figure the output (as I&#8217;m at work, don&#8217;t have time to figure it out; Matlab documentation is usually nice and gives you some examples, but this one is pretty sparton).


The second problem you have is one of logic. You have
(a)some ELSEIF statements in there
(b) You are comparing an vector/array/whatever to a 1x1 constant

The elseif is problematic because you are saying "quit execution of the rest of the loop once the condition is met"

Try this to understand the difference:
Code:
cat = 5
if cat > 1
print('fuck yeah!')
end
if cat > 2
print('fuck yeah!x2')
now try
Code:
if cat > 1
print('fuck yeah!')
elseif cat > 2
print('fuck yeah!x2')
end


Going to (b), try this:
Code:
c=[ -1 0 1]
if c >0
disp('cat')
end

What do you think will happen?


Resolve the three issues I said
(a) Understanding the output of the functions you call
(b) Knowing how to use your case statements
(c) Thinking about what it means to compare a constant to an array using > , < or ==

and you will figure it out.

edit:

If you post an example I could probably help you out more.
 

tboo

Diamond Member
Jun 25, 2000
7,626
1
81
Thanks for the advice, magomago. From what you have posted, I think I might have figured it out. Ill post back if it works. One question I do have is does Matlab have a and/or operator? I know it has and: & and a or: | but I cant find a and/or.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,837
4,817
75
WTF is "a and/or operator"? Can you give us a link or a truth table? Do you mean exclusive or?
 

magomago

Lifer
Sep 28, 2002
10,973
14
76
and should be
& or && it depends on where and what you use it

or should be | (vertical toothpick typically above the 'enter' key)

quick example:

Code:
cat=[1:.01:10]

find(cat>2 & cat <5)
will give you the indices of everything between that range
Code:
cat=5
dog='ha'
if ( cat==5 | strcmp(dog,'fish')
will execute whatever is in the if statement, because of the cat==5 (True) statement.

Can you please still post some sample functions? I love learning and reviewing my stuff, and this is your way of helping me out so i can jump into it and play around ;)
 
Last edited:

magomago

Lifer
Sep 28, 2002
10,973
14
76
oh i just realized what you meant



if( (statment AND statment) OR (statement OR statement) )

that would be the way

or do nested If statements.

If you really need to think of "and/or" then you haven't broken your logic down to a more fundamental level.

Matlab isn't programming at all, its just logic...