# Integrating only over positive values of an oscillating function

I have the following oscillatory function of time (it looks too lengthy!)

    myfun[t_] =
1/Log[2] (-((-E^(-0.001 t) (0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))/(1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t]))^2) + ((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (E^(-0.002 t) (0.001 Cos[
0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) (Cos[
0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]) -
0.001 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2) + (-E^(-0.001 t) \
(0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t])) (1/
2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2))/(2 (1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
3/2) \[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2))) - (3 (-E^(-0.001 t) \
(0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) \[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2)))/(2 (1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
5/2))) +
1/Log[2] Log[
1/(1 - E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) + (\[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2)))/(1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
3/2)] (-((-E^(-0.001 t) (0.001 Cos[
0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t]))/(1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t]))^2) + ((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[

0.09999499987499376 t])) (E^(-0.002 t) (0.001 \
Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[
0.09999499987499376 t]) (Cos[
0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]) -
0.001 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2) + (-E^(-0.001 t) \
(0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2))/(2 (1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
3/2) \[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +

0.010000500037503125 Sin[
0.09999499987499376 t])^2))) - (3 (-E^(-0.001 \
t) (0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) \[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2)))/(2 (1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
5/2))) +
1/Log[2] (-((-E^(-0.001 t) (0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))/(1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t]))^2) - ((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (E^(-0.002 t) (0.001 Cos[
0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) (Cos[
0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]) -
0.001 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2) + (-E^(-0.001 t) \
(0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t])) (1/
2 + 1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2))/(2 (1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
3/2) \[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +

0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2))) + (3 (-E^(-0.001 t) \
(0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) \[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2)))/(2 (1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
5/2))) +
1/Log[2] Log[
1/(1 - E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) - (\[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +

0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2)))/(1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
3/2)] (-((-E^(-0.001 t) (0.001 Cos[
0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t]))/(1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t]))^2) - ((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (E^(-0.002 t) (0.001 \
Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[
0.09999499987499376 t]) (Cos[
0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]) -
0.001 E^(-0.002 t) (Cos[0.09999499987499376 t] +

0.010000500037503125 Sin[
0.09999499987499376 t])^2) + (-E^(-0.001 t) \
(0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2))/(2 (1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
3/2) \[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2))) + (3 (-E^(-0.001 \
t) (0.001 Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) +
0.001 E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) \[Sqrt]((1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])) (1/2 +
1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2)))/(2 (1 -
E^(-0.001 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]))^(
5/2))) + (Log[
1/2 - 1/2 Sqrt[
1/2 + 1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2]] (E^(-0.002 t) (0.001 \
Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) (Cos[
0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]) -
0.001 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2))/(4 Log[2] Sqrt[
1/2 + 1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2]) - (Log[
1/2 (1 + Sqrt[
1/2 + 1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2])] (E^(-0.002 t) (0.001 \
Cos[0.09999499987499376 t] -
0.09999499987499376 Sin[0.09999499987499376 t]) (Cos[
0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t]) -
0.001 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[
0.09999499987499376 t])^2))/(4 Log[2] Sqrt[
1/2 + 1/2 E^(-0.002 t) (Cos[0.09999499987499376 t] +
0.010000500037503125 Sin[0.09999499987499376 t])^2]);


This function gives an oscillating curve. I want to integrate only over those time intervals for which curve is positive. How can this be achieved using Mathematica?

Edit: I stand corrected. I want to integrate over the time intervals for which myfun is positive (not its slope).

• "over the time intervals for which myfun is positive" - then just use Clip[] or Max[] Apr 1, 2019 at 9:22
• Could you kindly explain it as an answer? Apr 1, 2019 at 9:23

As J.M. says, clip the function to positive values:

Plot[Clip[myfun[t], {0, ∞}], {t, 0, 200}]


Then you can integrate it using Integrate or NIntegrate:

NIntegrate[Clip[myfun[t], {0, ∞}], {t, 0, 200}, Method -> "LocalAdaptive"]


266.413

Alternatively, you can notice that the positive intervals are $$[T,2T]$$, $$[3T,4T]$$, $$[5T,6T]$$ etc. with $$T=π/0.09999499987499376$$. Thus the integral over the $$i^{\text{th}}$$ positive interval is

posint[i_Integer /; i >= 1] :=
With[{T = π/0.09999499987499376},
NIntegrate[myfun[t], {t, (2 i - 1) T, (2 i) T}]]


The above result is confirmed with

Sum[posint[i], {i, 3}]


266.413

• Thanks, @Roman. I want the integral as a curve/plot (indefinite integral). Can it be done? Apr 2, 2019 at 5:27
• Your formula looks too complex for an analytic integration. Apr 2, 2019 at 9:09