0
$\begingroup$

I'm trying to build CDF from a code that you can see on the image. However, it is painfully slow. The way to improve speed would be to reduce calculations. The same value is recalculated over and over inside each of the 4 plots, and 2 times at the end of the code. I would like to put that at the beginning of the Manipulate to be evaluated once, and then used inside each plot as a constant. In other words, if I have a repeating function f(x)=x+5, I would like to have it at the beginning to be evaluated only once, instead on every instance. I tried creating a function before manipulate is executed, but it does not get passed to the CDF file, so its declaration must be within manipulate. Any help would be greatly appreciated.

Matematica screenshot

$\endgroup$
1
  • 3
    $\begingroup$ Please start by including your code as formatted text rather than pictures. $\endgroup$
    – MarcoB
    Commented Feb 14, 2019 at 13:12

1 Answer 1

1
$\begingroup$

Maybe this will give some good ideas on how you can improve your code.

Manipulate[
  With[{k = k}, Plot[{Cos[2 Pi k t], Sin[2 Pi /k t]}, {t, 0, 1}]],
  {a, 1, 5, Appearance -> "Labeled"},
  {b, 1, 5, Appearance -> "Labeled"},
  {k, None},
  Initialization :> (k := (1 + a)/(1 + b)),
  TrackedSymbols :> {a, b}]

demo

Update

This is another way to have k computed only once and only when either a or b changes.

DynamicModule[{k},
  Manipulate[
    k = (1 + a)/(1 + b);
    Plot[{Cos[2 Pi k t], Sin[2 Pi /k t]}, {t, 0, 1}],
    {a, 1, 5, Appearance -> "Labeled"},
    {b, 1, 5, Appearance -> "Labeled"}]]]

The idea behind both of these examples is the same: make k a localized dynamic variable and control evaluation to depend only changes to a and b.

$\endgroup$

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