3
$\begingroup$

I have a user defined function which is called many times in the code. I want to ask that when I am deploying the code toCDF, I need to define that user defined function also. I tried it using DynamicModule and With. But none is working. How should I give that user defined function so that it can be deployed into CDF.

$\endgroup$
2

1 Answer 1

6
$\begingroup$

Here is a small example. I saved it to CDF and it works as is in the CDF. It has small functions in the Initialization section that are used by the Dynamics inside the DynamicModule to run the simple simulation to illustrate the idea.

Notice that this is not much different from putting all your functions in the Initialization section of the Manipulate, since Manipulate is basically a DynamicModule. So same idea.

DynamicModule[{sol, angle, bob, r, time = 0, animationRate = 1, y},

 Dynamic@Grid[{{Trigger[Dynamic[time], {0, Infinity, 0.01}, 
      animationRate,
      AppearanceElements -> {"PlayPauseButton", "ResetButton"}],
     Style["time (sec)", 10], Dynamic[time]},
    {Dynamic@
      Show[Graphics[{{Dashed, Gray, Thin, Circle[{0, 0}, 1]}, {Red, 
          Thick, Line[{{0, 0}, bob}]},
         {Blue, PointSize[0.1], Point[bob]}},
        ImagePadding -> 10], ImageSize -> 300], SpanFromLeft}
    }, Frame -> True, Alignment -> Left],

 Initialization :> (
   sol := 
    First@NDSolve[{y''[t] + 0.1 y'[t] + Sin[y[t]] == 1.5 Cos[t], 
       y[0] == Pi/4, y'[0] == 0}, y, {t, time, time + 1}, 
      Sequence@ndsolveOptions];
   bob := {Sin[(y /. sol)[time]], -Cos[(y /. sol)[time]]};

   ndsolveOptions = {MaxSteps -> Infinity,
     Method -> {"StiffnessSwitching", 
       Method -> {"ExplicitRungeKutta", Automatic}},
     AccuracyGoal -> 10, PrecisionGoal -> 10}
   )]

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.