4
$\begingroup$

I have defined this function:

f[x_]:=1/(1+Sin[FractionalPart[x]])

Then I tried to plot it using this code

Plot[Integrate[f[t],{t,0,x}],{x,0,2}]

It finally plot it, but it takes about 4-5 minutes to do it. There is a way to speed up similar plots, that involve commands as FractionalPart?


EDIT: I observed that changing Integrate by NIntegrate the plot speed up a lot, but Im not sure how accurate is the numerical integration to trust in the result of the plot.

Anyway I will like to know if there are other approaches to this problem using the symbolic integration. Thank you.

$\endgroup$
3
  • 1
    $\begingroup$ Since you'e plotting over a (necessarily) limited domain: Assuming[0 < x < 2, Integrate[PiecewiseExpand[1/(1 + Sin[FractionalPart[t]]), 0 < t < 2] // Evaluate, {t, 0, x}]] $\endgroup$ Commented May 11, 2017 at 16:59
  • 1
    $\begingroup$ Could avoid repeated symbolic integrals: f[x_?NumberQ] := 1/(1 + Sin[FractionalPart[x]]) Plot[NIntegrate[f[t], {t, 0, x}], {x, 0, 2}] One could further improve by keeping track of prior quadrature results and only adding increments. $\endgroup$ Commented May 11, 2017 at 17:01
  • $\begingroup$ As a comment, looking at the function: Plot[1/(1 + Sin[FractionalPart[x]]), {x, 0, 10.7}] The integration will be Assuming[x > 0, IntegerPart[x]*Integrate[1/(1 + Sin[x]), {t, 0, 1}] + Integrate[f[t], {t, 0, FractionalPart[x]}]] which is somthig like manual using PiecewiseExpand $\endgroup$ Commented May 11, 2017 at 17:12

1 Answer 1

2
$\begingroup$

You could use DSolve to calculate the symbolic integral over a finite interval.

ClearAll[f];
f[x_] := 1/(1 + Sin[FractionalPart[x]])

F = y /. First@DSolve[{y'[x] == f[x], y[0] == 0}, y, {x, 0, 2}]

Plot[F[x], {x, 0, 2}]

Mathematica graphics

It's reasonably fast:

Mathematica graphics

Addendum: There is a reason I thought to try this. Events were added to DSolve a couple of versions ago, and these are used in discontinuity processing. What I imagine happens is this. When you've got an integrand with a standard discontinuous function, DSolve will split up the intervals and feed simple, continuous integrands to Integrate[]. It then adds up the results and pieces them together. At least that's how it seems when this trick works. I'm not familiar with the internal implementation.

$\endgroup$
3
  • $\begingroup$ this works, thank you. $\endgroup$
    – Masacroso
    Commented May 11, 2017 at 17:37
  • $\begingroup$ one last question, why you used the command /. in the definition of F? $\endgroup$
    – Masacroso
    Commented May 11, 2017 at 17:38
  • $\begingroup$ @Masacroso You're welcome, but why did you change the function after I answered? -- DSolve returns a rule y -> func, but we need func to do the graph. You could use the newer F = DSolveValue[..] instead, but in my own work, I usually want the rule until the problems are completely worked out. $\endgroup$
    – Michael E2
    Commented May 11, 2017 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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