# Is it possible to ask Mathematica to give a table of probability that $f(x)>0$?

I have a function $$f(x)=(4 \cos 2 a x+3) (\cos a x+2 \cos 300 x),$$ and I want to calculate the probability that $$f(x)>0$$ for $$a=\{1,2,3,...,100\}$$ assuming that $$x$$ has uniform distribution. More precisely, I want to calculate the fraction $$\frac tT$$ where $$T$$ is the period of the function, and $$t$$ is the proportion of $$x\in[0,T]$$ for which $$f(x)$$ is positive.

f[x_]:=(Cos[a x] + 2 Cos[300 x]) (3 + 4 Cos[2 a x]);

• Comments are not for extended discussion; this conversation has been moved to chat.
– Kuba
May 7, 2021 at 3:28
• In case of substantial edits in the question make sure everyone is up to date by adding a note or a comment. And if the edit makes existing answers obsolete then maybe it is better to ask a separate question.
– Kuba
May 8, 2021 at 4:26
• @Kuba I don't think the revision significantly deviates from the original intention. The original Q lacked clarity in some ways, and if others wished to base their answers on risky assumptions, they should take responsibility for it. Sometimes the easiest way to clarify a Q is to post code that solves some problem and see if it's what the OP wants. Such was the case here, in which the comments were struggling to be helpful. Ulrich's first answer correctly answered the OP's intended Q, which presumably is represented above; and Ulrich's final modification does so too. May 8, 2021 at 14:06
• @MichaelE2 I agree with you and yes, my comment may seem off topic. I wanted to present a proper alternative to an aggressive comment that was left here earlier.
– Kuba
May 12, 2021 at 5:02

final modification

Numerical solution using Boole and the correct period:

prob[a_?NumericQ, b_?NumericQ] :=
Block[{zw, T},
T = 2 Pi   /GCD[a, 2 a , 3 b ];
NIntegrate[Boole[(Cos[a x] + 2 Cos[3 b x])(3+ 4 Cos[2 a x]) >= 0], {x, 0,T} ]/T]

prob[10,100]
(* 0.468571*)


Alternatively Reduce evaluates the subintervalls f>0in the range 0<x<T.

Sum["subintervals"]/T gives the probability !

probR[a_?NumericQ, b_?NumericQ] :=
Block[{ T = 2 Pi   /GCD[a, 2 a , 3 b ], zw},
zw = Reduce[{(Cos[a x] + 2 Cos[3 b x]) (3 +4 Cos[2 a x]) >= 0,0 <= x <= T}] /. Or -> List //N;
Total@Map[#[[-1]] - #[[1]] &, zw /. aa_ ==bb_ :> Nothing ]/T]

probR[10,100]
(*0.468571*)

• Comments are not for extended discussion; this conversation has been moved to chat.
– Kuba
May 8, 2021 at 4:42
• @charmin and Urlich, I didn't follow everything what was going on with this thread, just make sure that the whole thread is consistent and not confusing for future visitors.
– Kuba
May 8, 2021 at 4:43