3
$\begingroup$

I tried manipulate simultaneously the plot of a function and its indefinite integral from 0. However, the evaluation is too slow and I believe that's not because of my computer hardware. Is there any way to optimize it?

f[x_] := (3/2) Sin[((2/10) x)^2];
i[y_] := Integrate[f[x], {x, 0, y}];
label1 = "f(x) = \!\(\*FractionBox[\(3\), \(2\)]\) \
sin(\!\(\*FractionBox[SuperscriptBox[\(x\), \(2\)], \(25\)]\))";
label2 = "\!\(\*SuperscriptBox[SubscriptBox[\(\[Integral]\), \(0\)], \
\(x\)]\) f(u) \[DifferentialD]u";

Manipulate[
 Plot[{f[a], i[a], 0 a}, {a, 0, x},
  PlotRange -> {{0, 15}, {-2, 8}}, 
  Filling -> {1 -> {3}, Axis},
  PlotLegends -> {label1, label2}],
            {x, 0.1, 15, .2}]
$\endgroup$
  • 2
    $\begingroup$ i[a] is being reintegrated at each time step. Try With[{ia=i[a]}, Manipulate[..., {f[a], ia, ...] ] instead. Works great for me. $\endgroup$ – b3m2a1 Mar 18 '17 at 20:17
  • $\begingroup$ Just worked fine! $\endgroup$ – Fred Mar 18 '17 at 20:19
  • $\begingroup$ You should also look through this for a more thorough discussion mathematica.stackexchange.com/a/28920/38205 $\endgroup$ – b3m2a1 Mar 18 '17 at 20:21
2
$\begingroup$

The simple solution to your problem is to integrate f just once before making a down-value (internal definition) of i. You can accomplish that by replacing SetDelayed ( := ) in your definition of i with Set ( = ). With this approach, plotting i becomes a simple expression evaluation in the Manipulate rather than a symbolic integration each time you move the slider for x.

f[x_] := (3/2) Sin[((2/10) x)^2]
i[y_] = Integrate[f[x], {x, 0, y}];

Manipulate[
  Plot[{f[a], i[a], 0 a}, {a, 0, x}, PlotRange -> {{0, 15}, {-2, 8}}],
  {x, 0.1, 15, .2}]

demo

$\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.