6
$\begingroup$

I'm interested on how to count the number of real roots of a random polynomials in an instant, how to put the result in a table and how to plot it in a graph.I generated 1 million random polynomials following Gaussian Distribution with mean 0 and variance 1. I used this command

r:=GaussianDistribution[0,1]
y=randompol[n_,i_]:=Sum[r x^{j},{j,0,n}]
Solve[y==0,x]
CountRoots[y,x]

But the command Countroots does not apply for it says that the above generated random polynomials is not univariate. But it seems awkward that when I manually paste the generated random polynomial to the CountRoots command instead of using link, it works!

But I want to do it in 1 million random polynomials and it is not practical for me to do so. I already used y//Tableform and Remove[{}] because I thought that the problem is braces since the random polynomials generated using this command y=randompol[n_,i_]:=Sum[r x^{j},{j,0,n}] is inside a brace while the CountRoots command does not require braces, but i Failed and it did not work.

please help me...

$\endgroup$
  • 2
    $\begingroup$ Your code is very confusing! The definition of r is nonsense, Solve unnecessary, your function doesn't produce any polynomials and what is i? $\endgroup$ – paw Sep 12 '14 at 12:14
  • $\begingroup$ See e.g. this Count and plot the number of solutions in an interval. $\endgroup$ – Artes Sep 12 '14 at 12:26
  • $\begingroup$ Thank You Teake Nutma,, :) it help me a lot :) I'll try your suggestion immediately..tnx a lot :) $\endgroup$ – Jerwin Sep 12 '14 at 15:16
  • $\begingroup$ Sir @Teake Nutma...your code works almost perfectly only that the last command do not execute..I lower the range up to 10 examples of 2 degree polynomials but the number of real roots does not appear? thanks a lot :) sorry for disturbance sir additionally i just want to ask why constant terms are not included in the generations of random polynomials. based on your code the power is restricted only on positive integers. $\endgroup$ – Jerwin Sep 12 '14 at 15:52
  • $\begingroup$ Sorry, I overlooked the constant terms; I've updated the answer to accommodate them. I think the reason why you weren't seeing the number of real roots was the trailing ; at the end of the lines of code -- I've also removed them. Btw, next time please post comments to answers as comments to those answers, not as a comment of your own question :). $\endgroup$ – Teake Nutma Sep 12 '14 at 18:07
9
$\begingroup$

There are a number of issues with your code. First, GaussianDistribution is not a built-in function; I think you want to have NormalDistribution in combination with RandomVariate. Secondly, SetDelayed (:=) returns Null (or $Failed if something went wrong), so y has the value Null after your second line. Hence the lines after that don't work.

Allow me to propose a function that does more or less what you want:

RandomPolynomial[degree_Integer?Positive, distribution_:NormalDistribution[]] := 
  With[{functionbody = Sum[RandomVariate @ distribution #^i, {i, 0, degree}]},
    functionbody &
  ];

You can then define a random polynomial as follows:

randompol = RandomPolynomial[4]
1.75688 - 0.0960234 # + 0.762145 #^2 - 2.28435 #^3 + 1.34854 #^4 &

This is a so-called pure function; you can let it act on e.g. the variable x to get a function of x:

randompol[x]
1.75688 - 0.0960234 x + 0.762145 x^2 - 2.28435 x^3 + 1.34854 x^4

Counting real roots then works straightforwardly:

CountRoots[randompol[x], x]
0

Then, if you want to generate one million random polynomials, you can do:

randompolys = Table[RandomPolynomial[4], {10^6}]

Finally, the number of real roots can be found by:

numrealroots = CountRoots[#[x], x] & /@ randompolys

Note that this last command will take some time to compute!

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Sir @Teake Nutma..:) tnx a lot lot.:) if it doesn't bother you sir,may I ask again a question?Im new in using Mathematica so i Cant form complex commands... Hmm... Since my research involve a very large number of data...May I ask sir how to make a code in such a way that it will form a table, where in the first column is the degree of the polynomial, the second column, composed of all possible number of real roots, the third column composed of frequency for every real root and the last column is for the product of the frequency and the number of real roots. at the right bottom is the average. $\endgroup$ – Jerwin Sep 13 '14 at 12:51
  • $\begingroup$ @Jerwin I'd recommend posting that as a new question. Small tip: include an explicit example of what you want. $\endgroup$ – Teake Nutma Sep 13 '14 at 18:47
2
$\begingroup$
ClearAll[myCount];
SetAttributes[myCount, Listable];

myCount[deg_, quant_] := (CountRoots[#, \[FormalX]] & /@ 
        ((Array[\[FormalX]^# &, deg + 1, 0].#) & /@ 
     RandomVariate[NormalDistribution[], {quant, deg + 1}]))

Needs["ErrorBarPlots`"];
ErrorListPlot[{Mean@#, StandardDeviation@#} & /@ myCount[Range@30, 100], PlotRange -> All]

Mathematica graphics

|improve this answer|||||
$\endgroup$
  • $\begingroup$ I can't get this to run; perhaps you (or I) made a copy-paste error somewhere? Also, given the OP's familiarity with Mathematica you might want to explain a bit what's going on. $\endgroup$ – Teake Nutma Sep 12 '14 at 12:52
  • $\begingroup$ @TeakeNutma I pasted an old version. Thanks. Corrected. $\endgroup$ – Dr. belisarius Sep 12 '14 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.