# Matlab roots help

• Apr 30th 2010, 06:56 PM
YankeesFan
Matlab roots help
I need help in generating 100 different polynomials of a certain degree and then finding the total number real roots. I know that I can generate a random polynomial of say degree 5 by typing

x=roots(rand([1 6]))
I also figured out the total number of real numbers by typing
size(x(imag(x)==0))
Can someone help me in writing a for loop for this?
• Apr 30th 2010, 10:49 PM
CaptainBlack
Quote:

Originally Posted by YankeesFan
I need help in generating 100 different polynomials of a certain degree and then finding the total number real roots. I know that I can generate a random polynomial of say degree 5 by typing

x=roots(rand([1 6]))
I also figured out the total number of real numbers by typing
size(x(imag(x)==0))
Can someone help me in writing a for loop for this?

x=roots(rand([1 6]))

will not generate the polynomials and roots you want as all the coefficients will be positive and so before you start you have no positive roots.

Try:

x=roots(rand([1 6]-0.5))

CB
• Apr 30th 2010, 10:52 PM
CaptainBlack
Quote:

Originally Posted by YankeesFan
I need help in generating 100 different polynomials of a certain degree and then finding the total number real roots. I know that I can generate a random polynomial of say degree 5 by typing

x=roots(rand([1 6]))
I also figured out the total number of real numbers by typing
size(x(imag(x)==0))
Can someone help me in writing a for loop for this?

Basic for loop structure is:

Code:

```rv=zeros(1,100); for idx=1:100     %code to do what you want   rv(idx)=...  ; % assign the idx-th element of the                   % results vector to result for current                   % loop trip end```
CB