Thread Subject: How do i get coefficients out of a function and assign it to a variable

Subject: How do i get coefficients out of a function and assign it to a variable

From: ishin

Date: 26 Nov, 2009 01:05:19

Message: 1 of 12

For example, i have a function S^3+6S^2
i want a command that will automatically take out the coefficients and do
a=[1 6]
without having me physically typing in the coefficients every time
how could this be done?
thank you in advance

Subject: How do i get coefficients out of a function and assign it to a variable

From: Darren Rowland

Date: 26 Nov, 2009 03:58:18

Message: 2 of 12

Your question is vague. Where did the function come from? How are you using it?

Subject: How do i get coefficients out of a function and assign it to a variable

From: George

Date: 26 Nov, 2009 19:36:03

Message: 3 of 12

im sorry, for example, i have a polynomial that is something like
5S^6+3S^5
and in my function, this is my output.
under the same function in the same step file, i would like to use just the coefficients ([5 3] in this case) from the results as an input for the next step.

right now i have it say a= [5 3] taken from the results. However, matlab would use [5 3] as an input regardless of what the output is from the previous command.

is there a function that will pull out the coefficients from a polynomial?
thank you

Subject: How do i get coefficients out of a function and assign it to a variable

From: us

Date: 26 Nov, 2009 20:36:03

Message: 4 of 12

"George " <iu23@drexel.edu> wrote in message <hemlb3$8t9$1@fred.mathworks.com>...
> im sorry, for example, i have a polynomial that is something like
> 5S^6+3S^5
> and in my function, this is my output.
> under the same function in the same step file, i would like to use just the coefficients ([5 3] in this case) from the results as an input for the next step.
>
> right now i have it say a= [5 3] taken from the results. However, matlab would use [5 3] as an input regardless of what the output is from the previous command.
>
> is there a function that will pull out the coefficients from a polynomial?
> thank you

one of the possible solutions
- this requires the symbolic math txb
- assume your function returns a CHAR string

     s='5*s^6+3*s^5';
     c=sym2poly(sym(s))
% c = 5 3 0 0 0 0 0
% also, see
     help coeffs;

us

Subject: How do i get coefficients out of a function and assign it to a variable

From: George

Date: 26 Nov, 2009 20:38:03

Message: 5 of 12

this is exactly what im trying to do with a and b values


% a and b are top and bottom coefficients of transfer function
a=[0.0948 0.6302 1.271 1.271 0.6415 0.193 0.0287 0.001753 2,24e-5 0 0];
b=[1 8.835 29.68 54.28 62.13 47.62 25.21 9.256 2.309 0.3717 0.035 0.001606 2.24e-5 0 0];

[r,p,k] = residue(b,a)

Subject: How do i get coefficients out of a function and assign it to a variable

From: us

Date: 26 Nov, 2009 21:06:03

Message: 6 of 12

"George " <iu23@drexel.edu> wrote in message <hemovb$hki$1@fred.mathworks.com>...
> this is exactly what im trying to do with a and b values
>
>
> % a and b are top and bottom coefficients of transfer function
> a=[0.0948 0.6302 1.271 1.271 0.6415 0.193 0.0287 0.001753 2,24e-5 0 0];
> b=[1 8.835 29.68 54.28 62.13 47.62 25.21 9.256 2.309 0.3717 0.035 0.001606 2.24e-5 0 0];
>
> [r,p,k] = residue(b,a)

well, since this obviously works
- why do you say ...what i'm trying to do...
- how does this relate to your OP

us

Subject: How do i get coefficients out of a function and assign it to a variable

From: George

Date: 27 Nov, 2009 16:50:18

Message: 7 of 12

sorry i did not see you post before i posted mine. i made my post right after i posted the second time.
thanks for your function. yes it does work. but how could i pull out coefficients from both the numerator and denominator?
thank you in advance
i.e.
C=(2S^5+4S^3)/(3S^6)

Subject: How do i get coefficients out of a function and assign it to a variable

From: Steven Lord

Date: 28 Nov, 2009 03:31:57

Message: 8 of 12


"George " <iu23@drexel.edu> wrote in message
news:hep00a$kan$1@fred.mathworks.com...
> sorry i did not see you post before i posted mine. i made my post right
> after i posted the second time.
> thanks for your function. yes it does work. but how could i pull out
> coefficients from both the numerator and denominator?
> thank you in advance
> i.e.
> C=(2S^5+4S^3)/(3S^6)

If this is a symbolic expression:

syms S
C = (2*S^5+4*S^3)/(3*S^6);

use NUMDEN, then use SYM2POLY on the two outputs of that function.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: How do i get coefficients out of a function and assign it to a variable

From: George

Date: 30 Nov, 2009 07:08:03

Message: 9 of 12

thank you again for the response. however, i still can not separate numerator and denominator.
I am trying to separate numerator and denominator of a transfer function but matlab does not accept Transfer function as an input...


rlocus(G_uncomp)
[d,n]=numden(G_uncomp);
d=sym2poly(sym(G_uncomp));
n=sym2poly(sym(G_uncomp));

((((((??? Undefined function or method 'numden' for input arguments of type 'tf'.

Error in ==> eval_water_sys at 90
[d,n]=numden(G_uncomp);))))))))


This is the error i get.


Transfer function:
 
0.474 s^8 + 3.143 s^7 + 6.302 s^6 + 5.977 s^5 + 3.106 s^4 + 0.9121 s^3 + 0.128 s^2
                                                                                    
                                                                        + 0.006587 s
                                                                                    
------------------------------------------------------------------------------------
s^12 + 8.835 s^11 + 29.68 s^10 + 54.28 s^9 + 62.13 s^8 + 47.62 s^7 + 25.21 s^6
                                                                                    
                     + 9.253 s^5 + 2.305 s^4 + 0.3687 s^3 + 0.03373 s^2 + 0.001317 s


and this is my G_uncomp

Subject: How do i get coefficients out of a function and assign it to a variable

From: us

Date: 30 Nov, 2009 08:24:04

Message: 10 of 12

"George " <iu23@drexel.edu> wrote in message <hevr0j$m2k$1@fred.mathworks.com>...
> thank you again for the response. however, i still can not separate numerator and denominator.
> I am trying to separate numerator and denominator of a transfer function but matlab does not accept Transfer function as an input...
>
>
> rlocus(G_uncomp)
> [d,n]=numden(G_uncomp);
> ((((((??? Undefined function or method 'numden' for input arguments of type 'tf'.
> Error in ==> eval_water_sys at 90
> [d,n]=numden(G_uncomp);))))))))

well, yes - NUMDEN wants a symbolic var as input...

us

Subject: How do i get coefficients out of a function and assign it to a variable

From: George

Date: 30 Nov, 2009 08:42:01

Message: 11 of 12

thanks for the response but im not really sure what that means. how can i transfer my transfer function into a symbolic form?

Subject: How do i get coefficients out of a function and assign it to a variable

From: Steven Lord

Date: 30 Nov, 2009 14:39:37

Message: 12 of 12


"George " <iu23@drexel.edu> wrote in message
news:hevr0j$m2k$1@fred.mathworks.com...
> thank you again for the response. however, i still can not separate
> numerator and denominator.
> I am trying to separate numerator and denominator of a transfer function
> but matlab does not accept Transfer function as an input...
>
>
> rlocus(G_uncomp)
> [d,n]=numden(G_uncomp);
> d=sym2poly(sym(G_uncomp));
> n=sym2poly(sym(G_uncomp));
>
> ((((((??? Undefined function or method 'numden' for input arguments of
> type 'tf'.

NUMDEN is only defined for symbolic objects. Since you don't have a
symbolic object but a tf object, you can't use this method.

What you should do instead is use the tf object's GET method to GET the
'num' and 'den' properties of the object as shown in the Example on this
documentation page.

http://www.mathworks.com/access/helpdesk/help/toolbox/control/ref/get.html

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
code us 26 Nov, 2009 15:39:08
sym us 26 Nov, 2009 15:39:08
sym2poly us 26 Nov, 2009 15:39:08
coeffs us 26 Nov, 2009 15:39:08
rssFeed for this Thread

Contact us at files@mathworks.com