3
$\begingroup$

In version 10 a simple numerical integraton of piecewise function is highly inefficient:

f[x_] := Which[-1 <= x <= 0, x, 0 <= x, x^2, True, 0]
ϕ[x_] := NIntegrate[f[t], {t, -1, x}]

in the version 10

whereas in the version 5 it was much faster:

enter image description here

How can I speed up the plotting (what options in Plot or in NIntegrate)?

@Micheal E2

partially you were right - there is difference in a number of points, however not so large like the diference in timing

enter image description here enter image description here

The Plot command was used to see difference in timing, however, for the same number of points older version is still better:

enter image description here enter image description here

$\endgroup$
  • $\begingroup$ How many points are plotted in each version? $\endgroup$ – Michael E2 Feb 9 '15 at 11:34
7
$\begingroup$

NIntegrate[f[t], {t, -1, x}] integrates the same thing over and over again, when a point is needed by Plot.

Integrate what you need one time only:

f[x_] = Which[-1 <= x <= 0, x, 0 <= x, x^2, True, 0];
ϕ[x_] = NDSolve[{Derivative[1][g][t] == f[t], g[-1] == 0},
g, {t, -1.5, 1}][[1, 1, 2]][x];
Plot[ϕ[x], {x, -1.5, 1}]
$\endgroup$
  • $\begingroup$ thanks for the answer, but I was just interested in the NIntegrate efficiency, maybe, I did not say it clearly. $\endgroup$ – sebqas Feb 10 '15 at 11:09
5
$\begingroup$

I agree NDSolve is the way to go, but I think I know why there has been a slow down since V5.

I believe sometime since V5 symbolic preprocessing was introduced, which can cause overhead. We can turn this off in V10 and see a drastic speed up:

f[x_] := Which[-1 <= x <= 0, x, 0 <= x, x^2, True, 0]
ϕ[x_] := NIntegrate[f[t], {t, -1, x}]

Plot[ϕ[x], {x, -1.5, 1}] // AbsoluteTiming

enter image description here

ψ[x_] := NIntegrate[f[t], {t, -1, x}, Method -> {Automatic, "SymbolicProcessing" -> False}]

Plot[ψ[x], {x, -1.5, 1}] // Quiet // AbsoluteTiming

enter image description here

Edit

I have verified that this is the reason for the slowdown. My copy of V5 does not know about the "SymbolicProcessing" submethod:

enter image description here

$\endgroup$
  • $\begingroup$ +1. I don't know when the symbolicprocessing option was introduced but its in v9 (same results). $\endgroup$ – george2079 Feb 10 '15 at 22:45
  • $\begingroup$ +1, thanks, that is what I asked! I suspected that Math is doing something other than pure numerics. $\endgroup$ – sebqas Feb 11 '15 at 8:35
  • $\begingroup$ @george2079 it looks like it was introduced in version 6. It appears in the v6 legacy documentation for NIntegrate: reference.wolfram.com/legacy/v6/ref/NIntegrate.html. $\endgroup$ – Chip Hurst Feb 11 '15 at 15:46
4
$\begingroup$
f[x_] = Piecewise[{
    {x, -1 <= x <= 0},
    {x^2, x > 0}}];

\[Phi][x_] = Assuming[Element[x, Reals],
  Integrate[f[t], {t, -1, x}]]

enter image description here

Plot[\[Phi][x], {x, -1.5, 1}] // Timing

enter image description here

$\endgroup$

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.